diff --git a/meta/definitions/routes.php b/meta/definitions/routes.php index 8712ff7..a167bce 100644 --- a/meta/definitions/routes.php +++ b/meta/definitions/routes.php @@ -68,7 +68,7 @@ return [ }; }, - 'routes.middlewares' => [ "errorHandler", "dump", SessionMiddleware::class, CronardMiddleware::class, HttpBasicAuthentication::class, JavascriptMiddleware::class ], + 'routes.middlewares' => [ "dump", "errorHandler", SessionMiddleware::class, CronardMiddleware::class, HttpBasicAuthentication::class, JavascriptMiddleware::class ], 'routes.list' => function($c) { return function (ContainerInterface $container) { diff --git a/meta/definitions/software.php b/meta/definitions/software.php index a70caa3..2ad9b0a 100644 --- a/meta/definitions/software.php +++ b/meta/definitions/software.php @@ -29,6 +29,8 @@ return [ 'order' => 99, ], ], + + 'extensions' => [], ], 'ulmus' => [], @@ -60,7 +62,7 @@ return [ Lean::class => autowire(Lean::class), CronardMiddleware::class => function($c) { - $cronardMiddleware = new CronardMiddleware(getenv('CRON_KEY'), function() : ResponseInterface { + $cronardMiddleware = new CronardMiddleware($c, getenv('CRON_KEY'), function() : ResponseInterface { return new TextResponse(sprintf("%s - cron task begin...", date('Y-m-d H:i:s'))); }); @@ -82,11 +84,13 @@ return [ $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")); + if ( file_exists($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 trim(file_get_contents($path), " \t\n\r"); + } } return "gitless-project"; diff --git a/meta/definitions/template.php b/meta/definitions/template.php index 0d2c387..3c999a3 100644 --- a/meta/definitions/template.php +++ b/meta/definitions/template.php @@ -32,14 +32,14 @@ return [ }, Compiler::class => function($c) { - return new Compiler(new class(null, [ + return new Compiler(new class(array_merge([ $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 { + ], array_map(fn($class) => $c->get($class), $c->get(Lean\Lean::class)->getPiceaExtensions() ))) extends DefaultRegistrations { public function registerAll(Compiler $compiler) : void { @@ -56,7 +56,7 @@ return [ Method\Pagination::class => autowire(Method\Pagination::class), - LanguageExtension::class => create(LanguageExtension::class)->constructor(get(Context::class), get(LanguageHandler::class)), + LanguageExtension::class => create(LanguageExtension::class)->constructor(get(LanguageHandler::class)), LanguageHandler::class => function($c) { return new class( $c->get(Tell\I18n::class) ) implements LanguageHandler { diff --git a/meta/i18n/en/lean.widget.json b/meta/i18n/en/lean.widget.json index b476a87..09bf7b2 100644 --- a/meta/i18n/en/lean.widget.json +++ b/meta/i18n/en/lean.widget.json @@ -1,7 +1,7 @@ { "pagination": { - "next": "Next >", - "previous": "< Previous", + "next": ">", + "previous": "<", "shown": "elements shown" } } diff --git a/meta/i18n/fr/lean.widget.json b/meta/i18n/fr/lean.widget.json new file mode 100644 index 0000000..e5ad832 --- /dev/null +++ b/meta/i18n/fr/lean.widget.json @@ -0,0 +1,7 @@ +{ + "pagination": { + "next": ">", + "previous": "<", + "shown": "éléments affichés" + } +} diff --git a/src/Application.php b/src/Application.php index e321f84..6a1376c 100644 --- a/src/Application.php +++ b/src/Application.php @@ -8,6 +8,8 @@ class Application { public string $piceaContext; + public array $piceaExtensions; + public array $views; public array $routes; @@ -25,6 +27,10 @@ class Application $this->piceaContext = $picea['context']; } + if ($picea['extensions'] ?? false ) { + $this->piceaExtensions = $picea['extensions']; + } + if ($picea['view'] ?? false) { $this->views = $picea['view']; } diff --git a/src/ControllerTrait.php b/src/ControllerTrait.php index 4eea310..d600b77 100644 --- a/src/ControllerTrait.php +++ b/src/ControllerTrait.php @@ -2,6 +2,8 @@ namespace Lean; +use CSLSJ\Common\RequestResponse\DownloadResponse; +use CSLSJ\Common\RequestResponse\ImageResponse; use Picea, Picea\Ui\Method\FormContext; @@ -35,7 +37,7 @@ trait ControllerTrait { public ? MailerInterface $mailer; public array $contextList = []; -#medias + public function exportJson(ServerRequestInterface $request, string $entityClass, bool $includeRelations = true, ? callable $callback = null) : ResponseInterface { foreach($entityClass::repository()->filterServerRequest( $entityClass::searchRequest()->fromRequest($request->withQueryParams($request->getQueryParams() + ['limit' => PHP_INT_MAX,])) )->loadAll() as $entity) { @@ -76,6 +78,16 @@ trait ControllerTrait { return new JsonResponse($data, $code, $headers); } + public static function renderDownloadable(string $data, string $filename, int $code = 200, array $headers = []) : ResponseInterface + { + return new DownloadResponse($data, $filename, $code, $headers); + } + + public static function renderImage(string $data, int $code = 200, array $headers = []) : ResponseInterface + { + return new ImageResponse($data, $code, $headers); + } + public function fromResponse(ResponseInterface $response) { if ( $response->getStatusCode() === 200 ) { diff --git a/src/Kernel.php b/src/Kernel.php index 4507b7b..09d69a7 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -11,6 +11,7 @@ use League\Route\Strategy\ApplicationStrategy; use Psr\Http\Message\ServerRequestInterface; +use Tell\I18n; use Ulmus\Container\AdapterProxy; use Zend\Diactoros\ServerRequestFactory, @@ -82,7 +83,7 @@ class Kernel { ini_set("log_errors", "1"); ini_set("error_log", $this->errorLogPath); - ini_set('display_errors', getenv("DEBUG") ? 'on' : 'off'); + ini_set('display_errors', getenv("DEBUG") ? 'on' : 'on'); error_reporting($this->errorReporting); @@ -95,8 +96,8 @@ class Kernel { if (getenv("APP_ENV") === "prod") { if (getenv("CACHE_PATH")) { - $containerBuilder->enableCompilation(getenv("CACHE_PATH") . "/di/"); - $containerBuilder->writeProxiesToFile(true); + # $containerBuilder->enableCompilation(getenv("CACHE_PATH") . "/di/"); + # $containerBuilder->writeProxiesToFile(true); } } @@ -116,6 +117,12 @@ class Kernel { $this->container->has(AdapterProxy::class) and $this->container->get(AdapterProxy::class); $this->container->has(Lean::class) and $this->container->get(Lean::class); + if ($this->container->has(I18n::class)) { + $i18n = $this->container->get(I18n::class); + $i18n->locale($this->locale); + $i18n->initialize(!getenv("DEBUG")); + } + return $this; } diff --git a/src/Lean.php b/src/Lean.php index 5d32238..7c8f64b 100644 --- a/src/Lean.php +++ b/src/Lean.php @@ -28,7 +28,7 @@ class Lean throw new \Exception("You must provide at least one application to autoload within your config file ( 'lean' => 'autoload' => [] )"); } - foreach($list as $application) { + foreach(array_filter($list) as $application) { if ( $this->container->has($application) ) { $this->applications[] = ( new Application() )->fromArray($this->container->get($application)); } @@ -49,6 +49,19 @@ class Lean return static::DEFAULT_PICEA_CONTEXT; } + public function getPiceaExtensions() : array + { + $list = []; + + foreach(array_reverse($this->applications) as $apps) { + if ( $apps->piceaExtensions ?? null ) { + $list = array_merge($list, $apps->piceaExtensions); + } + } + + return $list; + } + public function getRoutable() : array { return array_merge(...array_map(fn($app) => $app->routes ?? [], $this->applications)); diff --git a/src/Routing.php b/src/Routing.php index 4f93b15..4e0c4ce 100644 --- a/src/Routing.php +++ b/src/Routing.php @@ -17,7 +17,8 @@ use Notes\Security\SecurityHandler; use Notes\Tell\LanguageHandler; -use Picea\Extension\UrlExtension; +use Picea\Picea, + Picea\Extension\UrlExtension; use Storage\Cookie, Storage\Session; @@ -75,8 +76,8 @@ class Routing { # $container->set($class, autowire($class)->method($method, $request)); - if ( null !== ( $annotation = $this->language->verify($class) ) ) { - if ( $annotation->key ) { + if ( null !== ( $languageAnnotation = $this->language->verify($class) ) ) { + if ( $languageAnnotation->key ) { # TODO !!! $language } } @@ -85,10 +86,16 @@ class Routing { # Checking if user needs to be logged if ( ! $object->user->logged && ( $redirect = $this->security->verify($class, $method) ) ) { + $this->session->redirectedFrom = (string) $request->getUri(); + return $redirect; } - return $object->$method($request, $arguments); + if ( $container->has(Picea::class) ) { + $container->get(Picea::class)->globalVariables['route'] = $annotation; + } + + return $object->$method($request->withAttribute('lean.route', $annotation), $arguments); }); } } diff --git a/view/lean/widget/pagination.phtml b/view/lean/widget/pagination.phtml index 5c8c761..2769e45 100644 --- a/view/lean/widget/pagination.phtml +++ b/view/lean/widget/pagination.phtml @@ -2,13 +2,14 @@ \ No newline at end of file