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

This commit is contained in:
mcndave committed 2023-11-03 16:28:31 +00:00
1 parent 7e92391113
commit 08c27e38b0
10 files changed
+143 -52

No files matched your search

+31 -34
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
+55
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);
}
}
+1
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"),