- Work done on the kinda-micro-framework to allow smoother deployment

This commit is contained in:
mcndave committed 2022-11-29 19:53:41 +00:00
1 parent f719246624
commit aba9a65a3c
14 files changed
+357 -12

No files matched your search

+10
View File
@@ -6,6 +6,8 @@ namespace Lean;
class Application
{
public string $name;
public string $piceaContext;
public array $piceaExtensions;
@@ -22,8 +24,16 @@ class Application
public array $tellPhp;
public array $data = [];
public function __construct(string $name) {
$this->name = $name;
}
public function fromArray(array $data) : self
{
$this->data = array_replace($this->data, $data);
if (is_array($picea = $data['picea'] ?? false)) {
if ($picea['context'] ?? false ) {
$this->piceaContext = $picea['context'];
+1 -1
View File
@@ -2,7 +2,7 @@
namespace Lean;
use CSLSJ\Common\RequestResponse\{ PdfResponse, ImageResponse, DownloadResponse };
use Lean\Response\{ PdfResponse, ImageResponse, DownloadResponse };
use Picea,
Picea\Ui\Method\FormContext;
+13 -2
View File
@@ -30,7 +30,7 @@ class Lean
foreach(array_filter($list) as $application) {
if ( $this->container->has($application) ) {
$this->applications[] = ( new Application() )->fromArray($this->container->get($application));
$this->applications[] = ( new Application($application) )->fromArray($this->container->get($application));
}
else {
throw new \RuntimeException("Trying to load an application '$application' which have not been configured yet");
@@ -38,6 +38,17 @@ class Lean
}
}
public function getApplication(string $name) : ? Application
{
foreach($this->applications as $app) {
if ($app->name === $name) {
return $app;
}
}
return null;
}
public function getPiceaContext() : string
{
foreach(array_reverse($this->applications) as $apps) {
@@ -116,7 +127,7 @@ class Lean
require($path . "http.php"),
require($path . "language.php"),
require($path . "routes.php"),
require($path . "security.php"),
# require($path . "security.php"),
require($path . "software.php"),
require($path . "template.php"),
);
+80
View File
@@ -0,0 +1,80 @@
<?php declare(strict_types=1);
namespace Lean\Response;
use function get_class, gettype, is_object, is_string, sprintfm, pathinfo, PATHINFO_EXTENSION;
use Psr\Http\Message\StreamInterface;
use Zend\Diactoros\Exception,
Zend\Diactoros\Response,
Zend\Diactoros\Stream,
Zend\Diactoros\Response\InjectContentTypeTrait;
/**
* PDF Response
*
* Allows creating a response by passing a string to the constructor;
* by default, sets a status code of 200 and sets the Content-Type header to
* application/pdf.
*/
class DownloadResponse extends Response
{
use InjectContentTypeTrait;
/**
* Create a PDF response
*
* Produces a pdf response with a Content-Type of application/json and a default
* status of 200.
*
* @param string|StreamInterface $content String or stream for the message body.
* @param int $status Integer status code for the response; 200 by default.
* @param array $headers Array of headers to use at initialization.
* @throws Exception\InvalidArgumentException if $filepath is neither a string or stream.
*/
public function __construct(string $content, string $filename, int $status = 200, array $headers = [])
{
parent::__construct(
$this->createBody($content),
$status,
$this->injectFilename($filename, $this->injectContentType("application/octet-stream", $headers))
);
}
/**
* Inject the provided Content-Type, if none is already present.
*
* @return array Headers with injected Content-Type
*/
private function injectFilename(string $filename, array $headers) : array
{
$hasContentType = array_reduce(array_keys($headers), function ($carry, $item) {
return $carry ?: (strtolower($item) === 'content-disposition');
}, false);
if (! $hasContentType) {
$headers['content-disposition'] = ['Content-Disposition:attachment; filename="' . $filename . '"'];
}
return $headers;
}
/**
* Create the message body.
*
* @param string|StreamInterface $content
* @throws Exception\InvalidArgumentException if $content is neither a string or stream.
*/
private function createBody(string $content) : StreamInterface
{
if ($content instanceof StreamInterface) {
return $content;
}
$body = new Stream('php://temp', 'wb+');
$body->write($content);
$body->rewind();
return $body;
}
}
+77
View File
@@ -0,0 +1,77 @@
<?php declare(strict_types=1);
namespace Lean\Response;
use function get_class, gettype, is_object, is_string, sprintfm, pathinfo, PATHINFO_EXTENSION;
use Psr\Http\Message\StreamInterface;
use Zend\Diactoros\Exception,
Zend\Diactoros\Response,
Zend\Diactoros\Stream,
Zend\Diactoros\Response\InjectContentTypeTrait;
/**
* PDF Response
*
* Allows creating a response by passing a string to the constructor;
* by default, sets a status code of 200 and sets the Content-Type header to
* application/pdf.
*/
class FileDownloadResponse extends Response
{
use InjectContentTypeTrait;
/**
* Create a PDF response
*
* Produces a pdf response with a Content-Type of application/json and a default
* status of 200.
*
* @param string|StreamInterface $filepath String or stream for the message body.
* @param int $status Integer status code for the response; 200 by default.
* @param array $headers Array of headers to use at initialization.
* @throws Exception\InvalidArgumentException if $filepath is neither a string or stream.
*/
public function __construct($filepath, int $status = 200, array $headers = [])
{
$body = $this->createBody($filepath);
parent::__construct(
$body,
$status,
$this->injectContentType( (new \Mimey\MimeTypes())->getMimeType( pathinfo($filepath, PATHINFO_EXTENSION) ), $headers)
);
}
/**
* Create the message body.
*
* @param string|StreamInterface $filepath
* @throws Exception\InvalidArgumentException if $filepath is neither a string or stream.
*/
private function createBody($filepath) : StreamInterface
{
if ($filepath instanceof StreamInterface) {
return $filepath;
}
if (! is_string($filepath)) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid content (%s) provided to %s',
(is_object($filepath) ? get_class($filepath) : gettype($filepath)),
__CLASS__
));
}
if ( ! file_exists($filepath) ) {
throw new Exception\InvalidArgumentException("Given file (%s) do not look like a valid file path.", $filepath);
}
if ( ! is_readable($filepath) ) {
throw new Exception\InvalidArgumentException("Given file (%s) do not seem to be readable. This could indicate a permission problem", $filepath);
}
return new Stream(fopen($filepath, "r"), 'r');
}
}
+74
View File
@@ -0,0 +1,74 @@
<?php declare(strict_types=1);
namespace Lean\Response;
use function get_class, gettype, is_object, is_string, sprintf;
use Psr\Http\Message\StreamInterface;
use Laminas\Diactoros\Exception,
Laminas\Diactoros\Response,
Laminas\Diactoros\Stream,
Laminas\Diactoros\Response\InjectContentTypeTrait;
/**
* PDF Response
*
* Allows creating a response by passing a string to the constructor;
* by default, sets a status code of 200 and sets the Content-Type header to
* application/pdf.
*/
class ImageResponse extends Response
{
use InjectContentTypeTrait;
/**
* Create a PDF response
*
* Produces a pdf response with a Content-Type of application/json and a default
* status of 200.
*
* @param string|StreamInterface $content String or stream for the message body.
* @param int $status Integer status code for the response; 200 by default.
* @param array $headers Array of headers to use at initialization.
* @throws Exception\InvalidArgumentException if $content is neither a string or stream.
*/
public function __construct($content, int $status = 200, array $headers = [])
{
if ( false === ( array_change_key_case($headers)['content-disposition'] ?? false )) {
$headers['Content-Disposition'] = 'inline';
}
parent::__construct(
$this->createBody($content),
$status,
$this->injectContentType('image', $headers)
);
}
/**
* Create the message body.
*
* @param string|StreamInterface $content
* @throws Exception\InvalidArgumentException if $content is neither a string or stream.
*/
private function createBody($content) : StreamInterface
{
if ($content instanceof StreamInterface) {
return $content;
}
if (! is_string($content)) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid content (%s) provided to %s',
(is_object($content) ? get_class($content) : gettype($content)),
__CLASS__
));
}
$body = new Stream('php://temp', 'wb+');
$body->write($content);
$body->rewind();
return $body;
}
}
+20
View File
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);
namespace Lean\Response;
use Zend\Diactoros\Response\HtmlResponse;
use League\CommonMark\CommonMarkConverter;
/**
* Markdown response.
*
* Permet de convertir un fichier Markdown en html, et utilise en suite le HtmlResponse
* pour pousser le résultat en ligne, avec un code 200 par défault et un Content-Type approprié
*/
class MarkdownResponse extends HtmlResponse
{
public function __construct(string $markdown, int $status = 200, array $headers = [])
{
parent::__construct(( new CommonMarkConverter() )->convertToHtml($markdown), $status, $headers);
}
}
+70
View File
@@ -0,0 +1,70 @@
<?php declare(strict_types=1);
namespace Lean\Response;
use function get_class, gettype, is_object, is_string, sprintf;
use Psr\Http\Message\StreamInterface;
use Zend\Diactoros\Exception,
Zend\Diactoros\Response,
Zend\Diactoros\Stream,
Zend\Diactoros\Response\InjectContentTypeTrait;
/**
* PDF Response
*
* Allows creating a response by passing a string to the constructor;
* by default, sets a status code of 200 and sets the Content-Type header to
* application/pdf.
*/
class PdfResponse extends Response
{
use InjectContentTypeTrait;
/**
* Create a PDF response
*
* Produces a pdf response with a Content-Type of application/json and a default
* status of 200.
*
* @param string|StreamInterface $content String or stream for the message body.
* @param int $status Integer status code for the response; 200 by default.
* @param array $headers Array of headers to use at initialization.
* @throws Exception\InvalidArgumentException if $content is neither a string or stream.
*/
public function __construct($content, int $status = 200, array $headers = [])
{
parent::__construct(
$this->createBody($content),
$status,
$this->injectContentType('application/pdf; charset=utf-8', $headers)
);
}
/**
* Create the message body.
*
* @param string|StreamInterface $content
* @throws Exception\InvalidArgumentException if $content is neither a string or stream.
*/
private function createBody($content) : StreamInterface
{
if ($content instanceof StreamInterface) {
return $content;
}
if (! is_string($content)) {
throw new Exception\InvalidArgumentException(sprintf(
'Invalid content (%s) provided to %s',
(is_object($content) ? get_class($content) : gettype($content)),
__CLASS__
));
}
$body = new Stream('php://temp', 'wb+');
$body->write($content);
$body->rewind();
return $body;
}
}
+2 -1
View File
@@ -64,6 +64,7 @@ class Routing {
public function registerRoute(ContainerInterface $container, string $urlBase) {
$this->router->group(rtrim($urlBase, "/"), function (RouteGroup $route) use ($container) {
foreach($this->fetcher->compile() as $annotation) {
# Register routes to the UrlExtension from picea (handling url, route and asset extensions)
if ( null !== ( $name = $annotation->name ?? null ) ) {
@@ -97,10 +98,10 @@ class Routing {
}
if ( $forbidden = $this->security->taxus($class, $method, $object->user ?? null) ) {
return $forbidden;
}
# @TODO of course, this as to go ; moving to a simple callback method soon which can then be fed by Picea
if ( $container->has(Picea::class) ) {
$container->get(Picea::class)->globalVariables['route'] = $annotation;
}