lean/src/ControllerTrait.php

73 lines
2.0 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
namespace Lean;
use Picea;
use Storage\Session;
use Laminas\Diactoros\Response\{ HtmlResponse, TextResponse, RedirectResponse, JsonResponse };
use Ulmus\EntityCollection;
use Psr\Http\Message\ResponseInterface;
# use TheBugs\Email\MailerInterface;
use Notes\Route\Annotation\Object\Route as RouteParam,
Notes\Route\Annotation\Method\Route,
Notes\Security\Annotation\Security,
Notes\Tell\Annotation\Language;
use function file_get_contents;
/**
* @Security("locked" => false)
* @RouteParam("methods" => [ "GET", "POST", "DELETE" ])
*/
trait ControllerTrait {
public Session $session;
public ? Picea\Picea $picea;
public MailerInterface $mailer;
public function __construct(? Picea\Picea $picea, Session $session/*, MailerInterface $mailer*/) {
$this->picea = $picea;
$this->session = $session;
// $this->mailer = $mailer;
}
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);
}
}