- Added the application feature - a big reason why Lean was created in the first place.

- Began moving some repetitive parts from projects to projects into lean
This commit is contained in:
Dave M. 2020-12-07 14:11:29 +00:00
parent 4bd68a74f8
commit bdb9f7f732
13 changed files with 571 additions and 2 deletions

19
meta/definitions/http.php Normal file
View File

@ -0,0 +1,19 @@
<?php
use function DI\autowire, DI\create, DI\get;
use Psr\Http\Message\ServerRequestInterface;
use Zend\Diactoros\ServerRequestFactory;
use Zend\HttpHandlerRunner\Emitter\EmitterInterface,
Zend\HttpHandlerRunner\Emitter\SapiEmitter;
return [
ServerRequestInterface::class => function ($c) {
return ServerRequestFactory::fromGlobals(
$_SERVER, $_GET, $_POST, $_COOKIE, $_FILES
);
},
EmitterInterface::class => create(SapiEmitter::class),
];

View File

@ -0,0 +1,31 @@
<?php
use function DI\autowire, DI\create, DI\get;
use Notes\Tell\LanguageHandler;
return [
Tell\I18n::class => create( Tell\I18n::class ) ->constructor(
get(Tell\Reader\JsonReader::class),
get(Tell\Reader\PhpReader::class),
getenv("DEBUG") ? create(Tell\PrintMissingKey::class) : get('tell.fallback')
),
'tell.fallback' => function($c) {
$tell = new Tell\I18n( $c->get(Tell\Reader\PhpReader::class) );
$tell->locale = "en_US";
return $tell;
},
# TODO -- accept folders from Lean Apps
Tell\Reader\PhpReader::class => function($c) {
return new Tell\Reader\PhpReader($c->get(Lean\Lean::class)->getI18n('php'), true);
},
Tell\Reader\JsonReader::class => function($c) {
return new Tell\Reader\JsonReader($c->get(Lean\Lean::class)->getI18n('json'), true);
},
LanguageHandler::class => autowire(LanguageHandler::class),
];

View File

@ -0,0 +1,86 @@
<?php
use function DI\autowire, DI\create, DI\get;
use League\Route\Strategy\ApplicationStrategy,
League\Route\Http\Exception\NotFoundException,
League\Route\Router;
use Psr\Http\Message\ResponseFactoryInterface,
Psr\Container\ContainerInterface,
Psr\Http\Message\ResponseInterface,
Psr\Http\Message\ServerRequestInterface,
Psr\Http\Server\MiddlewareInterface,
Psr\Http\Server\RequestHandlerInterface;
use TheBugs\JavascriptMiddleware;
use Cronard\CronardMiddleware;
use Tuupola\Middleware\HttpBasicAuthentication;
use Notes\Route\RouteFetcher;
use Storage\SessionMiddleware;
return [
Lean\Routing::class => autowire(Lean\Routing::class),
RouteFetcher::class => function($c) {
$fetcher = new RouteFetcher();
$fetcher->setFolderList(array_map(function($item) {
return getenv("PROJECT_PATH") . $item;
}, $c->get(Lean\Lean::class)->getRoutable()));
return $fetcher;
},
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 Zend\Diactoros\Response\HtmlResponse($this->picea->renderHtml("lean/error/404", [], $this), 404);
}
};
}
};
},
'routes.list' => function($c) {
return function (ContainerInterface $container) {
$router = $container->get(Router::class);
foreach([ "errorHandler", "dump", SessionMiddleware::class, CronardMiddleware::class, HttpBasicAuthentication::class, JavascriptMiddleware::class ] as $middleware) {
if ( $container->has($middleware) ) {
$router->middleware($container->get($middleware));
}
}
$routing = $container->get(Lean\Routing::class);
$routing->registerRoute($container, getenv('URL_BASE'));
return $router;
};
},
];

View File

@ -0,0 +1,100 @@
<?php
use function DI\autowire, DI\create, DI\get;
use Zend\Diactoros\Response\TextResponse;
use TheBugs\JavascriptMiddleware;
use Cronard\CronardMiddleware;
use Lean\Lean;
use Storage\Cookie,
Storage\Session,
Storage\SessionMiddleware;
return [
'lean.default' => [
'picea' => [
'context' => "Lean\\View",
'view' => [
[
'path' => getenv("VIEW_PATH"),
'order' => 10,
],
[
'path' => getenv("PROJECT_PATH") . "/vendor/mcnd/lean/view/",
'order' => 99,
],
],
],
'ulmus' => [
'entities' => [ 'Growlogs\\Entity' ]
],
'tell' => [
'json' => [
[
'path' => getenv("META_PATH") . "/i18n",
'order' => 10,
],
[
'path' => getenv("PROJECT_PATH") . "/vendor/mcnd/lean/meta/i18n",
'order' => 99,
],
],
'php' => [
[
'path' => getenv("CACHE_PATH")."/i18n",
'order' => 0,
]
]
],
'routes' => [
'Growlogs\\Api' => '/src/Api/',
'Growlogs\\Dev' => '/src/Dev/',
'Growlogs\\Web' => '/src/Web/',
],
],
Lean::class => autowire(Lean::class),
CronardMiddleware::class => function($c) {
$cronardMiddleware = new CronardMiddleware(getenv('CRON_KEY'), function() : ResponseInterface {
return new TextResponse(sprintf("%s - cron task begin...", date('Y-m-d H:i:s')));
});
return $cronardMiddleware->fromFile(getenv("META_PATH")."/crontab.php");
},
JavascriptMiddleware::class => create(JavascriptMiddleware::class),
Cookie::class => create(Cookie::class)->constructor([ 'secure' => true, 'samesite' => 'Strict' ], getenv("LEAN_RANDOM")),
Session::class => create(Session::class),
SessionMiddleware::class => create(SessionMiddleware::class)->constructor(get(Cookie::class), [ 'name' => "lean_sess_" . substr(md5(getenv("LEAN_RANDOM")), 0, 12) ]),
'git.commit' => function($c) {
if ( getenv("DEBUG") ) {
return uniqid("debug_");
}
$gitdir = getenv("PROJECT_PATH") . DIRECTORY_SEPARATOR . ".git" . DIRECTORY_SEPARATOR;
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 "gitless-project";
},
];

View File

@ -0,0 +1,86 @@
<?php
use function DI\autowire, DI\create, DI\get;
use Zend\Diactoros\Response\HtmlResponse;
use Picea\Picea,
Picea\Caching\Cache,
Picea\Caching\Opcache,
Picea\Compiler,
Picea\Compiler\Context,
Picea\Compiler\BaseContext,
Picea\Extension\LanguageHandler,
Picea\Extension\LanguageExtension,
Picea\Extension\TitleExtension,
Picea\Extension\UrlExtension,
Picea\FileFetcher,
Picea\Language\DefaultRegistrations,
Picea\Method\Request,
Picea\Ui\Method,
Picea\Ui\Ui;
return [
Picea::class => function($c) {
return new Picea(function($html) {
return new HtmlResponse( $html );
}, $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(null, [
$c->get(LanguageExtension::class),
$c->get(TitleExtension::class),
$c->get(UrlExtension::class),
$c->get(Method\Form::class),
$c->get(Method\Pagination::class),
$c->get(Request::class),
]) 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(Context::class), 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) ?: "";
}
};
},
TitleExtension::class => autowire(TitleExtension::class),
UrlExtension::class => create(UrlExtension::class)->constructor(get(Context::class), getenv("URL_BASE"), get('git.commit')),
Cache::class => create(Opcache::class)->constructor(getenv("CACHE_PATH"), get(Context::class)),
FileFetcher::class => function($c) {
return new FileFetcher($c->get(Lean\Lean::class)->getViewPaths());
},
];

View File

@ -0,0 +1,9 @@
{
"404": {
"title": "Page not found",
"page-title": "404 - Page not found",
"subtitle": "You may have followed an invalid or expired link...",
"message": "It seems like an error occured on the link you visited / action you tried. A notification was sent to the application's developer.",
"back": "Return to previous page"
}
}

View File

@ -0,0 +1,9 @@
{
"404": {
"title": "Page introuvable",
"page-title": "404 - Page introuvable",
"subtitle": "Vous avez peut-être suivi un lien expiré ou invalide...",
"message": "Il semblerait que l'action que vous avez tentez à mener vers une page qui n'existe pas / plus. Une notification a été envoyé au développeur de l'application.",
"back": "Revenir à la page précédente"
}
}

55
src/Application.php Normal file
View File

@ -0,0 +1,55 @@
<?php
namespace Lean;
class Application
{
public string $piceaContext;
public array $views;
public array $routes;
public array $entities;
public array $tellJson;
public array $tellPhp;
public function fromArray(array $data) : self
{
if (is_array($picea = $data['picea'] ?? false)) {
if ($picea['context'] ?? false ) {
$this->piceaContext = $picea['context'];
}
if ($picea['view'] ?? false) {
$this->views = $picea['view'];
}
}
if (is_array($ulmus = $data['ulmus'] ?? false)) {
if ($ulmus['entities'] ?? false) {
$this->entities = $ulmus['entities'];
}
}
if (is_array($tell = $data['tell'] ?? false)) {
if ($tell['json'] ?? false) {
$this->tellJson = $tell['json'];
}
if ($tell['php'] ?? false) {
$this->tellPhp = $tell['php'];
}
}
if (is_array($routes = $data['routes'] ?? false)) {
$this->routes = $data['routes'];
}
return $this;
}
}

View File

@ -29,7 +29,7 @@ use function file_get_contents;
*/
trait ControllerTrait {
public Session $session;
public Session $session;
public ? Picea\Picea $picea;
@ -37,7 +37,7 @@ trait ControllerTrait {
public array $contextList = [];
public function __construct(? Picea\Picea $picea, Session $session, ? MailerInterface $mailer = null) {
public function __construct(? Picea\Picea $picea = null, ? Session $session = null, ? MailerInterface $mailer = null) {
$this->picea = $picea;
$this->session = $session;
$this->mailer = $mailer;

View File

@ -112,6 +112,7 @@ class Kernel {
protected function serviceContainer() : self
{
$this->container->has(AdapterProxy::class) and $this->container->get(AdapterProxy::class);
$this->container->has(Lean::class) and $this->container->get(Lean::class);
return $this;
}

99
src/Lean.php Normal file
View File

@ -0,0 +1,99 @@
<?php
namespace Lean;
use Psr\Container\ContainerInterface;
class Lean
{
const DEFAULT_PICEA_CONTEXT = __NAMESPACE__;
protected ContainerInterface $container;
public array $applications = [];
public function __construct(ContainerInterface $container)
{
$this->container = $container;
$this->loadApplications();
}
protected function loadApplications() : void
{
$list = $this->container->get('config')['lean']['autoload'] ?? [];
if (! $list ) {
throw new \Exception("You must provide at least one application to autoload within your config file ( 'lean' => 'autoload' => [] )");
}
foreach($list as $application) {
if ( $this->container->has($application) ) {
$this->applications[] = ( new Application() )->fromArray($this->container->get($application));
}
else {
throw new \RuntimeException("Trying to load an application '$application' which have not been configured yet");
}
}
}
public function getPiceaContext() : string
{
foreach(array_reverse($this->applications) as $apps) {
if ( $apps->piceaContext ) {
return $apps->piceaContext;
}
}
return static::DEFAULT_PICEA_CONTEXT;
}
public function getRoutable() : array
{
return array_merge(...array_map(fn($app) => $app->routes ?? [], $this->applications));
}
public function getViewPaths() : array
{
$list = array_merge(...array_map(fn($app) => $app->views ?? [], $this->applications));
uasort($list, fn($i1, $i2) => $i1['order'] <=> $i2['order'] );
return $list;
}
public function getI18n(string $reader) : ? array
{
switch($reader) {
case "php":
$list = array_merge(...array_map(fn($app) => $app->tellPhp ?? [], $this->applications));
break;
case "json":
$list = array_merge(...array_map(fn($app) => $app->tellJson ?? [], $this->applications));
break;
}
if ( $list ?? false ) {
uasort($list, fn($i1, $i2) => $i2['order'] <=> $i1['order']);
return array_map(fn($item) => $item['path'], $list);
}
return null;
}
public static function definitions() : array
{
$path = dirname(__DIR__) . "/meta/definitions/";
return array_merge(
require($path . "http.php"),
require($path . "language.php"),
require($path . "routes.php"),
require($path . "software.php"),
require($path . "template.php"),
);
}
}

24
view/lean/error/404.phtml Normal file
View File

@ -0,0 +1,24 @@
{% extends "lean/layout/error" %}
{% language.set "lean.error.404" %}
{% title _('page-title') %}
{% section "content-right" %}
<div>
<div class="title">{% _ "title" %}</div>
<div class="subtitle">{% _ "subtitle" %}</div>
<div class="content">{% _ "message" %}</div>
<u><a href="#" onclick="history.back()">{% _ "back" %}</a></u>
</div>
{% endsection %}
{% section "content-left" %}
<img class="picto-login" src="{% asset 'asset/picto/undraw_lost_bqr2.svg' %}">
{% endsection %}
{% section "head.css" %}
.title {font-size:2rem}
.subtitle {font-size:1.25rem; padding-top: 1rem;}
.content {padding-top:1rem}
{% endsection %}

View File

@ -0,0 +1,50 @@
{% section "layout" %}
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; ">
<meta name="viewport" content="width=device-width">
<title>{{ title() }}</title>
<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}
#main-content {min-height: 100vh;width: 100vw;align-items:center;justify-content: center;}
#wrapper-content {max-width:960px; max-height:65vh;flex-direction:row-reverse; margin:0 10vw}
#wrapper-content .content-right {background:#efefef;padding:5vh 2vw;align-items:center;display:flex;justify-content:center; width:100%;border-radius:6px 0 0 6px}
#wrapper-content .content-left {background:#ffffff;padding:5vh 2vw;border-radius:0 6px 6px 0;display:flex;align-items:center;justify-content: center}
.form-user-login {width:80%;}
.picto-login {max-width:80%;}
{% section "head.css" %}{% endsection %}
</style>
</head>
<body class="body">
<ui-responsive id="full-wrapper">
<div id="body-wrapper">
<ui-responsive id="main-content" class="main-content is-flex">
<div id="wrapper-content" class="columns">
<div class="column content-left">
{% section "content-left" %}{% endsection %}
</div>
<div class="column content-right">
{% section "content-right" %}{% endsection %}
</div>
</div>
</ui-responsive>
</div>
</ui-responsive>
{% section "javascript-body" %}{% endsection %}
{% section "component-template" %}{% endsection %}
</body>
</html>
{% endsection %}