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

This commit is contained in:
Dave M. 2022-11-29 19:53:41 +00:00
parent f719246624
commit aba9a65a3c
14 changed files with 357 additions and 12 deletions

View File

@ -8,23 +8,24 @@ return [
Tell\I18n::class => create( Tell\I18n::class ) ->constructor(
get(Tell\Reader\JsonReader::class),
get(Tell\Reader\PhpReader::class),
getenv("DEBUG") ? create(Tell\PrintMissingKey::class) : get('tell.fallback')
get('tell.fallback') /* getenv("DEBUG") ? create(Tell\PrintMissingKey::class) : get('tell.fallback') */
),
'tell.fallback' => function($c) {
$tell = new Tell\I18n( $c->get(Tell\Reader\PhpReader::class) );
$tell->locale = "en_US";
$i18n = new Tell\I18n( $c->get(Tell\Reader\JsonReader::class), $c->get(Tell\Reader\PhpReader::class), new Tell\PrintMissingKey() );
$i18n->locale(getenv('DEFAULT_LOCAL_FALLBACK') ?: "en_US");
$i18n->initialize(true);
return $tell;
return $i18n;
},
# TODO -- accept folders from Lean Apps
Tell\Reader\PhpReader::class => function($c) {
return new Tell\Reader\PhpReader($c->get(Lean\Lean::class)->getI18n('php'), true);
return new Tell\Reader\PhpReader($c->get(Lean\Lean::class)->getI18n('php'), false);
},
Tell\Reader\JsonReader::class => function($c) {
return new Tell\Reader\JsonReader($c->get(Lean\Lean::class)->getI18n('json'), true);
return new Tell\Reader\JsonReader($c->get(Lean\Lean::class)->getI18n('json'), true, \JSON_PRETTY_PRINT);
},
LanguageHandler::class => autowire(LanguageHandler::class),

View File

@ -28,7 +28,7 @@ return [
'order' => 10,
],
[
'path' => getenv("PROJECT_PATH") . "/vendor/mcnd/lean/view/",
'path' => getenv("PROJECT_PATH") . implode(DIRECTORY_SEPARATOR, [ "", "vendor", "mcnd" , "lean" , "view" ]),
'order' => 99,
],
],

View File

@ -31,6 +31,7 @@ return array_merge(
require("$dir/auth.php"),
require("$dir/storage.php"),
require("$dir/security.php"),
require("$dir/env/" . getenv('APP_ENV') . ".php"),
[ 'config' => function () { return require(getenv("META_PATH")."/config.php"); } ]
);

View File

@ -23,7 +23,7 @@ class Form implements \Picea\Ui\Method\FormInterface {
public function execute(FormContextInterface $context) : void
{
$context->pushMessage(Message::generateSuccess(
$context->pushMessage(Lib\Message::generateSuccess(
lang("form.success")
));
}

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'];

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;

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"),
);

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;
}
}

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');
}
}

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;
}
}

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);
}
}

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;
}
}

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;
}