diff --git a/src/ControllerTrait.php b/src/ControllerTrait.php new file mode 100644 index 0000000..2d5b1b3 --- /dev/null +++ b/src/ControllerTrait.php @@ -0,0 +1,72 @@ + 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); + } +} diff --git a/src/Kernel.php b/src/Kernel.php index 0df559b..87db266 100644 --- a/src/Kernel.php +++ b/src/Kernel.php @@ -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; } diff --git a/src/Routing.php b/src/Routing.php new file mode 100644 index 0000000..9b6f9ba --- /dev/null +++ b/src/Routing.php @@ -0,0 +1,97 @@ +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); + }); + } + } + }); + } +}