From 5d6ad29db8d6856c4e7e705cb75de3e6c75ce8f7 Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 8 Aug 2023 17:18:18 +0200 Subject: [PATCH] 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 @@ -Hello'); + } + + #[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 $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 + */ + 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 --- app/Controller/BaseController.php | 16 -------------- app/Controller/HelloWorldController.php | 29 +++++++++++++++++++++++++ app/Kernel.php | 4 ++-- src/Container/ContainerInterface.php | 2 +- src/Http/GenericResponse.php | 9 ++++++++ src/Http/GenericRouter.php | 7 +++--- src/Http/ResponseInterface.php | 5 +++++ src/Kernel/BaseKernel.php | 11 ++++++++-- 8 files changed, 59 insertions(+), 24 deletions(-) delete mode 100644 app/Controller/BaseController.php create mode 100644 app/Controller/HelloWorldController.php 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 @@ -Hello'); + } + + #[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 $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 + */ + 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