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

This commit is contained in:
mcndave committed 2023-10-17 20:10:42 -04:00
1 parent 4dcc7f407e
commit 4c0cddd617
10 files changed
+208 -79

No files matched your search

+20 -10
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);
+23 -4
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));
}
}
+3 -1
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
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) ?: "";
}
}
+64 -14
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;
}
}
+28
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);
}
}