- Done fixing Container build caching and WIP on Picea's asset

This commit is contained in:
Dave M. 2023-10-17 20:10:42 -04:00
parent 4dcc7f407e
commit 4c0cddd617
10 changed files with 208 additions and 79 deletions

View File

@ -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' => [],
],

View File

@ -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),
];

View File

@ -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', [

View File

@ -13,9 +13,8 @@ return array_merge(
'%APPKEY%' => [
'picea' => [
'context' => "%ESCAPED_NAMESPACE%\\View",
'extensions' => [
],
'asset' => [],
'extensions' => [],
],

View File

@ -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);

View File

@ -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));
}
}

View File

@ -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/");
}
}

18
src/LanguageHandler.php Normal file
View File

@ -0,0 +1,18 @@
<?php
namespace Lean;
use Picea\Extension\LanguageHandlerInterface;
use Tell;
class LanguageHandler implements LanguageHandlerInterface {
public function __construct(
public Tell\I18n $tell
) {}
public function languageFromKey(string $key, array $variables = []) #: array|string
{
return $this->tell->fromKey($key, $variables) ?: "";
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace Lean;
use Picea\Compiler;
use Picea\Language\DefaultRegistrations;
use Picea\{ Ui\Ui, Asset\Asset };
class PiceaDefaultRegistration extends DefaultRegistrations {
public function __construct(
protected array $extensions,
protected array $syntaxes,
protected array $controlStructures,
public null|Ui $ui,
public null|Asset $asset,
) {
parent::__construct($this->extensions, $this->syntaxes, $this->controlStructures);
}
public function registerAll(Compiler $compiler) : void
{
parent::registerAll($compiler);
$this->ui->registerFormExtension($compiler);
$this->asset->registerExtension($compiler);
}
}