This commit is contained in:
Dave M. 2023-11-01 09:57:00 -04:00
commit 0ad57c87df
16 changed files with 314 additions and 88 deletions

View File

@ -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"

View File

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

View File

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

View File

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

9
skeleton/lean Executable file
View File

@ -0,0 +1,9 @@
#!/bin/php
<?php
try {
require_once(__DIR__."/src/Kernel.php");
}
catch(\Throwable $t) {
echo sprintf("%s [ %s ]" , $t->getMessage(), var_export($t->getTrace(), true));
}

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

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

View File

@ -0,0 +1,61 @@
<?php
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 {
public const ASSET_TRIGGER_UPDATE = [
"js", "mjs", "manifest", "webmanifest", "css", "png", "ico",
"jpg", "jpeg", "gif", "webp", "woff", "woff2", "eot", "svg",
"ttf"
];
public function __construct(
protected Picea $picea,
protected ContainerInterface $di,
) {}
public function getNotFoundDecorator(NotFoundException $exception): MiddlewareInterface
{
return new class($this->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);
}
};
}
}

View File

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

View File

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

View File

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

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

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

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 AND $this->ui->registerFormExtension($compiler);
$this->asset AND $this->asset->registerExtension($compiler);
}
}

View File

@ -8,9 +8,6 @@
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato|Ubuntu%20Mono">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.7.1/css/bulma.min.css">
<link rel="stylesheet" href='{% asset "asset/css/bulma.extension.css" %}'>
<link rel="stylesheet" href="https://cdn.eckinox.net/fontawesome/latest/css/fontawesome-all.min.css">
<style>
body {background:#272822}