From 41083c767a53bf30b673342fc13f9c8ac43c40ee Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Sun, 9 Jul 2023 12:37:39 -0400 Subject: [PATCH] - Working on CLI modules --- meta/definitions/cli.php | 51 +++++++++++++++++++++++ meta/definitions/routes.php | 3 +- meta/definitions/software.php | 1 + skeleton/meta/definitions/definitions.php | 4 ++ skeleton/meta/definitions/env/dev.php | 3 +- skeleton/meta/definitions/env/prod.php | 1 + skeleton/src/PrivilegeGrantAccess.php | 2 +- src/Application.php | 7 ++++ src/ControllerTrait.php | 16 +++++++ src/Lean.php | 6 +++ 10 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 meta/definitions/cli.php diff --git a/meta/definitions/cli.php b/meta/definitions/cli.php new file mode 100644 index 0000000..98a42b2 --- /dev/null +++ b/meta/definitions/cli.php @@ -0,0 +1,51 @@ + 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)); + }, + + 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; + },*/ + + CommandFetcher::class => 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 new \Laminas\Diactoros\Response\TextResponse("This is the default response from CLI middleware which indicates that no command were registered for this application.\n"); + }; + }, +]; diff --git a/meta/definitions/routes.php b/meta/definitions/routes.php index 07585c8..d564003 100644 --- a/meta/definitions/routes.php +++ b/meta/definitions/routes.php @@ -81,7 +81,7 @@ return [ }; }, - 'routes.middlewares' => [ "dump", "errorHandler", SessionMiddleware::class, CronardMiddleware::class, Mcnd\Event\EventMiddleware::class, HttpBasicAuthentication::class ], + 'routes.middlewares' => [ "dump", "errorHandler", SessionMiddleware::class, CronardMiddleware::class, Mcnd\Event\EventMiddleware::class, Mcnd\CLI\CliMiddleware::class, ], 'routes.list' => function($c) { return function (ContainerInterface $container) { @@ -94,7 +94,6 @@ return [ } $routing = $container->get(Lean\Routing::class); - $routing->registerRoute($container, getenv('URL_BASE')); return $router; diff --git a/meta/definitions/software.php b/meta/definitions/software.php index 3a5c08e..3d9a3b8 100644 --- a/meta/definitions/software.php +++ b/meta/definitions/software.php @@ -6,6 +6,7 @@ use Laminas\Diactoros\Response\HtmlResponse; use TheBugs\JavascriptMiddleware; + use Cronard\CronardMiddleware, Notes\Cronard\TaskFetcher; diff --git a/skeleton/meta/definitions/definitions.php b/skeleton/meta/definitions/definitions.php index 75be0fb..2d90588 100644 --- a/skeleton/meta/definitions/definitions.php +++ b/skeleton/meta/definitions/definitions.php @@ -34,6 +34,10 @@ return array_merge( 'cronard' => [ '%ESCAPED_NAMESPACE%\\Controller' => implode(DIRECTORY_SEPARATOR, [ getenv("PROJECT_PATH"), 'src', 'Controller', '' ]), ], + + 'cli' => [ + '%ESCAPED_NAMESPACE%\\Controller' => implode(DIRECTORY_SEPARATOR, [ getenv("PROJECT_PATH"), 'src', 'Controller', '' ]), + ], ], ], diff --git a/skeleton/meta/definitions/env/dev.php b/skeleton/meta/definitions/env/dev.php index e49d6b2..7d5d493 100644 --- a/skeleton/meta/definitions/env/dev.php +++ b/skeleton/meta/definitions/env/dev.php @@ -15,5 +15,6 @@ return [ 'breadcrumbs.caching' => create(Kash\ArrayCache::class)->constructor( get(Kash\CacheInvalidator::class), "lean.breadcrumbs", 30), 'ulmus.caching' => create(Kash\ArrayCache::class)->constructor( get(Kash\CacheInvalidator::class), "ulmus.entities", 30), 'cronard.caching' => create(Kash\ArrayCache::class)->constructor( get(Kash\CacheInvalidator::class), "lean.cronards", 30), - 'events.caching' => create(Kash\ArrayCache::class)->constructor( get(Kash\CacheInvalidator::class), "lean.events", random_int(3600, 7200)), + 'events.caching' => create(Kash\ArrayCache::class)->constructor( get(Kash\CacheInvalidator::class), "lean.events", 30), + 'cli.caching' => create(Kash\ArrayCache::class)->constructor( get(Kash\CacheInvalidator::class), "lean.cli", 30), ]; diff --git a/skeleton/meta/definitions/env/prod.php b/skeleton/meta/definitions/env/prod.php index 9bf09af..dc8ad97 100644 --- a/skeleton/meta/definitions/env/prod.php +++ b/skeleton/meta/definitions/env/prod.php @@ -49,4 +49,5 @@ return [ 'ulmus.caching' => create(Kash\ApcuCache::class)->constructor( get(Kash\CacheInvalidator::class), "ulmus.entities", random_int(3600, 7200)), 'cronard.caching' => create(Kash\ApcuCache::class)->constructor( get(Kash\CacheInvalidator::class), "lean.cronards", random_int(3600, 7200)), 'events.caching' => create(Kash\ApcuCache::class)->constructor( get(Kash\CacheInvalidator::class), "lean.events", random_int(3600, 7200)), + 'cli.caching' => create(Kash\ApcuCache::class)->constructor( get(Kash\CacheInvalidator::class), "lean.cli", random_int(3600, 7200)), ]; \ No newline at end of file diff --git a/skeleton/src/PrivilegeGrantAccess.php b/skeleton/src/PrivilegeGrantAccess.php index 745304e..db963d0 100644 --- a/skeleton/src/PrivilegeGrantAccess.php +++ b/skeleton/src/PrivilegeGrantAccess.php @@ -25,7 +25,7 @@ class PrivilegeGrantAccess implements PermissionGrantInterface { */ public function is_dev() : bool { - return false; + return getenv('DEBUG'); } public function is_admin(User $user) : bool diff --git a/src/Application.php b/src/Application.php index c027ecf..2fdf4f6 100644 --- a/src/Application.php +++ b/src/Application.php @@ -16,6 +16,8 @@ class Application public array $routes; + public array $cli; + public array $cronard; public array $entities; @@ -73,10 +75,15 @@ class Application if (is_array($data['cronard'] ?? false)) { $this->cronard = $data['cronard']; } + if (is_array($data['events'] ?? false)) { $this->events = $data['events']; } + if (is_array($data['cli'] ?? false)) { + $this->cli = $data['cli']; + } + return $this; } } \ No newline at end of file diff --git a/src/ControllerTrait.php b/src/ControllerTrait.php index 9f866c6..f9179b1 100644 --- a/src/ControllerTrait.php +++ b/src/ControllerTrait.php @@ -117,6 +117,22 @@ trait ControllerTrait { return new FileDownloadResponse($path, $code, $headers); } + public function renderCLI(ServerRequestInterface $request, mixed $data) : ResponseInterface + { + if ($data instanceof \JsonSerializable ) { + return $this->renderJson( + $data + ); + } + elseif ( is_array($data) ) { + var_export($data); + } + + return $this->renderText( + $data . PHP_EOL + ); + } + public function fromResponse(ResponseInterface $response) { if ( $response->getStatusCode() === 200 ) { diff --git a/src/Lean.php b/src/Lean.php index 84705d6..1f6a330 100644 --- a/src/Lean.php +++ b/src/Lean.php @@ -83,6 +83,11 @@ class Lean return array_merge(...array_map(fn($app) => $app->cronard ?? [], $this->applications)); } + public function getCLI() : array + { + return array_merge(...array_map(fn($app) => $app->cli ?? [], $this->applications)); + } + public function getEvents() : array { return array_merge(...array_map(fn($app) => $app->events ?? [], $this->applications)); @@ -128,6 +133,7 @@ class Lean $path = dirname(__DIR__) . "/meta/definitions/"; return array_merge( + require($path . "cli.php"), require($path . "cronard.php"), require($path . "email.php"), require($path . "event.php"),