diff --git a/composer.json b/composer.json index 1763a42..8316d52 100644 --- a/composer.json +++ b/composer.json @@ -21,16 +21,20 @@ "laminas/laminas-httphandlerrunner": "2.5.x-dev", "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", "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 +49,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 +73,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 +89,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" 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/software.php b/meta/definitions/software.php index 3d9a3b8..ddda44b 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,18 @@ return [ 'order' => 10, ], [ - 'path' => getenv("PROJECT_PATH") . implode(DIRECTORY_SEPARATOR, [ "", "vendor", "mcnd" , "lean" , "view" ]), + 'path' => implode(DIRECTORY_SEPARATOR, [ $dir, "view", '' ]), 'order' => 99, ], ], + 'asset' => [ + [ + 'path' => implode(DIRECTORY_SEPARATOR, [ getenv("PROJECT_PATH"), "asset", '' ]), + 'order' => 10 + ] + ], + 'extensions' => [], ], @@ -83,11 +92,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; } } diff --git a/meta/definitions/template.php b/meta/definitions/template.php index 95d3d81..e799323 100644 --- a/meta/definitions/template.php +++ b/meta/definitions/template.php @@ -2,23 +2,47 @@ use function DI\autowire, DI\create, DI\get; -use Laminas\Diactoros\Response\HtmlResponse; +use Picea\{ + Language\LanguageRegistration, + Caching\Cache, + Caching\Opcache, + Compiler, + Compiler\Context, + Compiler\BaseContext, + FileFetcher, + Method\Request}; + +use Picea\Extension\{ LanguageHandlerInterface, LanguageExtension, TitleExtension, NumberExtension, UrlExtension }; -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 }; 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() ); + 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), + + 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), @@ -27,38 +51,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); - ( new Ui() )->registerFormExtension($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), 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/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/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/ApplicationStrategy.php b/src/ApplicationStrategy.php new file mode 100644 index 0000000..6288c45 --- /dev/null +++ b/src/ApplicationStrategy.php @@ -0,0 +1,61 @@ +picea, $this->di) implements MiddlewareInterface { + + public function __construct( + protected Picea $picea, + protected ContainerInterface $di, + ) { } + + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { + return $this->throw404($request); + } + + public function throw404(ServerRequestInterface $request) : ResponseInterface + { + if ( getenv('DEBUG') && class_exists(\Picea\Asset\Asset::class) ) { + $params = $request->getServerParams(); + + $scpName = basename(explode('?', $params['REQUEST_URI'] ?? "", 2)[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); + } + }; + } +} \ No newline at end of file diff --git a/src/Composer.php b/src/Composer.php index 30b851b..572be9f 100644 --- a/src/Composer.php +++ b/src/Composer.php @@ -69,18 +69,41 @@ class Composer new CacheInvalidator($path, true); } + + if ( file_exists($container = static::createPath('var/cache/di/CompiledContainer.php')) ) { + unlink($container); + } } - 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 +126,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/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 diff --git a/src/Kernel.php b/src/Kernel.php index d99ebe0..b6784a4 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -110,8 +110,17 @@ class Kernel { if (getenv("APP_ENV") === "prod") { if (getenv("CACHE_PATH")) { - # $containerBuilder->enableCompilation(getenv("CACHE_PATH") . "/di/"); - # $containerBuilder->writeProxiesToFile(true); + # check if ACPU is there first, $containerBuilder->enableDefinitionCache(); + + $containerBuilder->enableCompilation(getenv("CACHE_PATH") . "/di/"); + # No proxies yet... $containerBuilder->writeProxiesToFile(true, getenv("CACHE_PATH") . "/di/"); + } + } + else { + $compiled = getenv("CACHE_PATH") . "/di/CompiledContainer.php"; + + if ( file_exists($compiled) ) { + unlink($compiled); } } 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 7e3dc77..af48421 100644 --- a/src/Lean.php +++ b/src/Lean.php @@ -102,11 +102,33 @@ class Lean { $list = array_merge(...array_map(fn($app) => $app->views ?? [], $this->applications)); + $this->verifyPathList($list); + uasort($list, fn($i1, $i2) => $i1['order'] <=> $i2['order'] ); return $list; } + public function getAssetPaths() : array + { + $list = array_merge(...array_map(fn($app) => $app->piceaAssets ?? [], $this->applications)); + + $this->verifyPathList($list); + + uasort($list, fn($i1, $i2) => $i1['order'] <=> $i2['order'] ); + + return $list; + } + + protected function verifyPathList(array $list) : void + { + foreach($list as $item) { + if (! isset($item['order']) ) { + throw new \RuntimeException(sprintf("An error occured while verifying a path list (%s)", json_encode($item, \JSON_PRETTY_PRINT))); + } + } + } + public function getI18n(string $reader) : ? array { switch($reader) { @@ -132,7 +154,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"), @@ -146,4 +168,53 @@ 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) { + $list = array_replace($list, static::loadFromPackage($package, $autoload)); + } + } + + return $list; + } + + public static function autoloadConfigFromComposerExtra() : array + { + $list = []; + + foreach(Composer::readComposerLock()['packages'] as $package) { + foreach($package['extra']['lean']['autoload']['config'] ?? [] as $autoload) { + $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/src/PiceaDefaultRegistration.php b/src/PiceaDefaultRegistration.php new file mode 100644 index 0000000..eeb6192 --- /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 AND $this->ui->registerFormExtension($compiler); + $this->asset AND $this->asset->registerExtension($compiler); + } +} \ 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 @@ - - -