false) * @RouteParam("methods" => [ "GET", "POST", "DELETE" ]) */ trait ControllerTrait { public Session $session; public ? Picea\Picea $picea; public ? MailerInterface $mailer; public array $contextList = []; public function __construct(? Picea\Picea $picea = null, ? Session $session = null, ? MailerInterface $mailer = null) { $this->picea = $picea; $this->session = $session; $this->mailer = $mailer; } 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 renderView(string $view, ?array $variables = null) : ResponseInterface { return static::renderHtml( $this->picea->renderHtml($view, $variables ?? [], $this) ); } 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 renderText(string $html, int $code = 200, array $headers = []) : ResponseInterface { 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(array $data, int $code = 200, array $headers = []) : ResponseInterface { return new JsonResponse($data, $code, $headers); } public function pushContext(FormContext $context) : FormContext { $this->contextList[$context->formName ?? uniqid("context_")] = $context; return $context; } public function context(string $name) : FormContext { return $this->contextList[$name]; } }