lean/src/ControllerTrait.php

206 lines
6.8 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
namespace Lean;
use Lean\Response\{FileDownloadResponse, PdfResponse, ImageResponse, DownloadResponse};
use Picea,
Picea\Ui\Method\FormContext;
use Psr\Http\Message\ServerRequestInterface;
use Storage\Session;
2022-10-12 18:29:59 +00:00
use Laminas\Diactoros\Response\{EmptyResponse, HtmlResponse, TextResponse, RedirectResponse, JsonResponse};
use Ulmus\EntityCollection;
use Psr\Http\Message\ResponseInterface;
use TheBugs\Email\MailerInterface;
2022-01-31 00:05:27 +00:00
use Notes\Cronard\Annotation\Method\Cronard,
2023-01-26 13:28:36 +00:00
Notes\Breadcrumb\Annotation\Method\Breadcrumb,
2022-01-31 00:05:27 +00:00
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 function file_get_contents;
2023-01-26 13:28:36 +00:00
#[\Notes\Security\Attribute\Security(locked: true)]
#[\Notes\Route\Attribute\Object\Route(method: [ "GET", "POST", "DELETE" ])]
trait ControllerTrait {
2023-01-26 13:28:36 +00:00
public ? \Notes\Breadcrumb\Breadcrumb $breadcrumb;
public ? Session $session;
public ? Picea\Picea $picea;
public ? MailerInterface $mailer;
public array $contextList = [];
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) {
$data[] = $callback ? call_user_func($callback, $entity->toArray($includeRelations)) : $entity->toArray($includeRelations);
}
return $this->renderJson( array_filter($data ?? [], function($row) { return $row !== null; }));
}
public function renderRawView(string $view, ?array $variables = null) : string
{
2023-10-06 23:09:45 +00:00
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
{
return static::renderHtml(
$this->renderRawView($view, $variables ?? [])
);
}
2022-05-11 12:32:05 +00:00
public function renderError(string $errorCode, ?array $variables = null) : ResponseInterface
{
return static::renderHtml(
$this->renderRawView("lean/error/{$errorCode}", $variables ?? [])
2022-05-11 12:32:05 +00:00
);
}
public function renderDocumentation(string $filename) : ResponseInterface
{
return $this->renderMarkdown( file_get_contents(getenv("PROJECT_PATH") . "/meta/doc/$filename.md") );
}
protected function redirect(string $url, int $code = 302, array $headers = []) {
return new RedirectResponse($url, $code, $headers);
}
public static function renderNothing(int $code = 204, array $headers = []) : ResponseInterface
{
2022-10-12 18:29:59 +00:00
return new EmptyResponse($code, $headers);
}
2022-10-12 18:29:59 +00:00
public static function renderText(string $html, int $code = 200, array $headers = []) : ResponseInterface
{
2022-10-12 18:29:59 +00:00
return new TextResponse($html, $code, $headers);
}
public static function renderHtml(string $html, int $code = 200, array $headers = []) : ResponseInterface
{
return new HtmlResponse($html, $code, $headers);
}
public static function renderJson(mixed $data, int $code = 200, array $headers = []) : ResponseInterface
{
return new JsonResponse($data, $code, $headers);
}
public function renderPdf($rawdata, int $status = 200, array $headers = []) : PdfResponse
{
return new PdfResponse($rawdata, $status, $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 static function renderAsset(string $path, int $code = 200, array $headers = []) : ResponseInterface
{
return new FileDownloadResponse($path, $code, $headers);
}
2023-07-09 16:37:39 +00:00
public function renderCLI(ServerRequestInterface $request, mixed $data) : ResponseInterface
{
if ($data instanceof \JsonSerializable ) {
return $this->renderJson(
$data
);
}
elseif ( is_array($data) ) {
var_export($data);
}
return $this->renderText(
$data . PHP_EOL
);
}
public function fromResponse(ResponseInterface $response)
{
if ( $response->getStatusCode() === 200 ) {
if ( $response instanceof \Laminas\Diactoros\Response\JsonResponse) {
return $response->getPayload();
}
}
return null;
}
public function asset(string $url, array $parameters = []) : string
{
if (! $this->picea ) {
throw new \Exception("A picea object must be provided on class initialization to use this method.");
}
return $this->picea->compiler->getExtensionFromToken('asset')->buildAssetUrl($url, $parameters);
}
public function url(string $url, array $parameters = []) : string
{
if (! $this->picea ) {
throw new \Exception("A picea object must be provided on class initialization to use this method.");
}
return $this->picea->compiler->getExtensionFromToken('url')->buildUrl($url, $parameters);
}
public function route(string $name, array $parameters = []) : string
{
if (! $this->picea ) {
throw new \Exception("A picea object must be provided on class initialization to use this method.");
}
return $this->picea->compiler->getExtensionFromToken('route')->buildRouteUrl($name, $parameters);
}
2023-01-26 13:28:36 +00:00
public function json($data, int $flags = 0) : string
{
return htmlentities(json_encode($data, $flags), ENT_QUOTES, 'UTF-8');
}
public function pushContext(FormContext $context) : FormContext
{
$this->contextList[$context->formName ?? uniqid("context_")] = $context;
return $context;
}
public function context(? string $name = null) : ? FormContext
{
return $name ? $this->contextList[$name] : array_values($this->contextList)[0] ?? null;
}
2023-01-26 13:28:36 +00:00
public function isRoute(mixed $name, ServerRequestInterface $request) : bool
{
foreach((array) $name as $item) {
if ( fnmatch($item, $request->getAttribute('lean.route')->name) ) {
return true;
}
}
return false;
}
}