From 08c27e38b0755cdf71176156ac4f38e24ae29913 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Fri, 3 Nov 2023 16:28:31 +0000 Subject: [PATCH] - Added an HttpFactory and beginning the removal of old annotations from Notes --- meta/definitions/authorize.php | 17 ++++++ meta/definitions/cronard.php | 2 +- meta/definitions/negundo.php | 6 +-- meta/definitions/routes.php | 18 +++++-- skeleton/meta/config.php | 16 ++++-- skeleton/meta/definitions/definitions.php | 11 +++- skeleton/meta/definitions/security.php | 4 +- src/ControllerTrait.php | 65 +++++++++++------------ src/Factory/HttpFactory.php | 55 +++++++++++++++++++ src/Lean.php | 1 + 10 files changed, 143 insertions(+), 52 deletions(-) create mode 100644 meta/definitions/authorize.php create mode 100644 src/Factory/HttpFactory.php diff --git a/meta/definitions/authorize.php b/meta/definitions/authorize.php new file mode 100644 index 0000000..0545d02 --- /dev/null +++ b/meta/definitions/authorize.php @@ -0,0 +1,17 @@ + create(AuthorizeMiddleware::class)->constructor(get('authorize.error')), + + 'authorize.error' => function($c) { + return HttpFactory::createJsonResponse([ + 'api.error' => "Authorization failed", + 'api.datetime' => ( new \DateTime )->format(\DateTime::ATOM), + ]); + }, +]; \ No newline at end of file diff --git a/meta/definitions/cronard.php b/meta/definitions/cronard.php index 3fc0ff5..33fc66e 100644 --- a/meta/definitions/cronard.php +++ b/meta/definitions/cronard.php @@ -15,7 +15,7 @@ return [ CronardMiddleware::class => function($c) { $cronardMiddleware = new CronardMiddleware($c, getenv('CRON_KEY'), function() : ResponseInterface { return new HtmlResponse(sprintf("%s - cron task begin...", date('Y-m-d H:i:s'))); - }, [], $c->get(TaskFetcher::class)); + }, []); return $cronardMiddleware->fromFile(getenv("META_PATH")."/crontab.php")->fromAnnotations($c->get(TaskFetcher::class)); }, diff --git a/meta/definitions/negundo.php b/meta/definitions/negundo.php index 3d61e5c..67fb3ee 100644 --- a/meta/definitions/negundo.php +++ b/meta/definitions/negundo.php @@ -6,7 +6,7 @@ use Negundo\Client\{ SoftwareConfig, Dump, Task, NegundoMiddleware }; return [ SoftwareConfig::class => create(SoftwareConfig::class)->constructor(getenv('NEGUNDO_HASH'), getenv('NEGUNDO_SERVER')), - # NegundoMiddleware::class => create(NegundoMiddleware::class)->constructor(get(SoftwareConfig::class)), - Dump::class => create(Dump::class)->constructor(get(SoftwareConfig::class)), - Task::class => create(Task::class)->constructor(get(SoftwareConfig::class)), + NegundoMiddleware::class => autowire(NegundoMiddleware::class), + Dump::class => autowire(Dump::class), + Task::class => autowire(Task::class), ]; diff --git a/meta/definitions/routes.php b/meta/definitions/routes.php index 12540f2..7bcaa38 100644 --- a/meta/definitions/routes.php +++ b/meta/definitions/routes.php @@ -53,15 +53,23 @@ return [ ApplicationStrategy::class => autowire(\Lean\ApplicationStrategy::class), - 'routes.middlewares' => [ "dump", "errorHandler", SessionMiddleware::class, CronardMiddleware::class, Mcnd\Event\EventMiddleware::class, Mcnd\CLI\CliMiddleware::class, ], + 'routes.middlewares' => [ + "dump", "errorHandler", SessionMiddleware::class, CronardMiddleware::class, Mcnd\Event\EventMiddleware::class, Mcnd\CLI\CliMiddleware::class, + ], + + 'app.middlewares' => [], 'routes.list' => function($c) { return function (ContainerInterface $container) { $router = $container->get(Router::class); - - foreach($container->get('routes.middlewares') as $i => $middleware) { - if ( $container->has($middleware) ) { - $router->middleware($container->get($middleware)); + + foreach([ 'routes.middlewares', 'app.middlewares' ] as $key) { + if ($container->has('app.middlewares')) { + foreach ($container->get($key) as $i => $middleware) { + if ($container->has($middleware)) { + $router->middleware($container->get($middleware)); + } + } } } diff --git a/skeleton/meta/config.php b/skeleton/meta/config.php index 2e40b90..d3c0275 100644 --- a/skeleton/meta/config.php +++ b/skeleton/meta/config.php @@ -31,11 +31,17 @@ return [ ], ], - #'sqlite' => [ - # 'adapter' => getenv("SQLITE_ADAPTER"), - # 'path' => getenv('PROJECT_PATH') . DIRECTORY_SEPARATOR . getenv("SQLITE_PATH"), - # 'pragma' => explode(',', getenv("SQLITE_PRAGMA")), - #], + 'sqlite' => [ + 'adapter' => getenv("SQLITE_ADAPTER"), + 'path' => getenv('PROJECT_PATH') . DIRECTORY_SEPARATOR . getenv("SQLITE_PATH"), + 'pragma' => explode(',', getenv("SQLITE_PRAGMA")), + 'pragma_begin' => array_merge( + explode(',', getenv("SQLITE_PRAGMA_BEGIN")), explode(',', getenv('DEBUG') ? getenv("SQLITE_PRAGMA_DEBUG_BEGIN") : "") + ), + 'pragma_close' => array_merge( + explode(',', getenv("SQLITE_PRAGMA_CLOSE")), explode(',', getenv('DEBUG') ? getenv("SQLITE_PRAGMA_DEBUG_CLOSE") : "") + ), + ], ] ] ]; diff --git a/skeleton/meta/definitions/definitions.php b/skeleton/meta/definitions/definitions.php index b909892..90ea4f2 100644 --- a/skeleton/meta/definitions/definitions.php +++ b/skeleton/meta/definitions/definitions.php @@ -7,7 +7,7 @@ $dir = getenv("META_PATH") . "/definitions"; return array_merge( Lean\Lean::definitions(), - Lean\Console\Lean::definitions(), + Lean\Lean::autoloadDefinitionsFromComposerExtra(), [ '%APPKEY%' => [ @@ -38,11 +38,18 @@ return array_merge( '%ESCAPED_NAMESPACE%\\Controller' => implode(DIRECTORY_SEPARATOR, [ getenv("PROJECT_PATH"), 'src', 'Controller', '' ]), ], ], + + 'app.middlewares' => [], ], 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"); } ] + [ + 'config' => function () { return array_replace( + require(getenv("META_PATH")."/config.php"), + Lean\Lean::autoloadConfigFromComposerExtra(), + ); } + ] ); diff --git a/skeleton/meta/definitions/security.php b/skeleton/meta/definitions/security.php index 892ac27..7647b7f 100644 --- a/skeleton/meta/definitions/security.php +++ b/skeleton/meta/definitions/security.php @@ -1,13 +1,13 @@ function ($c) { return ( new Taxus( $c->get(PermissionGrantInterface::class) ) )->add( diff --git a/src/ControllerTrait.php b/src/ControllerTrait.php index 21e3a77..ea0b2be 100644 --- a/src/ControllerTrait.php +++ b/src/ControllerTrait.php @@ -2,33 +2,19 @@ namespace Lean; -use Lean\Response\{FileDownloadResponse, PdfResponse, ImageResponse, DownloadResponse}; - -use Picea, - Picea\Ui\Method\FormContext; - -use Psr\Http\Message\ServerRequestInterface; -use Storage\Session; -use Laminas\Diactoros\Response\{EmptyResponse, HtmlResponse, TextResponse, RedirectResponse, JsonResponse}; - -use Ulmus\EntityCollection; - -use Psr\Http\Message\ResponseInterface; - +use Lean\Factory\HttpFactory; +use Notes\Route\Attribute\Object\Route; +use Notes\Security\Attribute\Security; +use Picea, Picea\Ui\Method\FormContext; use TheBugs\Email\MailerInterface; +use Storage\Session; -use Notes\Cronard\Annotation\Method\Cronard, - Notes\Breadcrumb\Annotation\Method\Breadcrumb, - Notes\Route\Annotation\Object\Route as RouteParam, - Notes\Route\Annotation\Method\Route, - Notes\Security\Annotation\Security, - Notes\Security\Annotation\Taxus, - Notes\Tell\Annotation\Language; +use Psr\Http\Message\{ ServerRequestInterface, ResponseInterface }; use function file_get_contents; -#[\Notes\Security\Attribute\Security(locked: true)] -#[\Notes\Route\Attribute\Object\Route(method: [ "GET", "POST", "DELETE" ])] +#[Security(locked: true)] +#[Route(method: [ "GET", "POST", "DELETE" ])] trait ControllerTrait { public ? \Notes\Breadcrumb\Breadcrumb $breadcrumb; @@ -52,7 +38,7 @@ trait ControllerTrait { public function renderRawView(string $view, ?array $variables = null) : string { if ( null === $content = $this->picea->renderHtml($view, $variables ?? [], $this) ) { - throw new \RuntimeException("Picea's renderHtml() returned NULL as result ; an error occured within your template `$view`."); + throw new \RuntimeException("Picea's renderHtml() returned NULL as result ; an error occurred within your template `$view`."); } return $content; @@ -78,47 +64,58 @@ trait ControllerTrait { } protected function redirect(string $url, int $code = 302, array $headers = []) { - return new RedirectResponse($url, $code, $headers); + return HttpFactory::createRedirectResponse($url, $code, $headers); } public static function renderNothing(int $code = 204, array $headers = []) : ResponseInterface { - return new EmptyResponse($code, $headers); + return HttpFactory::createEmptyResponse($code, $headers); } - public static function renderText(string $html, int $code = 200, array $headers = []) : ResponseInterface + public static function renderText(string $text, int $code = 200, array $headers = []) : ResponseInterface { - return new TextResponse($html, $code, $headers); + return HttpFactory::createTextResponse($text, $code, $headers); } public static function renderHtml(string $html, int $code = 200, array $headers = []) : ResponseInterface { - return new HtmlResponse($html, $code, $headers); + return HttpFactory::createHtmlResponse($html, $code, $headers); } public static function renderJson(mixed $data, int $code = 200, array $headers = []) : ResponseInterface { - return new JsonResponse($data, $code, $headers); + return HttpFactory::createJsonResponse($data, $code, $headers); } - public function renderPdf($rawdata, int $status = 200, array $headers = []) : PdfResponse + public function renderPdf($rawdata, int $status = 200, array $headers = []) : ResponseInterface { - return new PdfResponse($rawdata, $status, $headers); + return HttpFactory::createPdfResponse($rawdata, $status, $headers); } public static function renderDownloadable(string $data, string $filename, int $code = 200, array $headers = []) : ResponseInterface { - return new DownloadResponse($data, $filename, $code, $headers); + return HttpFactory::createDownloadableResponse($data, $filename, $code, $headers); } public static function renderImage(string $data, int $code = 200, array $headers = []) : ResponseInterface { - return new ImageResponse($data, $code, $headers); + return HttpFactory::createImageResponse($data, $code, $headers); } public static function renderAsset(string $path, int $code = 200, array $headers = []) : ResponseInterface { - return new FileDownloadResponse($path, $code, $headers); + return HttpFactory::createFileDownloadResponse($path, $code, $headers); + } + + public function renderMarkdown(string $filepath, int $code = 200, array $headers = []) : ResponseInterface + { + if ( ! class_exists(CommonMarkConverter::class)) { + throw new \BadFunctionCallException("League\CommonMark seems to be missing, please install dependency before trying to render Markdown content"); + } + + $markdown = ( new CommonMarkConverter() )->convertToHtml(file_get_contents($filepath)); + + return $this->renderView("docs", get_defined_vars()); } public function renderCLI(ServerRequestInterface $request, mixed $data) : ResponseInterface diff --git a/src/Factory/HttpFactory.php b/src/Factory/HttpFactory.php new file mode 100644 index 0000000..aeec5fa --- /dev/null +++ b/src/Factory/HttpFactory.php @@ -0,0 +1,55 @@ +