From 4c0cddd6176d43c7d369f556cd5fb5d2027065bd Mon Sep 17 00:00:00 2001 From: Dave Mc Nicoll Date: Tue, 17 Oct 2023 20:10:42 -0400 Subject: [PATCH] - 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