From b6d7e5fad50de9b685c94846e304462eb9057a18 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Fri, 6 Oct 2023 19:09:45 -0400 Subject: [PATCH 01/13] - WIP on picea-asset --- meta/definitions/template.php | 6 ++++++ skeleton/lean | 9 +++++++++ src/ControllerTrait.php | 6 +++++- 3 files changed, 20 insertions(+), 1 deletion(-) create mode 100755 skeleton/lean diff --git a/meta/definitions/template.php b/meta/definitions/template.php index 95d3d81..ea13690 100644 --- a/meta/definitions/template.php +++ b/meta/definitions/template.php @@ -7,6 +7,7 @@ use Laminas\Diactoros\Response\HtmlResponse; use Picea\{ Picea, Caching\Cache, Caching\Opcache, Compiler, Compiler\Context, Compiler\BaseContext, FileFetcher, Language\DefaultRegistrations, Method\Request }; use Picea\Extension\{ LanguageHandler, LanguageExtension, TitleExtension, NumberExtension, UrlExtension }; use Picea\Ui\{ Method, Ui }; +use Picea\Asset\Asset; return [ Picea::class => function($c) { @@ -33,6 +34,11 @@ return [ { parent::registerAll($compiler); ( new Ui() )->registerFormExtension($compiler); + + if ( class_exists('Picea\\Asset\\Asset') ) { + ( new Asset() )->registerExtension($compiler); + + } } }); diff --git a/skeleton/lean b/skeleton/lean new file mode 100755 index 0000000..c1e8d30 --- /dev/null +++ b/skeleton/lean @@ -0,0 +1,9 @@ +#!/bin/php +getMessage(), var_export($t->getTrace(), true)); +} \ No newline at end of file diff --git a/src/ControllerTrait.php b/src/ControllerTrait.php index f9179b1..21e3a77 100644 --- a/src/ControllerTrait.php +++ b/src/ControllerTrait.php @@ -51,7 +51,11 @@ trait ControllerTrait { public function renderRawView(string $view, ?array $variables = null) : string { - return $this->picea->renderHtml($view, $variables ?? [], $this); + 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`."); + } + + return $content; } public function renderView(string $view, ?array $variables = null) : ResponseInterface From 4309f46c866a866484f812f9a390045670da1c5a Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Mon, 9 Oct 2023 14:02:08 -0400 Subject: [PATCH 02/13] - Migrating to Guzzle 7x --- composer.json | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/composer.json b/composer.json index 1763a42..7fd0d08 100644 --- a/composer.json +++ b/composer.json @@ -28,9 +28,14 @@ "mcnd/lean": "dev-master", "mcnd/lean-console": "dev-master", "mcnd/ulmus": "dev-master", + "mcnd/picea": "dev-master", + "mcnd/picea-ui": "dev-master", + "mcnd/cronard": "dev-master", "mcnd/tell": "dev-master", "mcnd/dump": "dev-master", "mcnd/event": "dev-master", + "mcnd/notes-breadcrumb": "dev-master", + "mcnd/notes-cronard": "dev-master", "mcnd/notes-event": "dev-master", "mcnd/notes-tell": "dev-master", "mcnd/notes-route": "dev-master", @@ -45,6 +50,10 @@ "type": "vcs", "url": "https://git.mcnd.ca/mcndave/lean-console.git" }, + { + "type": "vcs", + "url": "https://git.mcnd.ca/mcndave/cronard.git" + }, { "type": "vcs", "url": "https://git.mcnd.ca/mcndave/event.git" @@ -65,6 +74,14 @@ "type": "vcs", "url": "https://git.mcnd.ca/mcndave/ulmus-user.git" }, + { + "type": "vcs", + "url": "https://git.mcnd.ca/mcndave/picea.git" + }, + { + "type": "vcs", + "url": "https://git.mcnd.ca/mcndave/picea-ui.git" + }, { "type": "vcs", "url": "https://git.mcnd.ca/mcndave/notes.git" @@ -73,10 +90,18 @@ "type": "vcs", "url": "https://git.mcnd.ca/mcndave/notes-route.git" }, + { + "type": "vcs", + "url": "https://git.mcnd.ca/mcndave/notes-cronard.git" + }, { "type": "vcs", "url": "https://git.mcnd.ca/mcndave/notes-security.git" }, + { + "type": "vcs", + "url": "https://git.mcnd.ca/mcndave/notes-breadcrumb.git" + }, { "type": "vcs", "url": "https://git.mcnd.ca/mcndave/notes-event.git" From 84b3a16b6e49cbf42379acc0af7dba1feba4f6e7 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Mon, 9 Oct 2023 14:05:55 -0400 Subject: [PATCH 03/13] - Prepping for Guzzle 7 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 7fd0d08..62986bb 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "vlucas/phpdotenv": "^3.4@dev", "middlewares/whoops": "dev-master", "ralouphie/getallheaders": "dev-master", - "guzzlehttp/guzzle": "^6.3@dev", + "guzzlehttp/guzzle": "^7@dev", "swiftmailer/swiftmailer": "^6.2@dev", "mcnd/storage": "dev-master", "mcnd/lean": "dev-master", From 438b5a69c031091a93f71407d050540aeb5423cb Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Mon, 9 Oct 2023 14:19:54 -0400 Subject: [PATCH 04/13] - Prepping for Guzzle 7 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 62986bb..bba2ab6 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,7 @@ "laminas/laminas-httphandlerrunner": "2.5.x-dev", "vlucas/phpdotenv": "^3.4@dev", "middlewares/whoops": "dev-master", - "ralouphie/getallheaders": "dev-master", + "ralouphie/getallheaders": "^3@dev", "guzzlehttp/guzzle": "^7@dev", "swiftmailer/swiftmailer": "^6.2@dev", "mcnd/storage": "dev-master", From 8d22132ef515a0b6efc1ee0bc1d5736f9925cd9c Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Thu, 12 Oct 2023 12:52:34 -0400 Subject: [PATCH 05/13] - WIP on picea asset --- meta/definitions/template.php | 11 ++++++----- src/Application.php | 6 ++++++ src/Lean.php | 13 +++++++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/meta/definitions/template.php b/meta/definitions/template.php index ea13690..ba0961f 100644 --- a/meta/definitions/template.php +++ b/meta/definitions/template.php @@ -15,7 +15,7 @@ return [ }, Context::class => function($c) { - return new BaseContext( $c->get(Lean\Lean::class)->getPiceaContext() ); + return new BaseContext($c->get(Lean\Lean::class)->getPiceaContext()); }, Compiler::class => function($c) { @@ -33,14 +33,15 @@ return [ public function registerAll(Compiler $compiler) : void { parent::registerAll($compiler); - ( new Ui() )->registerFormExtension($compiler); - if ( class_exists('Picea\\Asset\\Asset') ) { + if (class_exists(Ui::class)) { + ( new Ui() )->registerFormExtension($compiler); + } + + if (class_exists(Asset::class)) { ( new Asset() )->registerExtension($compiler); - } } - }); }, diff --git a/src/Application.php b/src/Application.php index 2fdf4f6..1b0cec1 100644 --- a/src/Application.php +++ b/src/Application.php @@ -12,6 +12,8 @@ class Application public array $piceaExtensions; + public array $piceaAssets; + public array $views; public array $routes; @@ -47,6 +49,10 @@ class Application $this->piceaExtensions = $picea['extensions']; } + if ($picea['asset'] ?? false ) { + $this->piceaAssets = $picea['asset']; + } + if ($picea['view'] ?? false) { $this->views = $picea['view']; } diff --git a/src/Lean.php b/src/Lean.php index 7e3dc77..e0cdfef 100644 --- a/src/Lean.php +++ b/src/Lean.php @@ -73,6 +73,19 @@ class Lean return $list; } + public function getPiceaAssets() : array + { + $list = []; + + foreach(array_reverse($this->applications) as $apps) { + if ( $apps->piceaAssets ?? null ) { + $list = array_merge_recursive($list, $apps->piceaAssets); + } + } +# dump($list); + return $list; + } + public function getRoutable() : array { return array_merge(...array_map(fn($app) => $app->routes ?? [], $this->applications)); From 3f92287829ff5c30b3847d70fbfedf9ae257515d Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Thu, 12 Oct 2023 18:50:17 +0000 Subject: [PATCH 06/13] - Merging codes --- meta/definitions/software.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/meta/definitions/software.php b/meta/definitions/software.php index 3d9a3b8..02c7383 100644 --- a/meta/definitions/software.php +++ b/meta/definitions/software.php @@ -83,11 +83,11 @@ return [ $gitdir = getenv("PROJECT_PATH") . DIRECTORY_SEPARATOR . ".git" . DIRECTORY_SEPARATOR; if ( file_exists($gitdir . "HEAD") ) { - if (false !== ($currentBranch = file_get_contents($gitdir . "HEAD"))) { + if (false !== ($currentBranch = file_get_contents($gitdir . "HEAD"))) { $file = explode(": ", $currentBranch)[1]; $path = $gitdir . str_replace("/", DIRECTORY_SEPARATOR, trim($file, " \t\n\r")); - return trim(file_get_contents($path), " \t\n\r"); + return file_exists($path) ? trim(file_get_contents($path), " \t\n\r") : null; } } From 4dcc7f407e99ae2928f63593292e85315e89fae3 Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Mon, 16 Oct 2023 13:58:51 -0400 Subject: [PATCH 07/13] - WIP on picea-asset AND fixing ApplicationStrategy allowing PHP-DI caching --- meta/definitions/routes.php | 30 +------------------- meta/definitions/template.php | 17 ++++++++++-- src/ApplicationStrategy.php | 52 +++++++++++++++++++++++++++++++++++ src/Kernel.php | 4 +-- 4 files changed, 70 insertions(+), 33 deletions(-) create mode 100644 src/ApplicationStrategy.php diff --git a/meta/definitions/routes.php b/meta/definitions/routes.php index d564003..12540f2 100644 --- a/meta/definitions/routes.php +++ b/meta/definitions/routes.php @@ -51,35 +51,7 @@ return [ return $breadcrumb; }, - ApplicationStrategy::class => function($c) { - return new class($c->get(Picea\Picea::class)) extends ApplicationStrategy { - - public Picea\Picea $picea; - - public function __construct(Picea\Picea $picea) { - $this->picea = $picea; - } - - public function getNotFoundDecorator(NotFoundException $exception): MiddlewareInterface - { - return new class($this->picea) implements MiddlewareInterface { - - protected Picea\Picea $picea; - - public function __construct(Picea\Picea $picea) { - $this->picea = $picea; - } - - public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface - { - return new Laminas\Diactoros\Response\HtmlResponse($this->picea->renderHtml("lean/error/404", [], $this), 404); - } - - }; - } - - }; - }, + ApplicationStrategy::class => autowire(\Lean\ApplicationStrategy::class), 'routes.middlewares' => [ "dump", "errorHandler", SessionMiddleware::class, CronardMiddleware::class, Mcnd\Event\EventMiddleware::class, Mcnd\CLI\CliMiddleware::class, ], diff --git a/meta/definitions/template.php b/meta/definitions/template.php index ba0961f..c4be788 100644 --- a/meta/definitions/template.php +++ b/meta/definitions/template.php @@ -4,10 +4,19 @@ use function DI\autowire, DI\create, DI\get; use Laminas\Diactoros\Response\HtmlResponse; -use Picea\{ Picea, Caching\Cache, Caching\Opcache, Compiler, Compiler\Context, Compiler\BaseContext, FileFetcher, Language\DefaultRegistrations, Method\Request }; +use Picea\{Asset\Action\InstallActionInterface, + Picea, + Caching\Cache, + Caching\Opcache, + Compiler, + Compiler\Context, + Compiler\BaseContext, + FileFetcher, + Language\DefaultRegistrations, + Method\Request}; use Picea\Extension\{ LanguageHandler, LanguageExtension, TitleExtension, NumberExtension, UrlExtension }; use Picea\Ui\{ Method, Ui }; -use Picea\Asset\Asset; +use Picea\Asset\{ Asset, Action }; return [ Picea::class => function($c) { @@ -79,4 +88,8 @@ return [ FileFetcher::class => function($c) { return new FileFetcher($c->get(Lean\Lean::class)->getViewPaths()); }, + + Action\Install::class => autowire(Action\Install::class), + Action\Symlink::class => autowire(Action\Symlink::class), + Action\InstallActionInterface::class => create(Action\Symlink::class), ]; \ No newline at end of file diff --git a/src/ApplicationStrategy.php b/src/ApplicationStrategy.php new file mode 100644 index 0000000..f92ef64 --- /dev/null +++ b/src/ApplicationStrategy.php @@ -0,0 +1,52 @@ +picea) implements MiddlewareInterface { + + protected Picea $picea; + + public function __construct(Picea $picea) + { + $this->picea = $picea; + } + + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + return $this->throw404($request); + } + + public function throw404(ServerRequestInterface $request) : ResponseInterface + { + if ( class_exists(\Picea\Asset\Asset::class) ) { + + } + + return new \Laminas\Diactoros\Response\HtmlResponse($this->picea->renderHtml("lean/error/404", [], $this), 404); + } + }; + } +} \ No newline at end of file diff --git a/src/Kernel.php b/src/Kernel.php index d99ebe0..492e6e2 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -110,8 +110,8 @@ class Kernel { if (getenv("APP_ENV") === "prod") { if (getenv("CACHE_PATH")) { - # $containerBuilder->enableCompilation(getenv("CACHE_PATH") . "/di/"); - # $containerBuilder->writeProxiesToFile(true); + $containerBuilder->enableCompilation(getenv("CACHE_PATH") . "/di/"); + $containerBuilder->writeProxiesToFile(true, getenv("CACHE_PATH") . "/di/proxies/"); } } From 4c0cddd6176d43c7d369f556cd5fb5d2027065bd Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Tue, 17 Oct 2023 20:10:42 -0400 Subject: [PATCH 08/13] - Done fixing Container build caching and WIP on Picea's asset --- meta/definitions/software.php | 15 ++++- meta/definitions/template.php | 80 ++++++++++------------- skeleton/meta/definitions/auth.php | 2 + skeleton/meta/definitions/definitions.php | 5 +- src/ApplicationStrategy.php | 30 ++++++--- src/Composer.php | 27 ++++++-- src/Kernel.php | 4 +- src/LanguageHandler.php | 18 +++++ src/Lean.php | 78 ++++++++++++++++++---- src/PiceaDefaultRegistration.php | 28 ++++++++ 10 files changed, 208 insertions(+), 79 deletions(-) create mode 100644 src/LanguageHandler.php create mode 100644 src/PiceaDefaultRegistration.php diff --git a/meta/definitions/software.php b/meta/definitions/software.php index 02c7383..c8c4eb9 100644 --- a/meta/definitions/software.php +++ b/meta/definitions/software.php @@ -18,6 +18,8 @@ use Storage\Cookie, Storage\Session, Storage\SessionMiddleware; +$dir = dirname(__DIR__, 2); + return [ 'lean.default' => [ 'picea' => [ @@ -29,11 +31,22 @@ return [ 'order' => 10, ], [ - 'path' => getenv("PROJECT_PATH") . implode(DIRECTORY_SEPARATOR, [ "", "vendor", "mcnd" , "lean" , "view" ]), + 'path' => implode(DIRECTORY_SEPARATOR, [ $dir, "view", '' ]), 'order' => 99, ], ], + 'asset' => [ + "public_path" => getenv("PUBLIC_PATH"), + 'destination_path' => "static", + 'source' => [ + [ + 'path' => implode(DIRECTORY_SEPARATOR, [ getenv("PROJECT_PATH"), "asset", '' ]), + 'order' => 10 + ] + ], + ], + 'extensions' => [], ], diff --git a/meta/definitions/template.php b/meta/definitions/template.php index c4be788..6ed312c 100644 --- a/meta/definitions/template.php +++ b/meta/definitions/template.php @@ -5,7 +5,7 @@ use function DI\autowire, DI\create, DI\get; use Laminas\Diactoros\Response\HtmlResponse; use Picea\{Asset\Action\InstallActionInterface, - Picea, + Language\LanguageRegistration, Caching\Cache, Caching\Opcache, Compiler, @@ -14,21 +14,39 @@ use Picea\{Asset\Action\InstallActionInterface, FileFetcher, Language\DefaultRegistrations, Method\Request}; -use Picea\Extension\{ LanguageHandler, LanguageExtension, TitleExtension, NumberExtension, UrlExtension }; +use Picea\Extension\{ LanguageHandlerInterface, LanguageExtension, TitleExtension, NumberExtension, UrlExtension }; use Picea\Ui\{ Method, Ui }; use Picea\Asset\{ Asset, Action }; return [ - Picea::class => function($c) { - return new Picea($c->get(Context::class), $c->get(Cache::class), $c->get(Compiler::class), null, $c->get(FileFetcher::class), null, getenv("DEBUG")); + 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()); }, - Compiler::class => function($c) { - return new Compiler(new class(array_merge([ + Ui::class => autowire(Ui::class), + + Asset::class => autowire(Asset::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), get(Asset::class)), + + 'picea.extensions' => function(\Psr\Container\ContainerInterface $c) { + return array_merge([ $c->get(LanguageExtension::class), $c->get(TitleExtension::class), $c->get(NumberExtension::class), @@ -37,44 +55,8 @@ return [ $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() ))) extends DefaultRegistrations { - - public function registerAll(Compiler $compiler) : void - { - parent::registerAll($compiler); - - if (class_exists(Ui::class)) { - ( new Ui() )->registerFormExtension($compiler); - } - - if (class_exists(Asset::class)) { - ( new Asset() )->registerExtension($compiler); - } - } - }); - }, - - 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(LanguageHandler::class)), - - LanguageHandler::class => function($c) { - return new class( $c->get(Tell\I18n::class) ) implements LanguageHandler { - public Tell\I18n $tell; - - public function __construct(Tell\I18n $tell) { - $this->tell = $tell; - } - - public function languageFromKey(string $key, array $variables = []) #: array|string - { - return $this->tell->fromKey($key, $variables) ?: ""; - } - }; + array_map(fn($class) => $c->get($class), $c->get(Lean\Lean::class)->getPiceaExtensions()) + ); }, TitleExtension::class => autowire(TitleExtension::class), @@ -89,7 +71,13 @@ return [ return new FileFetcher($c->get(Lean\Lean::class)->getViewPaths()); }, - Action\Install::class => autowire(Action\Install::class), + Picea\Asset\FileFetcher::class => function($c) { + return new Picea\Asset\FileFetcher($c->get(Lean\Lean::class)->getAssetPaths()); + }, + + Action\Install::class => autowire(Action\Install::class)->constructor(get(Action\InstallActionInterface::class), []), + Action\Symlink::class => autowire(Action\Symlink::class), - Action\InstallActionInterface::class => create(Action\Symlink::class), + + Action\InstallActionInterface::class => autowire(Action\Symlink::class), ]; \ No newline at end of file diff --git a/skeleton/meta/definitions/auth.php b/skeleton/meta/definitions/auth.php index 7e37832..56e9f64 100644 --- a/skeleton/meta/definitions/auth.php +++ b/skeleton/meta/definitions/auth.php @@ -30,6 +30,8 @@ return [ return new RedirectResponse(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', [ diff --git a/skeleton/meta/definitions/definitions.php b/skeleton/meta/definitions/definitions.php index 2d90588..b909892 100644 --- a/skeleton/meta/definitions/definitions.php +++ b/skeleton/meta/definitions/definitions.php @@ -13,9 +13,8 @@ return array_merge( '%APPKEY%' => [ 'picea' => [ 'context' => "%ESCAPED_NAMESPACE%\\View", - - 'extensions' => [ - ], + 'asset' => [], + 'extensions' => [], ], diff --git a/src/ApplicationStrategy.php b/src/ApplicationStrategy.php index f92ef64..c364129 100644 --- a/src/ApplicationStrategy.php +++ b/src/ApplicationStrategy.php @@ -5,34 +5,36 @@ namespace Lean; use League\Route\Strategy; use League\Route\Http\Exception\NotFoundException; +use Picea\Asset\Asset; +use Psr\Container\ContainerInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Picea\Picea; use Psr\Http\Server\RequestHandlerInterface; +use function DI\get; class ApplicationStrategy extends Strategy\ApplicationStrategy { - static array $asset_ext = [ + public const ASSET_TRIGGER_UPDATE = [ "js", "mjs", "manifest", "webmanifest", "css", "png", "ico", "jpg", "jpeg", "gif", "webp", "woff", "woff2", "eot", "svg", "ttf" ]; public function __construct( - public Picea $picea + protected Picea $picea, + protected ContainerInterface $di, ) {} public function getNotFoundDecorator(NotFoundException $exception): MiddlewareInterface { - return new class($this->picea) implements MiddlewareInterface { + return new class($this->picea, $this->di) implements MiddlewareInterface { - protected Picea $picea; - - public function __construct(Picea $picea) - { - $this->picea = $picea; - } + public function __construct( + protected Picea $picea, + protected ContainerInterface $di, + ) { } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { @@ -41,8 +43,16 @@ class ApplicationStrategy extends Strategy\ApplicationStrategy { public function throw404(ServerRequestInterface $request) : ResponseInterface { - if ( class_exists(\Picea\Asset\Asset::class) ) { + if ( getenv('DEBUG') && class_exists(\Picea\Asset\Asset::class) ) { + $params = $request->getServerParams(); + $scpName = basename(explode('?', $params['SCRIPT_NAME'] ?? $params['REQUEST_URI'] ?? $params['PHP_SELF'] ?? "", 1)[0]); + + list(, $ext) = array_pad(explode('.', $scpName), 2, null); + + if ($ext && in_array($ext, ApplicationStrategy::ASSET_TRIGGER_UPDATE)) { + $this->di->get(\Picea\Asset\Asset::class)->launchInstall(); + } } return new \Laminas\Diactoros\Response\HtmlResponse($this->picea->renderHtml("lean/error/404", [], $this), 404); diff --git a/src/Composer.php b/src/Composer.php index 30b851b..20083f5 100644 --- a/src/Composer.php +++ b/src/Composer.php @@ -71,16 +71,35 @@ class Composer } } - protected static function readComposerJson(Event $event) : ? array + public static function readComposerLock() : false|array { + static $content = null; + + $path = static::createPath('composer.lock'); + + if (! file_exists($path) ) { + throw new \UnexpectedValueException("Composer file 'composer.lock' could not be found within your project's root folder."); + } + + return $content ??= file_exists($path) ? json_decode(file_get_contents($path), true) : false; + } + + public static function readComposerJson() : false|array + { + static $content = null; + $path = static::createPath('composer.json'); - return file_exists($path) ? json_decode(file_get_contents($path), true) : null; + if (! file_exists($path) ) { + throw new \UnexpectedValueException("Composer file 'composer.json' could not be found within your project's root folder."); + } + + return $content ??= file_exists($path) ? json_decode(file_get_contents($path), true) : false; } protected static function getNamespaceFromAutoload(Event $event) : ? string { - if ( null !== $composerJson = static::readComposerJson($event) ) { + if ( false !== $composerJson = static::readComposerJson() ) { if ( $psr4 = $composerJson['autoload']['psr-4'] ?? false ) { foreach($psr4 as $ns => $directory) { if ($directory === 'src/') { @@ -103,6 +122,6 @@ class Composer protected static function createPath(... $path) : string { - return implode(DIRECTORY_SEPARATOR, array_merge([ $_SERVER['PWD'] ], $path)); + return implode(DIRECTORY_SEPARATOR, array_merge([ $_SERVER['PWD'] ?? getenv('PROJECT_PATH') ], $path)); } } \ No newline at end of file diff --git a/src/Kernel.php b/src/Kernel.php index 492e6e2..9f4da25 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -110,8 +110,10 @@ class Kernel { if (getenv("APP_ENV") === "prod") { if (getenv("CACHE_PATH")) { + # check if ACPU is there first, $containerBuilder->enableDefinitionCache(); + $containerBuilder->enableCompilation(getenv("CACHE_PATH") . "/di/"); - $containerBuilder->writeProxiesToFile(true, getenv("CACHE_PATH") . "/di/proxies/"); + # No proxies yet... $containerBuilder->writeProxiesToFile(true, getenv("CACHE_PATH") . "/di/"); } } diff --git a/src/LanguageHandler.php b/src/LanguageHandler.php new file mode 100644 index 0000000..151c411 --- /dev/null +++ b/src/LanguageHandler.php @@ -0,0 +1,18 @@ +tell->fromKey($key, $variables) ?: ""; + } +} diff --git a/src/Lean.php b/src/Lean.php index e0cdfef..d124adb 100644 --- a/src/Lean.php +++ b/src/Lean.php @@ -73,19 +73,6 @@ class Lean return $list; } - public function getPiceaAssets() : array - { - $list = []; - - foreach(array_reverse($this->applications) as $apps) { - if ( $apps->piceaAssets ?? null ) { - $list = array_merge_recursive($list, $apps->piceaAssets); - } - } -# dump($list); - return $list; - } - public function getRoutable() : array { return array_merge(...array_map(fn($app) => $app->routes ?? [], $this->applications)); @@ -120,6 +107,15 @@ class Lean return $list; } + public function getAssetPaths() : array + { + $list = array_merge(...array_map(fn($app) => $app->piceaAssets['source'] ?? [], $this->applications)); + + uasort($list, fn($i1, $i2) => $i1['order'] <=> $i2['order'] ); + + return $list; + } + public function getI18n(string $reader) : ? array { switch($reader) { @@ -145,7 +141,7 @@ class Lean { $path = dirname(__DIR__) . "/meta/definitions/"; - return array_merge( + return array_replace( class_exists(\Mcnd\CLI\CliMiddleware::class) ? require($path . "cli.php") : [], class_exists(\Cronard\CronardMiddleware::class) ? require($path . "cronard.php") : [], require($path . "email.php"), @@ -159,4 +155,58 @@ class Lean class_exists(\Picea\Picea::class) ? require($path . "template.php") : [], ); } + + public static function autoloadDefinitionsFromComposerExtra() : array + { + $list = []; + + foreach(Composer::readComposerLock()['packages'] as $package) { + foreach($package['extra']['lean']['autoload']['definitions'] ?? [] as $autoload) { + if (is_string($autoload)) { + $vendor = getenv('VENDOR_PATH') ? getenv('VENDOR_PATH') : dirname(__DIR__, 3); + $file = $vendor . DIRECTORY_SEPARATOR . $package['name'] . DIRECTORY_SEPARATOR . $autoload; + + if ( ! file_exists($file) ) { + throw new \InvalidArgumentException(sprintf("Given autoload file `%s` from package `%s` was not found or is unreachable", $autoload, $package['name'])); + } + + $list = array_replace($list, require($file)); + } + else { + $func = implode('::', array_merge([ key($autoload) ], $autoload)); + + $list = array_replace($list, call_user_func($func)); + } + } + } + + return $list; + } + + public static function autoloadConfigFromComposerExtra() : array + { + $list = []; + + foreach(Composer::readComposerLock()['packages'] as $package) { + foreach($package['extra']['lean']['autoload']['config'] ?? [] as $autoload) { + if (is_string($autoload)) { + $vendor = getenv('VENDOR_PATH') ? getenv('VENDOR_PATH') : dirname(__DIR__, 3); + $file = $vendor . DIRECTORY_SEPARATOR . $package['name'] . DIRECTORY_SEPARATOR . $autoload; + + if ( ! file_exists($file) ) { + throw new \InvalidArgumentException(sprintf("Given autoload file `%s` from package `%s` was not found or is unreachable", $autoload, $package['name'])); + } + + $list = array_merge_recursive($list, require($file)); + } + else { + $func = implode('::', array_merge([ key($autoload) ], $autoload)); + + $list = array_merge_recursive($list, call_user_func($func)); + } + } + } + + return $list; + } } \ No newline at end of file diff --git a/src/PiceaDefaultRegistration.php b/src/PiceaDefaultRegistration.php new file mode 100644 index 0000000..c440117 --- /dev/null +++ b/src/PiceaDefaultRegistration.php @@ -0,0 +1,28 @@ +extensions, $this->syntaxes, $this->controlStructures); + } + + public function registerAll(Compiler $compiler) : void + { + parent::registerAll($compiler); + + $this->ui->registerFormExtension($compiler); + $this->asset->registerExtension($compiler); + } +} \ No newline at end of file From bc6880c166d38f5ad41e1cd4dc99d80bd5a1127d Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Tue, 17 Oct 2023 23:14:59 -0400 Subject: [PATCH 09/13] - WIP on Picea's assets --- meta/definitions/software.php | 12 +++----- meta/definitions/template.php | 18 +++++++---- src/Composer.php | 4 +++ src/Kernel.php | 7 +++++ src/Lean.php | 57 ++++++++++++++++------------------- view/lean/layout/error.phtml | 3 -- 6 files changed, 53 insertions(+), 48 deletions(-) diff --git a/meta/definitions/software.php b/meta/definitions/software.php index c8c4eb9..ddda44b 100644 --- a/meta/definitions/software.php +++ b/meta/definitions/software.php @@ -37,14 +37,10 @@ return [ ], 'asset' => [ - "public_path" => getenv("PUBLIC_PATH"), - 'destination_path' => "static", - 'source' => [ - [ - 'path' => implode(DIRECTORY_SEPARATOR, [ getenv("PROJECT_PATH"), "asset", '' ]), - 'order' => 10 - ] - ], + [ + 'path' => implode(DIRECTORY_SEPARATOR, [ getenv("PROJECT_PATH"), "asset", '' ]), + 'order' => 10 + ] ], 'extensions' => [], diff --git a/meta/definitions/template.php b/meta/definitions/template.php index 6ed312c..7683ae0 100644 --- a/meta/definitions/template.php +++ b/meta/definitions/template.php @@ -4,7 +4,10 @@ use function DI\autowire, DI\create, DI\get; use Laminas\Diactoros\Response\HtmlResponse; -use Picea\{Asset\Action\InstallActionInterface, +use Picea\{ + Asset, + Asset\Action, + Asset\Action\InstallActionInterface, Language\LanguageRegistration, Caching\Cache, Caching\Opcache, @@ -16,7 +19,6 @@ use Picea\{Asset\Action\InstallActionInterface, Method\Request}; use Picea\Extension\{ LanguageHandlerInterface, LanguageExtension, TitleExtension, NumberExtension, UrlExtension }; use Picea\Ui\{ Method, Ui }; -use Picea\Asset\{ Asset, Action }; return [ Picea\Picea::class => function($c) { @@ -29,7 +31,11 @@ return [ Ui::class => autowire(Ui::class), - Asset::class => autowire(Asset::class), + Asset\Asset::class => autowire(Asset\Asset::class), + + Asset\Config::class => create(Asset\Config::class)->constructor( + destination: getenv("PUBLIC_PATH") . DIRECTORY_SEPARATOR . "static" + ), Compiler::class => autowire(Compiler::class), @@ -43,7 +49,7 @@ return [ LanguageHandlerInterface::class => autowire(\Lean\LanguageHandler::class), - LanguageRegistration::class => create(\Lean\PiceaDefaultRegistration::class)->constructor(get('picea.extensions'), [], [], get(Ui::class), get(Asset::class)), + LanguageRegistration::class => create(\Lean\PiceaDefaultRegistration::class)->constructor(get('picea.extensions'), [], [], get(Ui::class), get(Asset\Asset::class)), 'picea.extensions' => function(\Psr\Container\ContainerInterface $c) { return array_merge([ @@ -71,8 +77,8 @@ return [ return new FileFetcher($c->get(Lean\Lean::class)->getViewPaths()); }, - Picea\Asset\FileFetcher::class => function($c) { - return new Picea\Asset\FileFetcher($c->get(Lean\Lean::class)->getAssetPaths()); + Asset\FileFetcher::class => function($c) { + return new Asset\FileFetcher($c->get(Lean\Lean::class)->getAssetPaths()); }, Action\Install::class => autowire(Action\Install::class)->constructor(get(Action\InstallActionInterface::class), []), diff --git a/src/Composer.php b/src/Composer.php index 20083f5..572be9f 100644 --- a/src/Composer.php +++ b/src/Composer.php @@ -69,6 +69,10 @@ class Composer new CacheInvalidator($path, true); } + + if ( file_exists($container = static::createPath('var/cache/di/CompiledContainer.php')) ) { + unlink($container); + } } public static function readComposerLock() : false|array diff --git a/src/Kernel.php b/src/Kernel.php index 9f4da25..b6784a4 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -116,6 +116,13 @@ class Kernel { # No proxies yet... $containerBuilder->writeProxiesToFile(true, getenv("CACHE_PATH") . "/di/"); } } + else { + $compiled = getenv("CACHE_PATH") . "/di/CompiledContainer.php"; + + if ( file_exists($compiled) ) { + unlink($compiled); + } + } # $containerBuilder->useAnnotations(false); diff --git a/src/Lean.php b/src/Lean.php index d124adb..63a00d8 100644 --- a/src/Lean.php +++ b/src/Lean.php @@ -109,7 +109,7 @@ class Lean public function getAssetPaths() : array { - $list = array_merge(...array_map(fn($app) => $app->piceaAssets['source'] ?? [], $this->applications)); + $list = array_merge(...array_map(fn($app) => $app->piceaAssets ?? [], $this->applications)); uasort($list, fn($i1, $i2) => $i1['order'] <=> $i2['order'] ); @@ -162,21 +162,7 @@ class Lean foreach(Composer::readComposerLock()['packages'] as $package) { foreach($package['extra']['lean']['autoload']['definitions'] ?? [] as $autoload) { - if (is_string($autoload)) { - $vendor = getenv('VENDOR_PATH') ? getenv('VENDOR_PATH') : dirname(__DIR__, 3); - $file = $vendor . DIRECTORY_SEPARATOR . $package['name'] . DIRECTORY_SEPARATOR . $autoload; - - if ( ! file_exists($file) ) { - throw new \InvalidArgumentException(sprintf("Given autoload file `%s` from package `%s` was not found or is unreachable", $autoload, $package['name'])); - } - - $list = array_replace($list, require($file)); - } - else { - $func = implode('::', array_merge([ key($autoload) ], $autoload)); - - $list = array_replace($list, call_user_func($func)); - } + $list = array_replace($list, static::loadFromPackage($package, $autoload)); } } @@ -189,24 +175,33 @@ class Lean foreach(Composer::readComposerLock()['packages'] as $package) { foreach($package['extra']['lean']['autoload']['config'] ?? [] as $autoload) { - if (is_string($autoload)) { - $vendor = getenv('VENDOR_PATH') ? getenv('VENDOR_PATH') : dirname(__DIR__, 3); - $file = $vendor . DIRECTORY_SEPARATOR . $package['name'] . DIRECTORY_SEPARATOR . $autoload; - - if ( ! file_exists($file) ) { - throw new \InvalidArgumentException(sprintf("Given autoload file `%s` from package `%s` was not found or is unreachable", $autoload, $package['name'])); - } - - $list = array_merge_recursive($list, require($file)); - } - else { - $func = implode('::', array_merge([ key($autoload) ], $autoload)); - - $list = array_merge_recursive($list, call_user_func($func)); - } + $list = array_merge_recursive($list, static::loadFromPackage($package, $autoload)); } } return $list; } + + protected static function loadFromPackage(array $package, array|string $autoload) : false|array + { + $list = []; + + if (is_string($autoload)) { + $vendor = getenv('VENDOR_DIR') ? getenv('VENDOR_PATH') : dirname(__DIR__, 3); + $file = $vendor . DIRECTORY_SEPARATOR . $package['name'] . DIRECTORY_SEPARATOR . $autoload; + + if ( ! file_exists($file) ) { + throw new \InvalidArgumentException(sprintf("Given autoload file `%s` from package `%s` was not found or is unreachable", $autoload, $package['name'])); + } + + return require($file); + } + else { + $func = implode('::', array_merge([ key($autoload) ], $autoload)); + + return call_user_func($func); + } + + return false; + } } \ No newline at end of file diff --git a/view/lean/layout/error.phtml b/view/lean/layout/error.phtml index 1ba919b..f73f918 100644 --- a/view/lean/layout/error.phtml +++ b/view/lean/layout/error.phtml @@ -8,9 +8,6 @@ - - -