UnderCurrent/app/Controller/HelloWorldController.php

75 lines
1.9 KiB
PHP
Raw Permalink Normal View History

Expand the response with headers and set status code diff --git a/app/Controller/BaseController.php b/app/Controller/BaseController.php deleted file mode 100644 index 8e24148..0000000 --- a/app/Controller/BaseController.php +++ /dev/null @@ -1,16 +0,0 @@ -<?php - -namespace App\Controller; - -use Ardent\Undercurrent\Attribute\Route; -use Ardent\Undercurrent\Http\GenericResponse; -use Ardent\Undercurrent\Http\ResponseInterface; - -class BaseController -{ - #[Route('/hello')] - public function helloWorld(): ResponseInterface - { - return new GenericResponse('Hello World!'); - } -} \ No newline at end of file diff --git a/app/Controller/HelloWorldController.php b/app/Controller/HelloWorldController.php new file mode 100644 index 0000000..8361e9d --- /dev/null +++ b/app/Controller/HelloWorldController.php @@ -0,0 +1,29 @@ +<?php + +namespace App\Controller; + +use Ardent\Undercurrent\Attribute\Route; +use Ardent\Undercurrent\Http\GenericResponse; +use Ardent\Undercurrent\Http\ResponseInterface; +use Ardent\Undercurrent\Http\StatusEnum; + +class HelloWorldController +{ + #[Route('/')] + public function index(): ResponseInterface + { + return new GenericResponse('Index, <a href="/hello">Hello</a>'); + } + + #[Route('/error')] + public function error(): ResponseInterface + { + return new GenericResponse('error', StatusEnum::NOT_FOUND); + } + + #[Route('/hello')] + public function hello(): ResponseInterface + { + return new GenericResponse('Hello World!'); + } +} \ No newline at end of file diff --git a/app/Kernel.php b/app/Kernel.php index a365d39..8b28577 100644 --- a/app/Kernel.php +++ b/app/Kernel.php @@ -2,7 +2,7 @@ namespace App; -use App\Controller\BaseController; +use App\Controller\HelloWorldController; use Ardent\Undercurrent\Container\ContainerInterface; use Ardent\Undercurrent\Kernel\BaseKernel; @@ -11,7 +11,7 @@ class Kernel extends BaseKernel protected function dependencies(ContainerInterface $container): void { $this->addControllers($container, [ - BaseController::class, + HelloWorldController::class, ]); } } \ No newline at end of file diff --git a/src/Container/ContainerInterface.php b/src/Container/ContainerInterface.php index bd50325..7b55a94 100644 --- a/src/Container/ContainerInterface.php +++ b/src/Container/ContainerInterface.php @@ -16,7 +16,7 @@ interface ContainerInterface * @template TClassName * @param class-string<TClassName> $className * @return TClassName - * @throws Exception + * @throws ClassNotFoundException */ public function get(string $className): object; } \ No newline at end of file diff --git a/src/Http/GenericResponse.php b/src/Http/GenericResponse.php index d80d350..18852ff 100644 --- a/src/Http/GenericResponse.php +++ b/src/Http/GenericResponse.php @@ -7,6 +7,7 @@ class GenericResponse implements ResponseInterface public function __construct( private readonly string $body, private readonly StatusEnum $status = StatusEnum::OK, + private readonly array $headers = [], ) { } @@ -20,4 +21,12 @@ class GenericResponse implements ResponseInterface { return $this->body; } + + /** + * @inheritDoc + */ + public function getHeaders(): array + { + return $this->headers; + } } \ No newline at end of file diff --git a/src/Http/GenericRouter.php b/src/Http/GenericRouter.php index 73ce8f1..b2d8e1d 100644 --- a/src/Http/GenericRouter.php +++ b/src/Http/GenericRouter.php @@ -2,9 +2,7 @@ namespace Ardent\Undercurrent\Http; -use App\Controller\BaseController; -use Ardent\Undercurrent\Attribute\Route; -use Ardent\Undercurrent\Collector\ClassAttributeCollector; +use Ardent\Undercurrent\Container\ClassNotFoundException; use Ardent\Undercurrent\Container\ContainerInterface; class GenericRouter implements RouterInterface @@ -16,6 +14,9 @@ class GenericRouter implements RouterInterface { } + /** + * @throws RouteNotFoundException|ClassNotFoundException + */ public function dispatch(RequestInterface $request): ResponseInterface { foreach ($this->config->getRoutes() as $route) { diff --git a/src/Http/ResponseInterface.php b/src/Http/ResponseInterface.php index abdc290..8047eb9 100644 --- a/src/Http/ResponseInterface.php +++ b/src/Http/ResponseInterface.php @@ -7,4 +7,9 @@ interface ResponseInterface public function getStatus(): StatusEnum; public function getBody(): string; + + /** + * @return array<string> + */ + public function getHeaders(): array; } \ No newline at end of file diff --git a/src/Kernel/BaseKernel.php b/src/Kernel/BaseKernel.php index 7122dcc..634cafc 100644 --- a/src/Kernel/BaseKernel.php +++ b/src/Kernel/BaseKernel.php @@ -33,9 +33,16 @@ class BaseKernel $_SERVER['REQUEST_URI'], $_REQUEST, ); - $router = $container->get(RouterInterface::class); - echo $router->dispatch($request)->getBody(); + $response = $router->dispatch($request); + + http_response_code($response->getStatus()->value); + + foreach ($response->getHeaders() as $header) { + header($header); + } + + echo $response->getBody(); } protected function addControllers(ContainerInterface $container, array $controllers): void
2023-08-08 17:18:18 +02:00
<?php
namespace App\Controller;
use App\Entity\Book;
2023-08-15 13:56:04 +02:00
use App\View\RouteView;
Expand the response with headers and set status code diff --git a/app/Controller/BaseController.php b/app/Controller/BaseController.php deleted file mode 100644 index 8e24148..0000000 --- a/app/Controller/BaseController.php +++ /dev/null @@ -1,16 +0,0 @@ -<?php - -namespace App\Controller; - -use Ardent\Undercurrent\Attribute\Route; -use Ardent\Undercurrent\Http\GenericResponse; -use Ardent\Undercurrent\Http\ResponseInterface; - -class BaseController -{ - #[Route('/hello')] - public function helloWorld(): ResponseInterface - { - return new GenericResponse('Hello World!'); - } -} \ No newline at end of file diff --git a/app/Controller/HelloWorldController.php b/app/Controller/HelloWorldController.php new file mode 100644 index 0000000..8361e9d --- /dev/null +++ b/app/Controller/HelloWorldController.php @@ -0,0 +1,29 @@ +<?php + +namespace App\Controller; + +use Ardent\Undercurrent\Attribute\Route; +use Ardent\Undercurrent\Http\GenericResponse; +use Ardent\Undercurrent\Http\ResponseInterface; +use Ardent\Undercurrent\Http\StatusEnum; + +class HelloWorldController +{ + #[Route('/')] + public function index(): ResponseInterface + { + return new GenericResponse('Index, <a href="/hello">Hello</a>'); + } + + #[Route('/error')] + public function error(): ResponseInterface + { + return new GenericResponse('error', StatusEnum::NOT_FOUND); + } + + #[Route('/hello')] + public function hello(): ResponseInterface + { + return new GenericResponse('Hello World!'); + } +} \ No newline at end of file diff --git a/app/Kernel.php b/app/Kernel.php index a365d39..8b28577 100644 --- a/app/Kernel.php +++ b/app/Kernel.php @@ -2,7 +2,7 @@ namespace App; -use App\Controller\BaseController; +use App\Controller\HelloWorldController; use Ardent\Undercurrent\Container\ContainerInterface; use Ardent\Undercurrent\Kernel\BaseKernel; @@ -11,7 +11,7 @@ class Kernel extends BaseKernel protected function dependencies(ContainerInterface $container): void { $this->addControllers($container, [ - BaseController::class, + HelloWorldController::class, ]); } } \ No newline at end of file diff --git a/src/Container/ContainerInterface.php b/src/Container/ContainerInterface.php index bd50325..7b55a94 100644 --- a/src/Container/ContainerInterface.php +++ b/src/Container/ContainerInterface.php @@ -16,7 +16,7 @@ interface ContainerInterface * @template TClassName * @param class-string<TClassName> $className * @return TClassName - * @throws Exception + * @throws ClassNotFoundException */ public function get(string $className): object; } \ No newline at end of file diff --git a/src/Http/GenericResponse.php b/src/Http/GenericResponse.php index d80d350..18852ff 100644 --- a/src/Http/GenericResponse.php +++ b/src/Http/GenericResponse.php @@ -7,6 +7,7 @@ class GenericResponse implements ResponseInterface public function __construct( private readonly string $body, private readonly StatusEnum $status = StatusEnum::OK, + private readonly array $headers = [], ) { } @@ -20,4 +21,12 @@ class GenericResponse implements ResponseInterface { return $this->body; } + + /** + * @inheritDoc + */ + public function getHeaders(): array + { + return $this->headers; + } } \ No newline at end of file diff --git a/src/Http/GenericRouter.php b/src/Http/GenericRouter.php index 73ce8f1..b2d8e1d 100644 --- a/src/Http/GenericRouter.php +++ b/src/Http/GenericRouter.php @@ -2,9 +2,7 @@ namespace Ardent\Undercurrent\Http; -use App\Controller\BaseController; -use Ardent\Undercurrent\Attribute\Route; -use Ardent\Undercurrent\Collector\ClassAttributeCollector; +use Ardent\Undercurrent\Container\ClassNotFoundException; use Ardent\Undercurrent\Container\ContainerInterface; class GenericRouter implements RouterInterface @@ -16,6 +14,9 @@ class GenericRouter implements RouterInterface { } + /** + * @throws RouteNotFoundException|ClassNotFoundException + */ public function dispatch(RequestInterface $request): ResponseInterface { foreach ($this->config->getRoutes() as $route) { diff --git a/src/Http/ResponseInterface.php b/src/Http/ResponseInterface.php index abdc290..8047eb9 100644 --- a/src/Http/ResponseInterface.php +++ b/src/Http/ResponseInterface.php @@ -7,4 +7,9 @@ interface ResponseInterface public function getStatus(): StatusEnum; public function getBody(): string; + + /** + * @return array<string> + */ + public function getHeaders(): array; } \ No newline at end of file diff --git a/src/Kernel/BaseKernel.php b/src/Kernel/BaseKernel.php index 7122dcc..634cafc 100644 --- a/src/Kernel/BaseKernel.php +++ b/src/Kernel/BaseKernel.php @@ -33,9 +33,16 @@ class BaseKernel $_SERVER['REQUEST_URI'], $_REQUEST, ); - $router = $container->get(RouterInterface::class); - echo $router->dispatch($request)->getBody(); + $response = $router->dispatch($request); + + http_response_code($response->getStatus()->value); + + foreach ($response->getHeaders() as $header) { + header($header); + } + + echo $response->getBody(); } protected function addControllers(ContainerInterface $container, array $controllers): void
2023-08-08 17:18:18 +02:00
use Ardent\Undercurrent\Attribute\Route;
use Ardent\Undercurrent\Http\GenericResponse;
use Ardent\Undercurrent\Http\ResponseInterface;
2023-08-16 16:04:35 +02:00
use Ardent\Undercurrent\Http\RoutesConfig;
Expand the response with headers and set status code diff --git a/app/Controller/BaseController.php b/app/Controller/BaseController.php deleted file mode 100644 index 8e24148..0000000 --- a/app/Controller/BaseController.php +++ /dev/null @@ -1,16 +0,0 @@ -<?php - -namespace App\Controller; - -use Ardent\Undercurrent\Attribute\Route; -use Ardent\Undercurrent\Http\GenericResponse; -use Ardent\Undercurrent\Http\ResponseInterface; - -class BaseController -{ - #[Route('/hello')] - public function helloWorld(): ResponseInterface - { - return new GenericResponse('Hello World!'); - } -} \ No newline at end of file diff --git a/app/Controller/HelloWorldController.php b/app/Controller/HelloWorldController.php new file mode 100644 index 0000000..8361e9d --- /dev/null +++ b/app/Controller/HelloWorldController.php @@ -0,0 +1,29 @@ +<?php + +namespace App\Controller; + +use Ardent\Undercurrent\Attribute\Route; +use Ardent\Undercurrent\Http\GenericResponse; +use Ardent\Undercurrent\Http\ResponseInterface; +use Ardent\Undercurrent\Http\StatusEnum; + +class HelloWorldController +{ + #[Route('/')] + public function index(): ResponseInterface + { + return new GenericResponse('Index, <a href="/hello">Hello</a>'); + } + + #[Route('/error')] + public function error(): ResponseInterface + { + return new GenericResponse('error', StatusEnum::NOT_FOUND); + } + + #[Route('/hello')] + public function hello(): ResponseInterface + { + return new GenericResponse('Hello World!'); + } +} \ No newline at end of file diff --git a/app/Kernel.php b/app/Kernel.php index a365d39..8b28577 100644 --- a/app/Kernel.php +++ b/app/Kernel.php @@ -2,7 +2,7 @@ namespace App; -use App\Controller\BaseController; +use App\Controller\HelloWorldController; use Ardent\Undercurrent\Container\ContainerInterface; use Ardent\Undercurrent\Kernel\BaseKernel; @@ -11,7 +11,7 @@ class Kernel extends BaseKernel protected function dependencies(ContainerInterface $container): void { $this->addControllers($container, [ - BaseController::class, + HelloWorldController::class, ]); } } \ No newline at end of file diff --git a/src/Container/ContainerInterface.php b/src/Container/ContainerInterface.php index bd50325..7b55a94 100644 --- a/src/Container/ContainerInterface.php +++ b/src/Container/ContainerInterface.php @@ -16,7 +16,7 @@ interface ContainerInterface * @template TClassName * @param class-string<TClassName> $className * @return TClassName - * @throws Exception + * @throws ClassNotFoundException */ public function get(string $className): object; } \ No newline at end of file diff --git a/src/Http/GenericResponse.php b/src/Http/GenericResponse.php index d80d350..18852ff 100644 --- a/src/Http/GenericResponse.php +++ b/src/Http/GenericResponse.php @@ -7,6 +7,7 @@ class GenericResponse implements ResponseInterface public function __construct( private readonly string $body, private readonly StatusEnum $status = StatusEnum::OK, + private readonly array $headers = [], ) { } @@ -20,4 +21,12 @@ class GenericResponse implements ResponseInterface { return $this->body; } + + /** + * @inheritDoc + */ + public function getHeaders(): array + { + return $this->headers; + } } \ No newline at end of file diff --git a/src/Http/GenericRouter.php b/src/Http/GenericRouter.php index 73ce8f1..b2d8e1d 100644 --- a/src/Http/GenericRouter.php +++ b/src/Http/GenericRouter.php @@ -2,9 +2,7 @@ namespace Ardent\Undercurrent\Http; -use App\Controller\BaseController; -use Ardent\Undercurrent\Attribute\Route; -use Ardent\Undercurrent\Collector\ClassAttributeCollector; +use Ardent\Undercurrent\Container\ClassNotFoundException; use Ardent\Undercurrent\Container\ContainerInterface; class GenericRouter implements RouterInterface @@ -16,6 +14,9 @@ class GenericRouter implements RouterInterface { } + /** + * @throws RouteNotFoundException|ClassNotFoundException + */ public function dispatch(RequestInterface $request): ResponseInterface { foreach ($this->config->getRoutes() as $route) { diff --git a/src/Http/ResponseInterface.php b/src/Http/ResponseInterface.php index abdc290..8047eb9 100644 --- a/src/Http/ResponseInterface.php +++ b/src/Http/ResponseInterface.php @@ -7,4 +7,9 @@ interface ResponseInterface public function getStatus(): StatusEnum; public function getBody(): string; + + /** + * @return array<string> + */ + public function getHeaders(): array; } \ No newline at end of file diff --git a/src/Kernel/BaseKernel.php b/src/Kernel/BaseKernel.php index 7122dcc..634cafc 100644 --- a/src/Kernel/BaseKernel.php +++ b/src/Kernel/BaseKernel.php @@ -33,9 +33,16 @@ class BaseKernel $_SERVER['REQUEST_URI'], $_REQUEST, ); - $router = $container->get(RouterInterface::class); - echo $router->dispatch($request)->getBody(); + $response = $router->dispatch($request); + + http_response_code($response->getStatus()->value); + + foreach ($response->getHeaders() as $header) { + header($header); + } + + echo $response->getBody(); } protected function addControllers(ContainerInterface $container, array $controllers): void
2023-08-08 17:18:18 +02:00
use Ardent\Undercurrent\Http\StatusEnum;
use Ardent\Undercurrent\View\BaseView;
use Ardent\Undercurrent\View\ViewInterface;
use Doctrine\ORM\EntityManager;
Expand the response with headers and set status code diff --git a/app/Controller/BaseController.php b/app/Controller/BaseController.php deleted file mode 100644 index 8e24148..0000000 --- a/app/Controller/BaseController.php +++ /dev/null @@ -1,16 +0,0 @@ -<?php - -namespace App\Controller; - -use Ardent\Undercurrent\Attribute\Route; -use Ardent\Undercurrent\Http\GenericResponse; -use Ardent\Undercurrent\Http\ResponseInterface; - -class BaseController -{ - #[Route('/hello')] - public function helloWorld(): ResponseInterface - { - return new GenericResponse('Hello World!'); - } -} \ No newline at end of file diff --git a/app/Controller/HelloWorldController.php b/app/Controller/HelloWorldController.php new file mode 100644 index 0000000..8361e9d --- /dev/null +++ b/app/Controller/HelloWorldController.php @@ -0,0 +1,29 @@ +<?php + +namespace App\Controller; + +use Ardent\Undercurrent\Attribute\Route; +use Ardent\Undercurrent\Http\GenericResponse; +use Ardent\Undercurrent\Http\ResponseInterface; +use Ardent\Undercurrent\Http\StatusEnum; + +class HelloWorldController +{ + #[Route('/')] + public function index(): ResponseInterface + { + return new GenericResponse('Index, <a href="/hello">Hello</a>'); + } + + #[Route('/error')] + public function error(): ResponseInterface + { + return new GenericResponse('error', StatusEnum::NOT_FOUND); + } + + #[Route('/hello')] + public function hello(): ResponseInterface + { + return new GenericResponse('Hello World!'); + } +} \ No newline at end of file diff --git a/app/Kernel.php b/app/Kernel.php index a365d39..8b28577 100644 --- a/app/Kernel.php +++ b/app/Kernel.php @@ -2,7 +2,7 @@ namespace App; -use App\Controller\BaseController; +use App\Controller\HelloWorldController; use Ardent\Undercurrent\Container\ContainerInterface; use Ardent\Undercurrent\Kernel\BaseKernel; @@ -11,7 +11,7 @@ class Kernel extends BaseKernel protected function dependencies(ContainerInterface $container): void { $this->addControllers($container, [ - BaseController::class, + HelloWorldController::class, ]); } } \ No newline at end of file diff --git a/src/Container/ContainerInterface.php b/src/Container/ContainerInterface.php index bd50325..7b55a94 100644 --- a/src/Container/ContainerInterface.php +++ b/src/Container/ContainerInterface.php @@ -16,7 +16,7 @@ interface ContainerInterface * @template TClassName * @param class-string<TClassName> $className * @return TClassName - * @throws Exception + * @throws ClassNotFoundException */ public function get(string $className): object; } \ No newline at end of file diff --git a/src/Http/GenericResponse.php b/src/Http/GenericResponse.php index d80d350..18852ff 100644 --- a/src/Http/GenericResponse.php +++ b/src/Http/GenericResponse.php @@ -7,6 +7,7 @@ class GenericResponse implements ResponseInterface public function __construct( private readonly string $body, private readonly StatusEnum $status = StatusEnum::OK, + private readonly array $headers = [], ) { } @@ -20,4 +21,12 @@ class GenericResponse implements ResponseInterface { return $this->body; } + + /** + * @inheritDoc + */ + public function getHeaders(): array + { + return $this->headers; + } } \ No newline at end of file diff --git a/src/Http/GenericRouter.php b/src/Http/GenericRouter.php index 73ce8f1..b2d8e1d 100644 --- a/src/Http/GenericRouter.php +++ b/src/Http/GenericRouter.php @@ -2,9 +2,7 @@ namespace Ardent\Undercurrent\Http; -use App\Controller\BaseController; -use Ardent\Undercurrent\Attribute\Route; -use Ardent\Undercurrent\Collector\ClassAttributeCollector; +use Ardent\Undercurrent\Container\ClassNotFoundException; use Ardent\Undercurrent\Container\ContainerInterface; class GenericRouter implements RouterInterface @@ -16,6 +14,9 @@ class GenericRouter implements RouterInterface { } + /** + * @throws RouteNotFoundException|ClassNotFoundException + */ public function dispatch(RequestInterface $request): ResponseInterface { foreach ($this->config->getRoutes() as $route) { diff --git a/src/Http/ResponseInterface.php b/src/Http/ResponseInterface.php index abdc290..8047eb9 100644 --- a/src/Http/ResponseInterface.php +++ b/src/Http/ResponseInterface.php @@ -7,4 +7,9 @@ interface ResponseInterface public function getStatus(): StatusEnum; public function getBody(): string; + + /** + * @return array<string> + */ + public function getHeaders(): array; } \ No newline at end of file diff --git a/src/Kernel/BaseKernel.php b/src/Kernel/BaseKernel.php index 7122dcc..634cafc 100644 --- a/src/Kernel/BaseKernel.php +++ b/src/Kernel/BaseKernel.php @@ -33,9 +33,16 @@ class BaseKernel $_SERVER['REQUEST_URI'], $_REQUEST, ); - $router = $container->get(RouterInterface::class); - echo $router->dispatch($request)->getBody(); + $response = $router->dispatch($request); + + http_response_code($response->getStatus()->value); + + foreach ($response->getHeaders() as $header) { + header($header); + } + + echo $response->getBody(); } protected function addControllers(ContainerInterface $container, array $controllers): void
2023-08-08 17:18:18 +02:00
class HelloWorldController
{
#[Route('/')]
public function index(): ViewInterface
Expand the response with headers and set status code diff --git a/app/Controller/BaseController.php b/app/Controller/BaseController.php deleted file mode 100644 index 8e24148..0000000 --- a/app/Controller/BaseController.php +++ /dev/null @@ -1,16 +0,0 @@ -<?php - -namespace App\Controller; - -use Ardent\Undercurrent\Attribute\Route; -use Ardent\Undercurrent\Http\GenericResponse; -use Ardent\Undercurrent\Http\ResponseInterface; - -class BaseController -{ - #[Route('/hello')] - public function helloWorld(): ResponseInterface - { - return new GenericResponse('Hello World!'); - } -} \ No newline at end of file diff --git a/app/Controller/HelloWorldController.php b/app/Controller/HelloWorldController.php new file mode 100644 index 0000000..8361e9d --- /dev/null +++ b/app/Controller/HelloWorldController.php @@ -0,0 +1,29 @@ +<?php + +namespace App\Controller; + +use Ardent\Undercurrent\Attribute\Route; +use Ardent\Undercurrent\Http\GenericResponse; +use Ardent\Undercurrent\Http\ResponseInterface; +use Ardent\Undercurrent\Http\StatusEnum; + +class HelloWorldController +{ + #[Route('/')] + public function index(): ResponseInterface + { + return new GenericResponse('Index, <a href="/hello">Hello</a>'); + } + + #[Route('/error')] + public function error(): ResponseInterface + { + return new GenericResponse('error', StatusEnum::NOT_FOUND); + } + + #[Route('/hello')] + public function hello(): ResponseInterface + { + return new GenericResponse('Hello World!'); + } +} \ No newline at end of file diff --git a/app/Kernel.php b/app/Kernel.php index a365d39..8b28577 100644 --- a/app/Kernel.php +++ b/app/Kernel.php @@ -2,7 +2,7 @@ namespace App; -use App\Controller\BaseController; +use App\Controller\HelloWorldController; use Ardent\Undercurrent\Container\ContainerInterface; use Ardent\Undercurrent\Kernel\BaseKernel; @@ -11,7 +11,7 @@ class Kernel extends BaseKernel protected function dependencies(ContainerInterface $container): void { $this->addControllers($container, [ - BaseController::class, + HelloWorldController::class, ]); } } \ No newline at end of file diff --git a/src/Container/ContainerInterface.php b/src/Container/ContainerInterface.php index bd50325..7b55a94 100644 --- a/src/Container/ContainerInterface.php +++ b/src/Container/ContainerInterface.php @@ -16,7 +16,7 @@ interface ContainerInterface * @template TClassName * @param class-string<TClassName> $className * @return TClassName - * @throws Exception + * @throws ClassNotFoundException */ public function get(string $className): object; } \ No newline at end of file diff --git a/src/Http/GenericResponse.php b/src/Http/GenericResponse.php index d80d350..18852ff 100644 --- a/src/Http/GenericResponse.php +++ b/src/Http/GenericResponse.php @@ -7,6 +7,7 @@ class GenericResponse implements ResponseInterface public function __construct( private readonly string $body, private readonly StatusEnum $status = StatusEnum::OK, + private readonly array $headers = [], ) { } @@ -20,4 +21,12 @@ class GenericResponse implements ResponseInterface { return $this->body; } + + /** + * @inheritDoc + */ + public function getHeaders(): array + { + return $this->headers; + } } \ No newline at end of file diff --git a/src/Http/GenericRouter.php b/src/Http/GenericRouter.php index 73ce8f1..b2d8e1d 100644 --- a/src/Http/GenericRouter.php +++ b/src/Http/GenericRouter.php @@ -2,9 +2,7 @@ namespace Ardent\Undercurrent\Http; -use App\Controller\BaseController; -use Ardent\Undercurrent\Attribute\Route; -use Ardent\Undercurrent\Collector\ClassAttributeCollector; +use Ardent\Undercurrent\Container\ClassNotFoundException; use Ardent\Undercurrent\Container\ContainerInterface; class GenericRouter implements RouterInterface @@ -16,6 +14,9 @@ class GenericRouter implements RouterInterface { } + /** + * @throws RouteNotFoundException|ClassNotFoundException + */ public function dispatch(RequestInterface $request): ResponseInterface { foreach ($this->config->getRoutes() as $route) { diff --git a/src/Http/ResponseInterface.php b/src/Http/ResponseInterface.php index abdc290..8047eb9 100644 --- a/src/Http/ResponseInterface.php +++ b/src/Http/ResponseInterface.php @@ -7,4 +7,9 @@ interface ResponseInterface public function getStatus(): StatusEnum; public function getBody(): string; + + /** + * @return array<string> + */ + public function getHeaders(): array; } \ No newline at end of file diff --git a/src/Kernel/BaseKernel.php b/src/Kernel/BaseKernel.php index 7122dcc..634cafc 100644 --- a/src/Kernel/BaseKernel.php +++ b/src/Kernel/BaseKernel.php @@ -33,9 +33,16 @@ class BaseKernel $_SERVER['REQUEST_URI'], $_REQUEST, ); - $router = $container->get(RouterInterface::class); - echo $router->dispatch($request)->getBody(); + $response = $router->dispatch($request); + + http_response_code($response->getStatus()->value); + + foreach ($response->getHeaders() as $header) { + header($header); + } + + echo $response->getBody(); } protected function addControllers(ContainerInterface $container, array $controllers): void
2023-08-08 17:18:18 +02:00
{
2023-08-15 13:37:03 +02:00
return new BaseView('/index');
Expand the response with headers and set status code diff --git a/app/Controller/BaseController.php b/app/Controller/BaseController.php deleted file mode 100644 index 8e24148..0000000 --- a/app/Controller/BaseController.php +++ /dev/null @@ -1,16 +0,0 @@ -<?php - -namespace App\Controller; - -use Ardent\Undercurrent\Attribute\Route; -use Ardent\Undercurrent\Http\GenericResponse; -use Ardent\Undercurrent\Http\ResponseInterface; - -class BaseController -{ - #[Route('/hello')] - public function helloWorld(): ResponseInterface - { - return new GenericResponse('Hello World!'); - } -} \ No newline at end of file diff --git a/app/Controller/HelloWorldController.php b/app/Controller/HelloWorldController.php new file mode 100644 index 0000000..8361e9d --- /dev/null +++ b/app/Controller/HelloWorldController.php @@ -0,0 +1,29 @@ +<?php + +namespace App\Controller; + +use Ardent\Undercurrent\Attribute\Route; +use Ardent\Undercurrent\Http\GenericResponse; +use Ardent\Undercurrent\Http\ResponseInterface; +use Ardent\Undercurrent\Http\StatusEnum; + +class HelloWorldController +{ + #[Route('/')] + public function index(): ResponseInterface + { + return new GenericResponse('Index, <a href="/hello">Hello</a>'); + } + + #[Route('/error')] + public function error(): ResponseInterface + { + return new GenericResponse('error', StatusEnum::NOT_FOUND); + } + + #[Route('/hello')] + public function hello(): ResponseInterface + { + return new GenericResponse('Hello World!'); + } +} \ No newline at end of file diff --git a/app/Kernel.php b/app/Kernel.php index a365d39..8b28577 100644 --- a/app/Kernel.php +++ b/app/Kernel.php @@ -2,7 +2,7 @@ namespace App; -use App\Controller\BaseController; +use App\Controller\HelloWorldController; use Ardent\Undercurrent\Container\ContainerInterface; use Ardent\Undercurrent\Kernel\BaseKernel; @@ -11,7 +11,7 @@ class Kernel extends BaseKernel protected function dependencies(ContainerInterface $container): void { $this->addControllers($container, [ - BaseController::class, + HelloWorldController::class, ]); } } \ No newline at end of file diff --git a/src/Container/ContainerInterface.php b/src/Container/ContainerInterface.php index bd50325..7b55a94 100644 --- a/src/Container/ContainerInterface.php +++ b/src/Container/ContainerInterface.php @@ -16,7 +16,7 @@ interface ContainerInterface * @template TClassName * @param class-string<TClassName> $className * @return TClassName - * @throws Exception + * @throws ClassNotFoundException */ public function get(string $className): object; } \ No newline at end of file diff --git a/src/Http/GenericResponse.php b/src/Http/GenericResponse.php index d80d350..18852ff 100644 --- a/src/Http/GenericResponse.php +++ b/src/Http/GenericResponse.php @@ -7,6 +7,7 @@ class GenericResponse implements ResponseInterface public function __construct( private readonly string $body, private readonly StatusEnum $status = StatusEnum::OK, + private readonly array $headers = [], ) { } @@ -20,4 +21,12 @@ class GenericResponse implements ResponseInterface { return $this->body; } + + /** + * @inheritDoc + */ + public function getHeaders(): array + { + return $this->headers; + } } \ No newline at end of file diff --git a/src/Http/GenericRouter.php b/src/Http/GenericRouter.php index 73ce8f1..b2d8e1d 100644 --- a/src/Http/GenericRouter.php +++ b/src/Http/GenericRouter.php @@ -2,9 +2,7 @@ namespace Ardent\Undercurrent\Http; -use App\Controller\BaseController; -use Ardent\Undercurrent\Attribute\Route; -use Ardent\Undercurrent\Collector\ClassAttributeCollector; +use Ardent\Undercurrent\Container\ClassNotFoundException; use Ardent\Undercurrent\Container\ContainerInterface; class GenericRouter implements RouterInterface @@ -16,6 +14,9 @@ class GenericRouter implements RouterInterface { } + /** + * @throws RouteNotFoundException|ClassNotFoundException + */ public function dispatch(RequestInterface $request): ResponseInterface { foreach ($this->config->getRoutes() as $route) { diff --git a/src/Http/ResponseInterface.php b/src/Http/ResponseInterface.php index abdc290..8047eb9 100644 --- a/src/Http/ResponseInterface.php +++ b/src/Http/ResponseInterface.php @@ -7,4 +7,9 @@ interface ResponseInterface public function getStatus(): StatusEnum; public function getBody(): string; + + /** + * @return array<string> + */ + public function getHeaders(): array; } \ No newline at end of file diff --git a/src/Kernel/BaseKernel.php b/src/Kernel/BaseKernel.php index 7122dcc..634cafc 100644 --- a/src/Kernel/BaseKernel.php +++ b/src/Kernel/BaseKernel.php @@ -33,9 +33,16 @@ class BaseKernel $_SERVER['REQUEST_URI'], $_REQUEST, ); - $router = $container->get(RouterInterface::class); - echo $router->dispatch($request)->getBody(); + $response = $router->dispatch($request); + + http_response_code($response->getStatus()->value); + + foreach ($response->getHeaders() as $header) { + header($header); + } + + echo $response->getBody(); } protected function addControllers(ContainerInterface $container, array $controllers): void
2023-08-08 17:18:18 +02:00
}
#[Route('/error')]
public function error(): ResponseInterface
{
return new GenericResponse('error', StatusEnum::NOT_FOUND);
}
#[Route('/hello')]
public function hello(): ResponseInterface
{
return new GenericResponse('Hello World!');
}
Add controller method autowiring diff --git a/app/Controller/HelloWorldController.php b/app/Controller/HelloWorldController.php index 8361e9d..282a990 100644 --- a/app/Controller/HelloWorldController.php +++ b/app/Controller/HelloWorldController.php @@ -5,6 +5,7 @@ namespace App\Controller; use Ardent\Undercurrent\Attribute\Route; use Ardent\Undercurrent\Http\GenericResponse; use Ardent\Undercurrent\Http\ResponseInterface; +use Ardent\Undercurrent\Http\RouterConfig; use Ardent\Undercurrent\Http\StatusEnum; class HelloWorldController @@ -26,4 +27,21 @@ class HelloWorldController { return new GenericResponse('Hello World!'); } + + #[Route('/routes')] + public function routes(RouterConfig $config): ResponseInterface + { + $routes = implode('<br>', array_map( + fn($route) => $route->getRoute()->path, + $config->getRoutes() + )); + + return new GenericResponse($routes); + } + + #[Route('/world/{name}')] + public function world(string $name): ResponseInterface + { + return new GenericResponse("Hello $name!"); + } } \ No newline at end of file diff --git a/src/Http/GenericRouter.php b/src/Http/GenericRouter.php index b2d8e1d..23e9311 100644 --- a/src/Http/GenericRouter.php +++ b/src/Http/GenericRouter.php @@ -4,6 +4,8 @@ namespace Ardent\Undercurrent\Http; use Ardent\Undercurrent\Container\ClassNotFoundException; use Ardent\Undercurrent\Container\ContainerInterface; +use Exception; +use ReflectionMethod; class GenericRouter implements RouterInterface { @@ -23,10 +25,26 @@ class GenericRouter implements RouterInterface if ($route->getRoute()->path === $request->getUri()) { $controller = $this->container->get($route->getController()); $method = $route->getMethod(); - return $controller->$method(); + return $controller->$method(...$this->autowire($route)); } } throw new RouteNotFoundException($request); } + + private function autowire(RouteConfig $route): array + { + $reflectionMethod = new ReflectionMethod($route->getController(), $route->getMethod()); + + $params = []; + foreach ($reflectionMethod->getParameters() as $parameter) { + $type = $parameter->getType(); + if (!$type) { + throw new Exception('Cannot autowire parameter without type'); + } + $params[] = $this->container->get($type->getName()); + } + + return $params; + } } \ No newline at end of file
2023-08-09 00:49:47 +02:00
#[Route('/view/{name}')]
public function view(string $name): ViewInterface
{
2023-08-15 13:37:03 +02:00
return new BaseView('/home', ['name' => $name]);
}
2023-08-15 13:56:04 +02:00
#[Route('/routes')]
2023-08-16 16:04:35 +02:00
public function routes(RoutesConfig $config): ViewInterface
Add controller method autowiring diff --git a/app/Controller/HelloWorldController.php b/app/Controller/HelloWorldController.php index 8361e9d..282a990 100644 --- a/app/Controller/HelloWorldController.php +++ b/app/Controller/HelloWorldController.php @@ -5,6 +5,7 @@ namespace App\Controller; use Ardent\Undercurrent\Attribute\Route; use Ardent\Undercurrent\Http\GenericResponse; use Ardent\Undercurrent\Http\ResponseInterface; +use Ardent\Undercurrent\Http\RouterConfig; use Ardent\Undercurrent\Http\StatusEnum; class HelloWorldController @@ -26,4 +27,21 @@ class HelloWorldController { return new GenericResponse('Hello World!'); } + + #[Route('/routes')] + public function routes(RouterConfig $config): ResponseInterface + { + $routes = implode('<br>', array_map( + fn($route) => $route->getRoute()->path, + $config->getRoutes() + )); + + return new GenericResponse($routes); + } + + #[Route('/world/{name}')] + public function world(string $name): ResponseInterface + { + return new GenericResponse("Hello $name!"); + } } \ No newline at end of file diff --git a/src/Http/GenericRouter.php b/src/Http/GenericRouter.php index b2d8e1d..23e9311 100644 --- a/src/Http/GenericRouter.php +++ b/src/Http/GenericRouter.php @@ -4,6 +4,8 @@ namespace Ardent\Undercurrent\Http; use Ardent\Undercurrent\Container\ClassNotFoundException; use Ardent\Undercurrent\Container\ContainerInterface; +use Exception; +use ReflectionMethod; class GenericRouter implements RouterInterface { @@ -23,10 +25,26 @@ class GenericRouter implements RouterInterface if ($route->getRoute()->path === $request->getUri()) { $controller = $this->container->get($route->getController()); $method = $route->getMethod(); - return $controller->$method(); + return $controller->$method(...$this->autowire($route)); } } throw new RouteNotFoundException($request); } + + private function autowire(RouteConfig $route): array + { + $reflectionMethod = new ReflectionMethod($route->getController(), $route->getMethod()); + + $params = []; + foreach ($reflectionMethod->getParameters() as $parameter) { + $type = $parameter->getType(); + if (!$type) { + throw new Exception('Cannot autowire parameter without type'); + } + $params[] = $this->container->get($type->getName()); + } + + return $params; + } } \ No newline at end of file
2023-08-09 00:49:47 +02:00
{
2023-08-15 13:56:04 +02:00
return new RouteView($config);
Add controller method autowiring diff --git a/app/Controller/HelloWorldController.php b/app/Controller/HelloWorldController.php index 8361e9d..282a990 100644 --- a/app/Controller/HelloWorldController.php +++ b/app/Controller/HelloWorldController.php @@ -5,6 +5,7 @@ namespace App\Controller; use Ardent\Undercurrent\Attribute\Route; use Ardent\Undercurrent\Http\GenericResponse; use Ardent\Undercurrent\Http\ResponseInterface; +use Ardent\Undercurrent\Http\RouterConfig; use Ardent\Undercurrent\Http\StatusEnum; class HelloWorldController @@ -26,4 +27,21 @@ class HelloWorldController { return new GenericResponse('Hello World!'); } + + #[Route('/routes')] + public function routes(RouterConfig $config): ResponseInterface + { + $routes = implode('<br>', array_map( + fn($route) => $route->getRoute()->path, + $config->getRoutes() + )); + + return new GenericResponse($routes); + } + + #[Route('/world/{name}')] + public function world(string $name): ResponseInterface + { + return new GenericResponse("Hello $name!"); + } } \ No newline at end of file diff --git a/src/Http/GenericRouter.php b/src/Http/GenericRouter.php index b2d8e1d..23e9311 100644 --- a/src/Http/GenericRouter.php +++ b/src/Http/GenericRouter.php @@ -4,6 +4,8 @@ namespace Ardent\Undercurrent\Http; use Ardent\Undercurrent\Container\ClassNotFoundException; use Ardent\Undercurrent\Container\ContainerInterface; +use Exception; +use ReflectionMethod; class GenericRouter implements RouterInterface { @@ -23,10 +25,26 @@ class GenericRouter implements RouterInterface if ($route->getRoute()->path === $request->getUri()) { $controller = $this->container->get($route->getController()); $method = $route->getMethod(); - return $controller->$method(); + return $controller->$method(...$this->autowire($route)); } } throw new RouteNotFoundException($request); } + + private function autowire(RouteConfig $route): array + { + $reflectionMethod = new ReflectionMethod($route->getController(), $route->getMethod()); + + $params = []; + foreach ($reflectionMethod->getParameters() as $parameter) { + $type = $parameter->getType(); + if (!$type) { + throw new Exception('Cannot autowire parameter without type'); + } + $params[] = $this->container->get($type->getName()); + } + + return $params; + } } \ No newline at end of file
2023-08-09 00:49:47 +02:00
}
#[Route('/world/{name}')]
public function world(string $name): ResponseInterface
{
return new GenericResponse("Hello $name!");
}
#[Route('/db')]
public function db(EntityManager $em): ResponseInterface
{
$repo = $em->getRepository(Book::class);
dump($repo->findAll());
return new GenericResponse("DB stuff");
}
2023-08-17 15:53:58 +02:00
#[Route('/create')]
public function createDb(EntityManager $em): ResponseInterface
{
$books = $em->getRepository(Book::class)->findAll();
$book = new Book();
$book->setTitle(sprintf('Book %d', count($books) + 1));
$em->persist($book);
$em->flush();
dump($book);
return new GenericResponse("DB stuff");
}
Expand the response with headers and set status code diff --git a/app/Controller/BaseController.php b/app/Controller/BaseController.php deleted file mode 100644 index 8e24148..0000000 --- a/app/Controller/BaseController.php +++ /dev/null @@ -1,16 +0,0 @@ -<?php - -namespace App\Controller; - -use Ardent\Undercurrent\Attribute\Route; -use Ardent\Undercurrent\Http\GenericResponse; -use Ardent\Undercurrent\Http\ResponseInterface; - -class BaseController -{ - #[Route('/hello')] - public function helloWorld(): ResponseInterface - { - return new GenericResponse('Hello World!'); - } -} \ No newline at end of file diff --git a/app/Controller/HelloWorldController.php b/app/Controller/HelloWorldController.php new file mode 100644 index 0000000..8361e9d --- /dev/null +++ b/app/Controller/HelloWorldController.php @@ -0,0 +1,29 @@ +<?php + +namespace App\Controller; + +use Ardent\Undercurrent\Attribute\Route; +use Ardent\Undercurrent\Http\GenericResponse; +use Ardent\Undercurrent\Http\ResponseInterface; +use Ardent\Undercurrent\Http\StatusEnum; + +class HelloWorldController +{ + #[Route('/')] + public function index(): ResponseInterface + { + return new GenericResponse('Index, <a href="/hello">Hello</a>'); + } + + #[Route('/error')] + public function error(): ResponseInterface + { + return new GenericResponse('error', StatusEnum::NOT_FOUND); + } + + #[Route('/hello')] + public function hello(): ResponseInterface + { + return new GenericResponse('Hello World!'); + } +} \ No newline at end of file diff --git a/app/Kernel.php b/app/Kernel.php index a365d39..8b28577 100644 --- a/app/Kernel.php +++ b/app/Kernel.php @@ -2,7 +2,7 @@ namespace App; -use App\Controller\BaseController; +use App\Controller\HelloWorldController; use Ardent\Undercurrent\Container\ContainerInterface; use Ardent\Undercurrent\Kernel\BaseKernel; @@ -11,7 +11,7 @@ class Kernel extends BaseKernel protected function dependencies(ContainerInterface $container): void { $this->addControllers($container, [ - BaseController::class, + HelloWorldController::class, ]); } } \ No newline at end of file diff --git a/src/Container/ContainerInterface.php b/src/Container/ContainerInterface.php index bd50325..7b55a94 100644 --- a/src/Container/ContainerInterface.php +++ b/src/Container/ContainerInterface.php @@ -16,7 +16,7 @@ interface ContainerInterface * @template TClassName * @param class-string<TClassName> $className * @return TClassName - * @throws Exception + * @throws ClassNotFoundException */ public function get(string $className): object; } \ No newline at end of file diff --git a/src/Http/GenericResponse.php b/src/Http/GenericResponse.php index d80d350..18852ff 100644 --- a/src/Http/GenericResponse.php +++ b/src/Http/GenericResponse.php @@ -7,6 +7,7 @@ class GenericResponse implements ResponseInterface public function __construct( private readonly string $body, private readonly StatusEnum $status = StatusEnum::OK, + private readonly array $headers = [], ) { } @@ -20,4 +21,12 @@ class GenericResponse implements ResponseInterface { return $this->body; } + + /** + * @inheritDoc + */ + public function getHeaders(): array + { + return $this->headers; + } } \ No newline at end of file diff --git a/src/Http/GenericRouter.php b/src/Http/GenericRouter.php index 73ce8f1..b2d8e1d 100644 --- a/src/Http/GenericRouter.php +++ b/src/Http/GenericRouter.php @@ -2,9 +2,7 @@ namespace Ardent\Undercurrent\Http; -use App\Controller\BaseController; -use Ardent\Undercurrent\Attribute\Route; -use Ardent\Undercurrent\Collector\ClassAttributeCollector; +use Ardent\Undercurrent\Container\ClassNotFoundException; use Ardent\Undercurrent\Container\ContainerInterface; class GenericRouter implements RouterInterface @@ -16,6 +14,9 @@ class GenericRouter implements RouterInterface { } + /** + * @throws RouteNotFoundException|ClassNotFoundException + */ public function dispatch(RequestInterface $request): ResponseInterface { foreach ($this->config->getRoutes() as $route) { diff --git a/src/Http/ResponseInterface.php b/src/Http/ResponseInterface.php index abdc290..8047eb9 100644 --- a/src/Http/ResponseInterface.php +++ b/src/Http/ResponseInterface.php @@ -7,4 +7,9 @@ interface ResponseInterface public function getStatus(): StatusEnum; public function getBody(): string; + + /** + * @return array<string> + */ + public function getHeaders(): array; } \ No newline at end of file diff --git a/src/Kernel/BaseKernel.php b/src/Kernel/BaseKernel.php index 7122dcc..634cafc 100644 --- a/src/Kernel/BaseKernel.php +++ b/src/Kernel/BaseKernel.php @@ -33,9 +33,16 @@ class BaseKernel $_SERVER['REQUEST_URI'], $_REQUEST, ); - $router = $container->get(RouterInterface::class); - echo $router->dispatch($request)->getBody(); + $response = $router->dispatch($request); + + http_response_code($response->getStatus()->value); + + foreach ($response->getHeaders() as $header) { + header($header); + } + + echo $response->getBody(); } protected function addControllers(ContainerInterface $container, array $controllers): void
2023-08-08 17:18:18 +02:00
}