- Added an HttpFactory and beginning the removal of old annotations from Notes

This commit is contained in:
Dave M. 2023-11-03 16:28:31 +00:00
parent 7e92391113
commit 08c27e38b0
10 changed files with 143 additions and 52 deletions

View File

@ -0,0 +1,17 @@
<?php
use Lean\Factory\HttpFactory;
use \Ulmus\User\Middleware\AuthorizeMiddleware;
use function DI\{get, autowire, create};
return [
AuthorizeMiddleware::class => create(AuthorizeMiddleware::class)->constructor(get('authorize.error')),
'authorize.error' => function($c) {
return HttpFactory::createJsonResponse([
'api.error' => "Authorization failed",
'api.datetime' => ( new \DateTime )->format(\DateTime::ATOM),
]);
},
];

View File

@ -15,7 +15,7 @@ return [
CronardMiddleware::class => function($c) {
$cronardMiddleware = new CronardMiddleware($c, getenv('CRON_KEY'), function() : ResponseInterface {
return new HtmlResponse(sprintf("%s - cron task begin...", date('Y-m-d H:i:s')));
}, [], $c->get(TaskFetcher::class));
}, []);
return $cronardMiddleware->fromFile(getenv("META_PATH")."/crontab.php")->fromAnnotations($c->get(TaskFetcher::class));
},

View File

@ -6,7 +6,7 @@ use Negundo\Client\{ SoftwareConfig, Dump, Task, NegundoMiddleware };
return [
SoftwareConfig::class => create(SoftwareConfig::class)->constructor(getenv('NEGUNDO_HASH'), getenv('NEGUNDO_SERVER')),
# NegundoMiddleware::class => create(NegundoMiddleware::class)->constructor(get(SoftwareConfig::class)),
Dump::class => create(Dump::class)->constructor(get(SoftwareConfig::class)),
Task::class => create(Task::class)->constructor(get(SoftwareConfig::class)),
NegundoMiddleware::class => autowire(NegundoMiddleware::class),
Dump::class => autowire(Dump::class),
Task::class => autowire(Task::class),
];

View File

@ -53,17 +53,25 @@ return [
ApplicationStrategy::class => autowire(\Lean\ApplicationStrategy::class),
'routes.middlewares' => [ "dump", "errorHandler", SessionMiddleware::class, CronardMiddleware::class, Mcnd\Event\EventMiddleware::class, Mcnd\CLI\CliMiddleware::class, ],
'routes.middlewares' => [
"dump", "errorHandler", SessionMiddleware::class, CronardMiddleware::class, Mcnd\Event\EventMiddleware::class, Mcnd\CLI\CliMiddleware::class,
],
'app.middlewares' => [],
'routes.list' => function($c) {
return function (ContainerInterface $container) {
$router = $container->get(Router::class);
foreach($container->get('routes.middlewares') as $i => $middleware) {
if ( $container->has($middleware) ) {
foreach([ 'routes.middlewares', 'app.middlewares' ] as $key) {
if ($container->has('app.middlewares')) {
foreach ($container->get($key) as $i => $middleware) {
if ($container->has($middleware)) {
$router->middleware($container->get($middleware));
}
}
}
}
$routing = $container->get(Lean\Routing::class);
$routing->registerRoute($container, getenv('URL_BASE'));

View File

@ -31,11 +31,17 @@ return [
],
],
#'sqlite' => [
# 'adapter' => getenv("SQLITE_ADAPTER"),
# 'path' => getenv('PROJECT_PATH') . DIRECTORY_SEPARATOR . getenv("SQLITE_PATH"),
# 'pragma' => explode(',', getenv("SQLITE_PRAGMA")),
#],
'sqlite' => [
'adapter' => getenv("SQLITE_ADAPTER"),
'path' => getenv('PROJECT_PATH') . DIRECTORY_SEPARATOR . getenv("SQLITE_PATH"),
'pragma' => explode(',', getenv("SQLITE_PRAGMA")),
'pragma_begin' => array_merge(
explode(',', getenv("SQLITE_PRAGMA_BEGIN")), explode(',', getenv('DEBUG') ? getenv("SQLITE_PRAGMA_DEBUG_BEGIN") : "")
),
'pragma_close' => array_merge(
explode(',', getenv("SQLITE_PRAGMA_CLOSE")), explode(',', getenv('DEBUG') ? getenv("SQLITE_PRAGMA_DEBUG_CLOSE") : "")
),
],
]
]
];

View File

@ -7,7 +7,7 @@ $dir = getenv("META_PATH") . "/definitions";
return array_merge(
Lean\Lean::definitions(),
Lean\Console\Lean::definitions(),
Lean\Lean::autoloadDefinitionsFromComposerExtra(),
[
'%APPKEY%' => [
@ -38,11 +38,18 @@ return array_merge(
'%ESCAPED_NAMESPACE%\\Controller' => implode(DIRECTORY_SEPARATOR, [ getenv("PROJECT_PATH"), 'src', 'Controller', '' ]),
],
],
'app.middlewares' => [],
],
require("$dir/auth.php"),
require("$dir/storage.php"),
require("$dir/security.php"),
require("$dir/env/" . getenv('APP_ENV') . ".php"),
[ 'config' => function () { return require(getenv("META_PATH")."/config.php"); } ]
[
'config' => function () { return array_replace(
require(getenv("META_PATH")."/config.php"),
Lean\Lean::autoloadConfigFromComposerExtra(),
); }
]
);

View File

@ -1,13 +1,13 @@
<?php
use function DI\autowire, DI\create, DI\get;
use Storage\Session;
use Taxus\{ Privilege, Taxus, PermissionGrantInterface, DefaultPermissionGrant };
use Psr\Http\Message\ServerRequestInterface;
use function DI\autowire, DI\create, DI\get;
return [
Taxus::class => function ($c) {
return ( new Taxus( $c->get(PermissionGrantInterface::class) ) )->add(

View File

@ -2,33 +2,19 @@
namespace Lean;
use Lean\Response\{FileDownloadResponse, PdfResponse, ImageResponse, DownloadResponse};
use Picea,
Picea\Ui\Method\FormContext;
use Psr\Http\Message\ServerRequestInterface;
use Storage\Session;
use Laminas\Diactoros\Response\{EmptyResponse, HtmlResponse, TextResponse, RedirectResponse, JsonResponse};
use Ulmus\EntityCollection;
use Psr\Http\Message\ResponseInterface;
use Lean\Factory\HttpFactory;
use Notes\Route\Attribute\Object\Route;
use Notes\Security\Attribute\Security;
use Picea, Picea\Ui\Method\FormContext;
use TheBugs\Email\MailerInterface;
use Storage\Session;
use Notes\Cronard\Annotation\Method\Cronard,
Notes\Breadcrumb\Annotation\Method\Breadcrumb,
Notes\Route\Annotation\Object\Route as RouteParam,
Notes\Route\Annotation\Method\Route,
Notes\Security\Annotation\Security,
Notes\Security\Annotation\Taxus,
Notes\Tell\Annotation\Language;
use Psr\Http\Message\{ ServerRequestInterface, ResponseInterface };
use function file_get_contents;
#[\Notes\Security\Attribute\Security(locked: true)]
#[\Notes\Route\Attribute\Object\Route(method: [ "GET", "POST", "DELETE" ])]
#[Security(locked: true)]
#[Route(method: [ "GET", "POST", "DELETE" ])]
trait ControllerTrait {
public ? \Notes\Breadcrumb\Breadcrumb $breadcrumb;
@ -52,7 +38,7 @@ trait ControllerTrait {
public function renderRawView(string $view, ?array $variables = null) : string
{
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`.");
throw new \RuntimeException("Picea's renderHtml() returned NULL as result ; an error occurred within your template `$view`.");
}
return $content;
@ -78,47 +64,58 @@ trait ControllerTrait {
}
protected function redirect(string $url, int $code = 302, array $headers = []) {
return new RedirectResponse($url, $code, $headers);
return HttpFactory::createRedirectResponse($url, $code, $headers);
}
public static function renderNothing(int $code = 204, array $headers = []) : ResponseInterface
{
return new EmptyResponse($code, $headers);
return HttpFactory::createEmptyResponse($code, $headers);
}
public static function renderText(string $html, int $code = 200, array $headers = []) : ResponseInterface
public static function renderText(string $text, int $code = 200, array $headers = []) : ResponseInterface
{
return new TextResponse($html, $code, $headers);
return HttpFactory::createTextResponse($text, $code, $headers);
}
public static function renderHtml(string $html, int $code = 200, array $headers = []) : ResponseInterface
{
return new HtmlResponse($html, $code, $headers);
return HttpFactory::createHtmlResponse($html, $code, $headers);
}
public static function renderJson(mixed $data, int $code = 200, array $headers = []) : ResponseInterface
{
return new JsonResponse($data, $code, $headers);
return HttpFactory::createJsonResponse($data, $code, $headers);
}
public function renderPdf($rawdata, int $status = 200, array $headers = []) : PdfResponse
public function renderPdf($rawdata, int $status = 200, array $headers = []) : ResponseInterface
{
return new PdfResponse($rawdata, $status, $headers);
return HttpFactory::createPdfResponse($rawdata, $status, $headers);
}
public static function renderDownloadable(string $data, string $filename, int $code = 200, array $headers = []) : ResponseInterface
{
return new DownloadResponse($data, $filename, $code, $headers);
return HttpFactory::createDownloadableResponse($data, $filename, $code, $headers);
}
public static function renderImage(string $data, int $code = 200, array $headers = []) : ResponseInterface
{
return new ImageResponse($data, $code, $headers);
return HttpFactory::createImageResponse($data, $code, $headers);
}
public static function renderAsset(string $path, int $code = 200, array $headers = []) : ResponseInterface
{
return new FileDownloadResponse($path, $code, $headers);
return HttpFactory::createFileDownloadResponse($path, $code, $headers);
}
public function renderMarkdown(string $filepath, int $code = 200, array $headers = []) : ResponseInterface
{
if ( ! class_exists(CommonMarkConverter::class)) {
throw new \BadFunctionCallException("League\CommonMark seems to be missing, please install dependency before trying to render Markdown content");
}
$markdown = ( new CommonMarkConverter() )->convertToHtml(file_get_contents($filepath));
return $this->renderView("docs", get_defined_vars());
}
public function renderCLI(ServerRequestInterface $request, mixed $data) : ResponseInterface

View File

@ -0,0 +1,55 @@
<?php
namespace Lean\Factory;
use Laminas\Diactoros\Response\{EmptyResponse, HtmlResponse, JsonResponse, RedirectResponse, TextResponse};
use Lean\Response\{ DownloadResponse, ImageResponse, FileDownloadResponse, PdfResponse };
use Psr\Http\Message\ResponseInterface;
class HttpFactory
{
public static function createRedirectResponse(string $url, int $code = 302, array $headers = []) : ResponseInterface
{
return new RedirectResponse($url, $code, $headers);
}
public static function createHtmlResponse(string $html, int $code = 200, array $headers = []) : ResponseInterface
{
return new HtmlResponse($html, $code, $headers);
}
public static function createTextResponse(string $text, int $code = 200, array $headers = []) : ResponseInterface
{
return new TextResponse($text, $code, $headers);
}
public static function createJsonResponse(mixed $data, int $code = 200, array $headers = []) : ResponseInterface
{
return new JsonResponse($data, $code, $headers);
}
public static function createEmptyResponse(int $code = 204, array $headers = []) : ResponseInterface
{
return new EmptyResponse($code, $headers);
}
public static function createPdfResponse(string $binary, int $code = 200, array $headers = []) : ResponseInterface
{
return new PdfResponse($binary, $code, $headers);
}
public static function createDownloadableResponse(string $data, string $filename, int $code = 200, array $headers = []) : ResponseInterface
{
return new DownloadResponse($data, $filename, $code, $headers);
}
public static function createImageResponse(string $data, int $code = 200, array $headers = []) : ResponseInterface
{
return new ImageResponse($data, $code, $headers);
}
public static function createFileDownloadResponse(string $path, int $code = 200, array $headers = []) : ResponseInterface
{
return new FileDownloadResponse($path, $code, $headers);
}
}

View File

@ -155,6 +155,7 @@ class Lean
$path = dirname(__DIR__) . "/meta/definitions/";
return array_replace(
require($path . "authorize.php"),
class_exists(\Mcnd\CLI\CliMiddleware::class) ? require($path . "cli.php") : [],
class_exists(\Cronard\CronardMiddleware::class) ? require($path . "cronard.php") : [],
require($path . "email.php"),