From 08c27e38b0755cdf71176156ac4f38e24ae29913 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Fri, 3 Nov 2023 16:28:31 +0000 Subject: [PATCH 1/7] - 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 @@ + Date: Fri, 3 Nov 2023 19:51:39 -0400 Subject: [PATCH 2/7] - WIP on adding of HttpFactory and work on authorization from Ulmus/User too --- meta/definitions/authorize.php | 41 +++++++++++++++++++++++++----- meta/definitions/cli.php | 4 +-- meta/definitions/cronard.php | 6 ++--- meta/definitions/event.php | 4 +-- meta/definitions/routes.php | 15 +---------- meta/definitions/software.php | 8 ------ skeleton/meta/definitions/auth.php | 20 +++------------ src/Event/RoutingCompileRoutes.php | 2 +- src/Event/RoutingMapRoutes.php | 2 +- 9 files changed, 47 insertions(+), 55 deletions(-) diff --git a/meta/definitions/authorize.php b/meta/definitions/authorize.php index 0545d02..b5fbecd 100644 --- a/meta/definitions/authorize.php +++ b/meta/definitions/authorize.php @@ -1,17 +1,46 @@ create(AuthorizeMiddleware::class)->constructor(get('authorize.error')), + Authenticate::class => create(Authenticate::class)->constructor(get(Session::class), get(Cookie::class), get('authentication.method')), + + HeaderAuthenticationMiddleware::class => create(HeaderAuthenticationMiddleware::class)->constructor(get(Entity\UserInterface::class), get('authorize.error')), + + PostRequestAuthentication::class => create(PostRequestAuthentication::class)->constructor(get(\Ulmus\User\Lib\Authenticate::class), "email", "email", "password"), + + PostRequestAuthenticationMiddleware::class => create(PostRequestAuthenticationMiddleware::class)->constructor(get(Entity\UserInterface::class), get('authentication.error'), get(PostRequestAuthentication::class)), + + 'authentication.method' => null, 'authorize.error' => function($c) { - return HttpFactory::createJsonResponse([ - 'api.error' => "Authorization failed", - 'api.datetime' => ( new \DateTime )->format(\DateTime::ATOM), - ]); + return function(array $errorData) { + return HttpFactory::createJsonResponse($errorData + [ + 'api.error' => "Authorization failed", + 'api.datetime' => (new \DateTime)->format(\DateTime::ATOM), + ]); + }; + }, + + 'authentication.error' => function($c, Picea $picea) { + return function($message) use ($picea) { + return HttpFactory::createHtmlResponse($picea->renderHtml('lean/error/500', [ + 'title' => "Authentication failed", + 'subtitle' => "", + 'message' => $message, + ])); + }; }, ]; \ No newline at end of file diff --git a/meta/definitions/cli.php b/meta/definitions/cli.php index c6237cd..903aedf 100644 --- a/meta/definitions/cli.php +++ b/meta/definitions/cli.php @@ -8,7 +8,7 @@ use Mcnd\CLI\CliMiddleware; use Notes\CLI\CommandFetcher; -use Lean\Lean; +use Lean\{ Factory, Lean }; return [ CommandFetcher::class => function($c) { @@ -27,7 +27,7 @@ return [ 'cli.response:default' => function($c) { return function() { - return new \Laminas\Diactoros\Response\TextResponse("This is the default response from CLI middleware which indicates that no command were registered for this application.\n"); + return Factory\HttpFactory::createTextResponse("This is the default response from CLI middleware which indicates that no command were registered for this application.\n"); }; }, ]; diff --git a/meta/definitions/cronard.php b/meta/definitions/cronard.php index 33fc66e..ee1fc5c 100644 --- a/meta/definitions/cronard.php +++ b/meta/definitions/cronard.php @@ -2,19 +2,17 @@ use function DI\autowire, DI\create, DI\get; -use Laminas\Diactoros\Response\HtmlResponse; - use Cronard\CronardMiddleware, Notes\Cronard\TaskFetcher; use Psr\Http\Message\ResponseInterface; -use Lean\Lean; +use Lean\{ Factory, Lean }; 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'))); + return Factory\HttpFactory::createHtmlResponse(sprintf("%s - cron task begin...", date('Y-m-d H:i:s'))); }, []); return $cronardMiddleware->fromFile(getenv("META_PATH")."/crontab.php")->fromAnnotations($c->get(TaskFetcher::class)); diff --git a/meta/definitions/event.php b/meta/definitions/event.php index 2e5fdd6..36bd99d 100644 --- a/meta/definitions/event.php +++ b/meta/definitions/event.php @@ -26,7 +26,7 @@ return [ Event\EventDefinition::class => function($c) { return new Event\EventDefinition([ new class() implements RoutingCompileRoutes { - public function execute(Routing $routing, Route|\Notes\Route\Annotation\Method\Route $attribute) : void + public function execute(Routing $routing, Route $attribute) : void { if (null !== ($name = $attribute->name ?? null)) { $routing->extension->registerRoute($name, $attribute->getRoute(), $attribute->class, $attribute->classMethod, $attribute->methods ?? (array)$attribute->method); @@ -35,7 +35,7 @@ return [ }, new class() implements RoutingMapRoutes { - public function execute(Routing $routing, ContainerInterface $container, ServerRequestInterface & $request, Route|\Notes\Route\Annotation\Method\Route $attribute) : void + public function execute(Routing $routing, ContainerInterface $container, ServerRequestInterface & $request, Route $attribute) : void { $class = $attribute->class; $method = $attribute->classMethod; diff --git a/meta/definitions/routes.php b/meta/definitions/routes.php index 7bcaa38..ee3e7a0 100644 --- a/meta/definitions/routes.php +++ b/meta/definitions/routes.php @@ -3,29 +3,16 @@ use function DI\autowire, DI\create, DI\get; use League\Route\Strategy\ApplicationStrategy, - League\Route\Http\Exception\NotFoundException, League\Route\Router; -use Psr\Http\Message\ResponseFactoryInterface, - Psr\Container\ContainerInterface, - Psr\Http\Message\ResponseInterface, - Psr\Http\Message\ServerRequestInterface, - Psr\Http\Server\MiddlewareInterface, - Psr\Http\Server\RequestHandlerInterface, - Psr\SimpleCache\CacheInterface; - -use TheBugs\JavascriptMiddleware; +use Psr\Container\ContainerInterface; use Cronard\CronardMiddleware; -use Tuupola\Middleware\HttpBasicAuthentication; - use Notes\Route\RouteFetcher; use Notes\Breadcrumb\Breadcrumb; -use Ulmus\User\Lib\Authenticate; - use Storage\SessionMiddleware; return [ diff --git a/meta/definitions/software.php b/meta/definitions/software.php index ddda44b..56bbc16 100644 --- a/meta/definitions/software.php +++ b/meta/definitions/software.php @@ -2,18 +2,10 @@ use function DI\autowire, DI\create, DI\get; -use Laminas\Diactoros\Response\HtmlResponse; - use TheBugs\JavascriptMiddleware; - -use Cronard\CronardMiddleware, - Notes\Cronard\TaskFetcher; - use Lean\Lean; -use Psr\Http\Message\ResponseInterface; - use Storage\Cookie, Storage\Session, Storage\SessionMiddleware; diff --git a/skeleton/meta/definitions/auth.php b/skeleton/meta/definitions/auth.php index 56e9f64..0b05eab 100644 --- a/skeleton/meta/definitions/auth.php +++ b/skeleton/meta/definitions/auth.php @@ -22,29 +22,15 @@ use TheBugs\Email\{ EmailConfiguration, MailerInterface, SwiftMailer }; use Taxus\{ Taxus, PermissionGrantInterface }; return [ - Entity\User::class => autowire(Entity\User::class), - - Authenticate::class => create(Authenticate::class)->constructor(get(Session::class), get(Cookie::class), get('authentication.method')), + \Ulmus\User\Entity\UserInterface::class => autowire(Entity\User::class), SecurityHandler::class => create(SecurityHandler::class)->constructor(function() { - return new RedirectResponse(getenv("URL_BASE")."/login"); + return Lean\Factory\HttpFactory::createRedirectResponse(getenv("URL_BASE")."/login"); }, get('authentication.unauthorize'), get(Taxus::class)), - 'authentication.method' => null, - - 'authentication.error' => function($c, Picea $picea) { - return function($message) use ($picea) { - return new HtmlResponse($picea->renderHtml('lean/error/500', [ - 'title' => "", - 'subtitle' => "", - 'message' => $message, - ])); - }; - }, - 'authentication.unauthorize' => function($c, Picea $picea) { return function($message) use ($picea) { - return new HtmlResponse($picea->renderHtml('lean/error/401', [ + return Lean\Factory\HttpFactory::createHtmlResponse($picea->renderHtml('lean/error/401', [ 'title' => "", 'subtitle' => "", 'message' => $message, diff --git a/src/Event/RoutingCompileRoutes.php b/src/Event/RoutingCompileRoutes.php index 5eda957..1a8f0a1 100644 --- a/src/Event/RoutingCompileRoutes.php +++ b/src/Event/RoutingCompileRoutes.php @@ -6,5 +6,5 @@ use Lean\Routing; use Notes\Route\Attribute\Method\Route; interface RoutingCompileRoutes { - public function execute(Routing $routing, Route|\Notes\Route\Annotation\Method\Route $attribute) : void; + public function execute(Routing $routing, Route $attribute) : void; } diff --git a/src/Event/RoutingMapRoutes.php b/src/Event/RoutingMapRoutes.php index 292ff0e..e448574 100644 --- a/src/Event/RoutingMapRoutes.php +++ b/src/Event/RoutingMapRoutes.php @@ -8,5 +8,5 @@ use Psr\Container\ContainerInterface; use Psr\Http\Message\{ ResponseInterface, ServerRequestInterface }; interface RoutingMapRoutes { - public function execute(Routing $routing, ContainerInterface $container, ServerRequestInterface & $request, Route|\Notes\Route\Annotation\Method\Route $attribute) : void; + public function execute(Routing $routing, ContainerInterface $container, ServerRequestInterface & $request, Route $attribute) : void; } From 2cb911ea6f49e45ccb006356af01a6e9af2d6d9b Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Tue, 7 Nov 2023 15:02:19 -0500 Subject: [PATCH 3/7] - Fix for HttpFactory (more to come) --- src/Factory/HttpFactory.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Factory/HttpFactory.php b/src/Factory/HttpFactory.php index aeec5fa..6f32edc 100644 --- a/src/Factory/HttpFactory.php +++ b/src/Factory/HttpFactory.php @@ -33,9 +33,9 @@ class HttpFactory return new EmptyResponse($code, $headers); } - public static function createPdfResponse(string $binary, int $code = 200, array $headers = []) : ResponseInterface + public static function createPdfResponse(\Stringable $binary, int $code = 200, array $headers = []) : ResponseInterface { - return new PdfResponse($binary, $code, $headers); + return new PdfResponse((string) $binary, $code, $headers); } public static function createDownloadableResponse(string $data, string $filename, int $code = 200, array $headers = []) : ResponseInterface @@ -52,4 +52,4 @@ class HttpFactory { return new FileDownloadResponse($path, $code, $headers); } -} \ No newline at end of file +} From 2378245dbfb5e6361e72caee716e56a5db50e671 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Wed, 8 Nov 2023 06:52:17 -0500 Subject: [PATCH 4/7] - Removed authentication from controller, now handled by middleware --- meta/definitions/authorize.php | 2 +- meta/definitions/routes.php | 12 ++++++++++-- skeleton/meta/definitions/definitions.php | 10 ++++++---- skeleton/src/Lib/ControllerTrait.php | 15 ++++++--------- src/Application.php | 2 -- 5 files changed, 23 insertions(+), 18 deletions(-) diff --git a/meta/definitions/authorize.php b/meta/definitions/authorize.php index b5fbecd..a8a02c7 100644 --- a/meta/definitions/authorize.php +++ b/meta/definitions/authorize.php @@ -15,7 +15,7 @@ use Storage\{ Cookie, Session }; use function DI\{get, autowire, create}; return [ - Authenticate::class => create(Authenticate::class)->constructor(get(Session::class), get(Cookie::class), get('authentication.method')), + Authenticate::class => create(Authenticate::class)->constructor(get(Entity\UserInterface::class), get(Session::class), get(Cookie::class), get('authentication.method')), HeaderAuthenticationMiddleware::class => create(HeaderAuthenticationMiddleware::class)->constructor(get(Entity\UserInterface::class), get('authorize.error')), diff --git a/meta/definitions/routes.php b/meta/definitions/routes.php index ee3e7a0..8cae467 100644 --- a/meta/definitions/routes.php +++ b/meta/definitions/routes.php @@ -15,6 +15,11 @@ use Notes\Breadcrumb\Breadcrumb; use Storage\SessionMiddleware; +use Ulmus\User\Middleware\{ + HeaderAuthenticationMiddleware, + PostRequestAuthenticationMiddleware +}; + return [ Lean\Routing::class => autowire(Lean\Routing::class), @@ -44,14 +49,17 @@ return [ "dump", "errorHandler", SessionMiddleware::class, CronardMiddleware::class, Mcnd\Event\EventMiddleware::class, Mcnd\CLI\CliMiddleware::class, ], - 'app.middlewares' => [], + 'app.middlewares' => [ + HeaderAuthenticationMiddleware::class, + PostRequestAuthenticationMiddleware::class, + ], 'routes.list' => function($c) { return function (ContainerInterface $container) { $router = $container->get(Router::class); foreach([ 'routes.middlewares', 'app.middlewares' ] as $key) { - if ($container->has('app.middlewares')) { + if ( $container->has($key) ) { foreach ($container->get($key) as $i => $middleware) { if ($container->has($middleware)) { $router->middleware($container->get($middleware)); diff --git a/skeleton/meta/definitions/definitions.php b/skeleton/meta/definitions/definitions.php index 90ea4f2..ed85eec 100644 --- a/skeleton/meta/definitions/definitions.php +++ b/skeleton/meta/definitions/definitions.php @@ -47,9 +47,11 @@ return array_merge( require("$dir/security.php"), require("$dir/env/" . getenv('APP_ENV') . ".php"), [ - 'config' => function () { return array_replace( - require(getenv("META_PATH")."/config.php"), - Lean\Lean::autoloadConfigFromComposerExtra(), - ); } + 'config' => function () { + return array_merge_recursive( + Lean\Lean::autoloadConfigFromComposerExtra(), + require(getenv("META_PATH")."/config.php") + ); + } ] ); diff --git a/skeleton/src/Lib/ControllerTrait.php b/skeleton/src/Lib/ControllerTrait.php index 5fd2679..4c1a09a 100644 --- a/skeleton/src/Lib/ControllerTrait.php +++ b/skeleton/src/Lib/ControllerTrait.php @@ -5,36 +5,33 @@ namespace %NAMESPACE%\Lib; use Picea\Picea; use Storage\Session; use Ulmus\User\Entity\User; -use Ulmus\User\Lib\Authenticate; use Notes\Route\Attribute\Object\Route; use Notes\Security\Attribute\Security; use %NAMESPACE%\{ Entity, Lib, Form }; use Mcnd\Event\EventManager; +use Ulmus\User\Entity\UserInterface; #[Security(locked: false)] #[Route(method: ['GET', 'POST', ])] trait ControllerTrait { use \Lean\ControllerTrait; - public ? \Ulmus\User\Entity\User $user; - - protected Authenticate $authenticate; + public ? UserInterface $user; protected EventManager $eventManager; - public function __construct(Picea $picea, Session $session, Authenticate $authenticate, \Notes\Breadcrumb\Breadcrumb $breadcrumb, EventManager $eventManager) { - $this->initializeController($picea, $session, $authenticate, $breadcrumb, $eventManager); + public function __construct(Picea $picea, Session $session, UserInterface $user, \Notes\Breadcrumb\Breadcrumb $breadcrumb, EventManager $eventManager) { + $this->initializeController($picea, $session, $user, $breadcrumb, $eventManager); } - public function initializeController(Picea $picea, Session $session, Authenticate $authenticate, \Notes\Breadcrumb\Breadcrumb $breadcrumb, EventManager $eventManager) { + public function initializeController(Picea $picea, Session $session, UserInterface $user, \Notes\Breadcrumb\Breadcrumb $breadcrumb, EventManager $eventManager) { $this->picea = $picea; - $this->authenticate = $authenticate; $this->session = $session; $this->eventManager = $eventManager; $this->breadcrumb = $breadcrumb; - $this->user = $authenticate->rememberMe( Entity\User::repository() ) ?: new Entity\User(); + $this->user = $user; } } \ No newline at end of file diff --git a/src/Application.php b/src/Application.php index 1b0cec1..f242282 100644 --- a/src/Application.php +++ b/src/Application.php @@ -1,9 +1,7 @@ Date: Wed, 15 Nov 2023 19:10:30 -0500 Subject: [PATCH 5/7] - WIP on moving config and definition loading into composer --- composer.json | 26 +++++++++++++++++++++++++- meta/config.php | 14 ++++++++++++++ meta/definitions/software.php | 8 ++++++++ meta/definitions/storage.php | 19 +++++++++++++++++++ src/Lean.php | 9 ++++++++- 5 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 meta/config.php create mode 100644 meta/definitions/storage.php diff --git a/composer.json b/composer.json index 8316d52..645d93c 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "mcnd/lean", - "description": "A more-than-micro framework for basic apps", + "description": "A micro framework for rapid application development (RAD)", "type": "library", "license": "MIT", "authors": [ @@ -134,5 +134,29 @@ "post-update-cmd": [ "Lean\\Composer::postUpdate" ] + }, + "extra" : { + "lean" : { + "autoload": { + "definitions" : [ + "meta/definitions/software.php", + "meta/definitions/authorize.php", + "meta/definitions/cli.php", + "meta/definitions/cronard.php", + "meta/definitions/email.php", + "meta/definitions/event.php", + "meta/definitions/http.php", + "meta/definitions/language.php", + "meta/definitions/negundo.php", + "meta/definitions/routes.php", + "meta/definitions/software.php", + "meta/definitions/storage.php", + "meta/definitions/template.php", + ], + "config": [ + "meta/config.php" + ] + } + } } } diff --git a/meta/config.php b/meta/config.php new file mode 100644 index 0000000..d1e8b59 --- /dev/null +++ b/meta/config.php @@ -0,0 +1,14 @@ + [ + 'connections' => [ + 'sqlite' => [ + 'adapter' => 'SQLite', + 'path' => getenv('PROJECT_PATH') . DIRECTORY_SEPARATOR . "var/lean.sqlite3", + 'pragma_begin' => "journal_mode=WAL", + 'pragma_close' => "analysis_limit=500,optimize", + ], + ], + ], +]; \ No newline at end of file diff --git a/meta/definitions/software.php b/meta/definitions/software.php index 56bbc16..ebb1344 100644 --- a/meta/definitions/software.php +++ b/meta/definitions/software.php @@ -1,5 +1,6 @@ [], 'cronard' => [], + + 'taxus' => [ + [ new Privilege("dev", "Is a developper of this application."), "is_dev" ], + [ new Privilege("admin", "Can manage mostly everything from this application."), "is_admin" ], + [ new Privilege("user", "Is an authenticated user."), "is_user" ], + [ new Privilege("anonymous", "Is an anonymous (unauthenticated) user."), "is_anonymous" ], + ] ], Lean::class => autowire(Lean::class), diff --git a/meta/definitions/storage.php b/meta/definitions/storage.php new file mode 100644 index 0000000..329a014 --- /dev/null +++ b/meta/definitions/storage.php @@ -0,0 +1,19 @@ + function($c) { + $adapter = new ConnectionAdapter('sqlite', $c->get('config')['ulmus'], true); + $adapter->resolveConfiguration(); + + return $adapter; + } +]; diff --git a/src/Lean.php b/src/Lean.php index a2d62d8..eecad36 100644 --- a/src/Lean.php +++ b/src/Lean.php @@ -93,6 +93,11 @@ class Lean return array_merge(...array_map(fn($app) => $app->events ?? [], $this->applications)); } + public function getTaxusPrivileges() : array + { + return array_merge(...array_map(fn($app) => $app->taxus ?? [], $this->applications)); + } + public function getEntities() : array { return array_merge(...array_map(fn($app) => $app->entities ?? [], $this->applications)); @@ -152,6 +157,8 @@ class Lean public static function definitions() : array { + return []; + $path = dirname(__DIR__) . "/meta/definitions/"; return array_replace( @@ -164,8 +171,8 @@ class Lean require($path . "language.php"), class_exists(\Negundo\Client\NegundoMiddleware::class) ? require($path . "negundo.php") : [], require($path . "routes.php"), - # require($path . "security.php"), require($path . "software.php"), + require($path . "storage.php"), class_exists(\Picea\Picea::class) ? require($path . "template.php") : [], ); } From 4f648b47bd721c2011b59aa28847b190330b27d3 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Wed, 15 Nov 2023 19:13:41 -0500 Subject: [PATCH 6/7] - quickfix --- composer.json | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 645d93c..70aebd2 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,6 @@ "guzzlehttp/guzzle": "^7@dev", "swiftmailer/swiftmailer": "^6.2@dev", "mcnd/storage": "dev-master", - "mcnd/lean": "dev-master", "mcnd/lean-console": "dev-master", "mcnd/ulmus": "dev-master", "mcnd/picea": "dev-master", @@ -151,7 +150,7 @@ "meta/definitions/routes.php", "meta/definitions/software.php", "meta/definitions/storage.php", - "meta/definitions/template.php", + "meta/definitions/template.php" ], "config": [ "meta/config.php" @@ -159,4 +158,4 @@ } } } -} +} \ No newline at end of file From f3c4cbd95ac823f27a294d58f75b3a418c204ac3 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Wed, 15 Nov 2023 22:11:17 -0500 Subject: [PATCH 7/7] - WIP on leaning lean a bit --- composer.json | 3 -- meta/config.php | 2 +- meta/definitions/cli.php | 33 ------------ meta/definitions/cronard.php | 30 ----------- meta/definitions/language.php | 1 - meta/definitions/negundo.php | 12 ----- meta/definitions/software.php | 8 +-- meta/definitions/template.php | 61 ++--------------------- skeleton/meta/definitions/definitions.php | 12 +++-- skeleton/meta/definitions/security.php | 5 +- skeleton/meta/definitions/storage.php | 1 + src/Application.php | 35 +++++++++---- src/Lean.php | 18 +------ 13 files changed, 41 insertions(+), 180 deletions(-) delete mode 100644 meta/definitions/cli.php delete mode 100644 meta/definitions/cronard.php delete mode 100644 meta/definitions/negundo.php diff --git a/composer.json b/composer.json index 70aebd2..7c292d5 100644 --- a/composer.json +++ b/composer.json @@ -140,13 +140,10 @@ "definitions" : [ "meta/definitions/software.php", "meta/definitions/authorize.php", - "meta/definitions/cli.php", - "meta/definitions/cronard.php", "meta/definitions/email.php", "meta/definitions/event.php", "meta/definitions/http.php", "meta/definitions/language.php", - "meta/definitions/negundo.php", "meta/definitions/routes.php", "meta/definitions/software.php", "meta/definitions/storage.php", diff --git a/meta/config.php b/meta/config.php index d1e8b59..7e5ad72 100644 --- a/meta/config.php +++ b/meta/config.php @@ -3,7 +3,7 @@ return [ 'ulmus' => [ 'connections' => [ - 'sqlite' => [ + 'lean' => [ 'adapter' => 'SQLite', 'path' => getenv('PROJECT_PATH') . DIRECTORY_SEPARATOR . "var/lean.sqlite3", 'pragma_begin' => "journal_mode=WAL", diff --git a/meta/definitions/cli.php b/meta/definitions/cli.php deleted file mode 100644 index 903aedf..0000000 --- a/meta/definitions/cli.php +++ /dev/null @@ -1,33 +0,0 @@ - function($c) { - $fetcher = new CommandFetcher(null, null, $c->get('cli.caching')); - - $fetcher->setFolderList(array_map(function($item) { - return $item; - }, $c->get(Lean::class)->getCli())); - - return $fetcher; - }, - - CliMiddleware::class => function($c) { - return new CliMiddleware($c, $c->get('cli.response:default'), $c->get(CommandFetcher::class)); - }, - - 'cli.response:default' => function($c) { - return function() { - return Factory\HttpFactory::createTextResponse("This is the default response from CLI middleware which indicates that no command were registered for this application.\n"); - }; - }, -]; diff --git a/meta/definitions/cronard.php b/meta/definitions/cronard.php deleted file mode 100644 index ee1fc5c..0000000 --- a/meta/definitions/cronard.php +++ /dev/null @@ -1,30 +0,0 @@ - function($c) { - $cronardMiddleware = new CronardMiddleware($c, getenv('CRON_KEY'), function() : ResponseInterface { - return Factory\HttpFactory::createHtmlResponse(sprintf("%s - cron task begin...", date('Y-m-d H:i:s'))); - }, []); - - return $cronardMiddleware->fromFile(getenv("META_PATH")."/crontab.php")->fromAnnotations($c->get(TaskFetcher::class)); - }, - - TaskFetcher::class => function($c) { - $fetcher = new TaskFetcher(null, null, $c->get('cronard.caching')); - - $fetcher->setFolderList(array_map(function($item) { - return $item; - }, $c->get(Lean::class)->getCronard())); - - return $fetcher; - }, -]; diff --git a/meta/definitions/language.php b/meta/definitions/language.php index f72b077..2438eaa 100644 --- a/meta/definitions/language.php +++ b/meta/definitions/language.php @@ -19,7 +19,6 @@ return [ 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'), false); }, diff --git a/meta/definitions/negundo.php b/meta/definitions/negundo.php deleted file mode 100644 index 67fb3ee..0000000 --- a/meta/definitions/negundo.php +++ /dev/null @@ -1,12 +0,0 @@ - create(SoftwareConfig::class)->constructor(getenv('NEGUNDO_HASH'), getenv('NEGUNDO_SERVER')), - NegundoMiddleware::class => autowire(NegundoMiddleware::class), - Dump::class => autowire(Dump::class), - Task::class => autowire(Task::class), -]; diff --git a/meta/definitions/software.php b/meta/definitions/software.php index ebb1344..c6b8605 100644 --- a/meta/definitions/software.php +++ b/meta/definitions/software.php @@ -1,6 +1,5 @@ [], - 'taxus' => [ - [ new Privilege("dev", "Is a developper of this application."), "is_dev" ], - [ new Privilege("admin", "Can manage mostly everything from this application."), "is_admin" ], - [ new Privilege("user", "Is an authenticated user."), "is_user" ], - [ new Privilege("anonymous", "Is an anonymous (unauthenticated) user."), "is_anonymous" ], - ] + 'taxus' => [], ], Lean::class => autowire(Lean::class), diff --git a/meta/definitions/template.php b/meta/definitions/template.php index e799323..46a4d8b 100644 --- a/meta/definitions/template.php +++ b/meta/definitions/template.php @@ -2,68 +2,13 @@ use function DI\autowire, DI\create, DI\get; -use Picea\{ - Language\LanguageRegistration, - Caching\Cache, - Caching\Opcache, - Compiler, - Compiler\Context, - Compiler\BaseContext, - FileFetcher, - Method\Request}; +use Picea\{ Language\LanguageRegistration, }; -use Picea\Extension\{ LanguageHandlerInterface, LanguageExtension, TitleExtension, NumberExtension, UrlExtension }; +use Picea\Extension\{ LanguageHandlerInterface, }; -use Picea\Ui\{ Method, Ui }; +use Picea\Ui\{ Ui }; return [ - Picea\Picea::class => function($c) { - return new Picea\Picea($c->get(Context::class), $c->get(Cache::class), $c->get(Compiler::class), null, $c->get(FileFetcher::class), null, getenv("DEBUG")); - }, - - Context::class => function($c) { - return new BaseContext($c->get(Lean\Lean::class)->getPiceaContext()); - }, - - Ui::class => autowire(Ui::class), - - Compiler::class => autowire(Compiler::class), - - Request::class => autowire(Request::class), - - Method\Form::class => autowire(Method\Form::class), - - Method\Pagination::class => autowire(Method\Pagination::class), - - LanguageExtension::class => create(LanguageExtension::class)->constructor(get(LanguageHandlerInterface::class)), - LanguageHandlerInterface::class => autowire(\Lean\LanguageHandler::class), - LanguageRegistration::class => create(\Lean\PiceaDefaultRegistration::class)->constructor(get('picea.extensions'), [], [], get(Ui::class), null), - - 'picea.extensions' => function(\Psr\Container\ContainerInterface $c) { - return array_merge([ - $c->get(LanguageExtension::class), - $c->get(TitleExtension::class), - $c->get(NumberExtension::class), - $c->get(UrlExtension::class), - $c->get(Method\Form::class), - $c->get(Method\Pagination::class), - $c->get(Request::class), - ], class_exists(\Taxus\Picea\Extension::class) ? [ $c->get(\Taxus\Picea\Extension::class) ] : [], - array_map(fn($class) => $c->get($class), $c->get(Lean\Lean::class)->getPiceaExtensions()) - ); - }, - - TitleExtension::class => autowire(TitleExtension::class), - - NumberExtension::class => autowire(NumberExtension::class), - - UrlExtension::class => create(UrlExtension::class)->constructor(getenv("URL_BASE"), get('git.commit'), explode(',', getenv('APP_URL')), (bool) getenv('FORCE_SSL')), - - Cache::class => create(Opcache::class)->constructor(getenv("CACHE_PATH"), get(Context::class)), - - FileFetcher::class => function($c) { - return new FileFetcher($c->get(Lean\Lean::class)->getViewPaths()); - }, ]; \ No newline at end of file diff --git a/skeleton/meta/definitions/definitions.php b/skeleton/meta/definitions/definitions.php index ed85eec..c237b22 100644 --- a/skeleton/meta/definitions/definitions.php +++ b/skeleton/meta/definitions/definitions.php @@ -1,12 +1,12 @@ "%ESCAPED_NAMESPACE%\\View", 'asset' => [], 'extensions' => [], - ], 'ulmus' => [ @@ -37,6 +36,13 @@ return array_merge( 'cli' => [ '%ESCAPED_NAMESPACE%\\Controller' => implode(DIRECTORY_SEPARATOR, [ getenv("PROJECT_PATH"), 'src', 'Controller', '' ]), ], + + 'taxus' => [ + [ new Privilege("dev", "Is a developper of this application."), "is_dev" ], + [ new Privilege("admin", "Can manage mostly everything from this application."), "is_admin" ], + [ new Privilege("user", "Is an authenticated user."), "is_user" ], + [ new Privilege("anonymous", "Is an anonymous (unauthenticated) user."), "is_anonymous" ], + ] ], 'app.middlewares' => [], diff --git a/skeleton/meta/definitions/security.php b/skeleton/meta/definitions/security.php index 7647b7f..531fff1 100644 --- a/skeleton/meta/definitions/security.php +++ b/skeleton/meta/definitions/security.php @@ -11,10 +11,7 @@ use function DI\autowire, DI\create, DI\get; return [ Taxus::class => function ($c) { return ( new Taxus( $c->get(PermissionGrantInterface::class) ) )->add( - [ new Privilege("dev", "Is a developper of this application."), "is_dev" ], - [ new Privilege("admin", "Can manage mostly everything from this application."), "is_admin" ], - [ new Privilege("user", "Is an authenticated user."), "is_user" ], - [ new Privilege("anonymous", "Is an anonymous (unauthenticated) user."), "is_anonymous" ], + $c->get(Lean\Lean::class)->getTaxusPrivileges() ); }, diff --git a/skeleton/meta/definitions/storage.php b/skeleton/meta/definitions/storage.php index 8b485dc..b0336d4 100644 --- a/skeleton/meta/definitions/storage.php +++ b/skeleton/meta/definitions/storage.php @@ -19,6 +19,7 @@ return [ AdapterProxy::class => function (ContainerInterface $c) { return new AdapterProxy( + $c->get('lean:adapter.sqlite'), $c->get(ConnectionAdapter::class) ); }, diff --git a/src/Application.php b/src/Application.php index f242282..424c727 100644 --- a/src/Application.php +++ b/src/Application.php @@ -28,6 +28,8 @@ class Application public array $tellPhp; + public array $taxus; + public array $data = []; public function __construct(string $name) { @@ -38,7 +40,14 @@ class Application { $this->data = array_replace($this->data, $data); - if (is_array($picea = $data['picea'] ?? false)) { + $this->loadData(); + + return $this; + } + + public function loadData() : static + { + if (is_array($picea = $this->data['picea'] ?? false)) { if ($picea['context'] ?? false ) { $this->piceaContext = $picea['context']; } @@ -56,13 +65,13 @@ class Application } } - if (is_array($ulmus = $data['ulmus'] ?? false)) { + if (is_array($ulmus = $this->data['ulmus'] ?? false)) { if ($ulmus['entities'] ?? false) { $this->entities = $ulmus['entities']; } } - if (is_array($tell = $data['tell'] ?? false)) { + if (is_array($tell = $this->data['tell'] ?? false)) { if ($tell['json'] ?? false) { $this->tellJson = $tell['json']; } @@ -72,20 +81,24 @@ class Application } } - if (is_array($data['routes'] ?? false)) { - $this->routes = $data['routes']; + if (is_array($this->data['routes'] ?? false)) { + $this->routes = $this->data['routes']; } - if (is_array($data['cronard'] ?? false)) { - $this->cronard = $data['cronard']; + if (is_array($this->data['cronard'] ?? false)) { + $this->cronard = $this->data['cronard']; } - if (is_array($data['events'] ?? false)) { - $this->events = $data['events']; + if (is_array($this->data['events'] ?? false)) { + $this->events = $this->data['events']; } - if (is_array($data['cli'] ?? false)) { - $this->cli = $data['cli']; + if (is_array($this->data['cli'] ?? false)) { + $this->cli = $this->data['cli']; + } + + if (is_array($this->data['taxus'] ?? false)) { + $this->taxus = $this->data['taxus']; } return $this; diff --git a/src/Lean.php b/src/Lean.php index eecad36..0a01724 100644 --- a/src/Lean.php +++ b/src/Lean.php @@ -155,26 +155,10 @@ class Lean return null; } + #[\Deprecated("Definitions are now loaded automatically from composer extra array.")] public static function definitions() : array { return []; - - $path = dirname(__DIR__) . "/meta/definitions/"; - - return array_replace( - require($path . "authorize.php"), - class_exists(\Mcnd\CLI\CliMiddleware::class) ? require($path . "cli.php") : [], - class_exists(\Cronard\CronardMiddleware::class) ? require($path . "cronard.php") : [], - require($path . "email.php"), - require($path . "event.php"), - require($path . "http.php"), - require($path . "language.php"), - class_exists(\Negundo\Client\NegundoMiddleware::class) ? require($path . "negundo.php") : [], - require($path . "routes.php"), - require($path . "software.php"), - require($path . "storage.php"), - class_exists(\Picea\Picea::class) ? require($path . "template.php") : [], - ); } public static function autoloadDefinitionsFromComposerExtra() : array