- Second commit, added the ControllerTrait and Routing lib
This commit is contained in:
parent
9b07a19f81
commit
69f3993aa9
|
@ -0,0 +1,72 @@
|
|||
<?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);
|
||||
}
|
||||
}
|
|
@ -11,6 +11,8 @@ use League\Route\Strategy\ApplicationStrategy;
|
|||
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
use Ulmus\Container\AdapterProxy;
|
||||
|
||||
use Zend\Diactoros\ServerRequestFactory,
|
||||
Zend\HttpHandlerRunner\Emitter\EmitterInterface;
|
||||
|
||||
|
@ -109,6 +111,8 @@ class Kernel {
|
|||
|
||||
protected function serviceContainer() : self
|
||||
{
|
||||
$this->container->has(AdapterProxy::class) and $this->container->get(AdapterProxy::class);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
namespace Lean;
|
||||
|
||||
use function DI\autowire, DI\create;
|
||||
|
||||
use League\Route\RouteGroup,
|
||||
League\Route\Router;
|
||||
|
||||
use Psr\Http\Message\ServerRequestInterface,
|
||||
Psr\Http\Message\ResponseInterface,
|
||||
Psr\Container\ContainerInterface;
|
||||
|
||||
use Notes\Route\RouteFetcher;
|
||||
|
||||
use Notes\Security\SecurityHandler;
|
||||
|
||||
use Notes\Tell\LanguageHandler;
|
||||
|
||||
use Picea\Extension\UrlExtension;
|
||||
|
||||
use Storage\Cookie,
|
||||
Storage\Session;
|
||||
|
||||
class Routing {
|
||||
|
||||
protected Session $session;
|
||||
|
||||
protected Cookie $cookie;
|
||||
|
||||
protected UrlExtension $extension;
|
||||
|
||||
protected RouteFetcher $fetcher;
|
||||
|
||||
protected SecurityHandler $security;
|
||||
|
||||
protected LanguageHandler $language;
|
||||
|
||||
public function __construct(
|
||||
Session $session,
|
||||
Cookie $cookie,
|
||||
UrlExtension $extension,
|
||||
Router $router,
|
||||
RouteFetcher $routeFetcher,
|
||||
SecurityHandler $security,
|
||||
LanguageHandler $language
|
||||
)
|
||||
{
|
||||
$this->session = $session;
|
||||
$this->cookie = $cookie;
|
||||
$this->extension = $extension;
|
||||
$this->fetcher = $routeFetcher;
|
||||
$this->security = $security;
|
||||
$this->language = $language;
|
||||
$this->router = $router;
|
||||
}
|
||||
|
||||
public function registerRoute(ContainerInterface $container, string $urlBase) {
|
||||
$this->router->group(rtrim($urlBase, "/"), function (RouteGroup $route) use ($container) {
|
||||
foreach($this->fetcher->compile() as $annotation) {
|
||||
# Register routes to the UrlExtension from picea (handling url, route and asset extensions)
|
||||
if ( null !== ( $name = $annotation->name ?? null ) ) {
|
||||
$this->extension->registerRoute($name, $annotation->getRoute(), $annotation->class, $annotation->classMethod, $annotation->methods ?? (array) $annotation->method);
|
||||
}
|
||||
|
||||
foreach((array) ( $annotation->method ?? $annotation->methods ) as $method) {
|
||||
# Mapping every URLs from annotations in searched folders (Api, Controller, etc...)
|
||||
|
||||
$route->map(strtoupper($method), $annotation->getRoute(), function (ServerRequestInterface $request, array $arguments) use (
|
||||
$container, $route, $annotation
|
||||
) : ResponseInterface
|
||||
{
|
||||
$class = $annotation->class;
|
||||
$method = $annotation->classMethod;
|
||||
|
||||
# $container->set($class, autowire($class)->method($method, $request));
|
||||
|
||||
if ( null !== ( $annotation = $this->language->verify($class) ) ) {
|
||||
if ( $annotation->key ) {
|
||||
# TODO !!! $language
|
||||
}
|
||||
}
|
||||
|
||||
$object = $container->get($class);
|
||||
|
||||
# Checking if user needs to be logged
|
||||
if ( ! $object->user->logged && ( $redirect = $this->security->verify($class, $method) ) ) {
|
||||
return $redirect;
|
||||
}
|
||||
|
||||
return $object->$method($request, $arguments);
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue