From e9a636554f325053185c421d6a5366d40b10db25 Mon Sep 17 00:00:00 2001 From: Tim Date: Mon, 7 Aug 2023 14:23:00 +0200 Subject: [PATCH] Add response and request objects and interfaces diff --git a/README.md b/README.md index 4b64da1..c860957 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ Todos: - [ ] Collect routes properly - [ ] Process routes correctly +- [ ] Add request and response objects diff --git a/app/Controller/BaseController.php b/app/Controller/BaseController.php index aa0a79b..7df9387 100644 --- a/app/Controller/BaseController.php +++ b/app/Controller/BaseController.php @@ -3,12 +3,14 @@ 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(): string + public function HelloWorld(): ResponseInterface { - return 'Hello, World!'; + return new GenericResponse('Hello World!'); } } \ No newline at end of file diff --git a/src/Http/GenericRequest.php b/src/Http/GenericRequest.php new file mode 100644 index 0000000..c758a40 --- /dev/null +++ b/src/Http/GenericRequest.php @@ -0,0 +1,30 @@ +method; + } + + public function getUri(): string + { + return $this->uri; + } + + public function getBody(): array + { + return $this->body; + } +} \ No newline at end of file diff --git a/src/Http/GenericResponse.php b/src/Http/GenericResponse.php new file mode 100644 index 0000000..d80d350 --- /dev/null +++ b/src/Http/GenericResponse.php @@ -0,0 +1,23 @@ +status; + } + + public function getBody(): string + { + return $this->body; + } +} \ No newline at end of file diff --git a/src/Controller/GenericRouter.php b/src/Http/GenericRouter.php similarity index 89% rename from src/Controller/GenericRouter.php rename to src/Http/GenericRouter.php index 28f75b1..0332ce5 100644 --- a/src/Controller/GenericRouter.php +++ b/src/Http/GenericRouter.php @@ -1,6 +1,6 @@ add(GenericRouter::class); @@ -23,6 +24,6 @@ class BaseKernel $route = $router->getRoute($_SERVER['REQUEST_URI']); $controller = $container->get($route['controller']); $method = $route['method']; - echo $controller->$method(); + echo $controller->$method()->getBody(); } } \ No newline at end of file --- README.md | 1 + app/Controller/BaseController.php | 6 +++-- src/Http/GenericRequest.php | 30 ++++++++++++++++++++++ src/Http/GenericResponse.php | 23 +++++++++++++++++ src/{Controller => Http}/GenericRouter.php | 2 +- src/Http/MethodEnum.php | 16 ++++++++++++ src/Http/RequestInterface.php | 12 +++++++++ src/Http/ResponseInterface.php | 10 ++++++++ src/Http/RouterInterface.php | 8 ++++++ src/Http/StatusEnum.php | 10 ++++++++ src/Kernel/BaseKernel.php | 7 ++--- 11 files changed, 119 insertions(+), 6 deletions(-) create mode 100644 src/Http/GenericRequest.php create mode 100644 src/Http/GenericResponse.php rename src/{Controller => Http}/GenericRouter.php (89%) create mode 100644 src/Http/MethodEnum.php create mode 100644 src/Http/RequestInterface.php create mode 100644 src/Http/ResponseInterface.php create mode 100644 src/Http/RouterInterface.php create mode 100644 src/Http/StatusEnum.php diff --git a/README.md b/README.md index 4b64da1..c860957 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ Todos: - [ ] Collect routes properly - [ ] Process routes correctly +- [ ] Add request and response objects diff --git a/app/Controller/BaseController.php b/app/Controller/BaseController.php index aa0a79b..7df9387 100644 --- a/app/Controller/BaseController.php +++ b/app/Controller/BaseController.php @@ -3,12 +3,14 @@ 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(): string + public function HelloWorld(): ResponseInterface { - return 'Hello, World!'; + return new GenericResponse('Hello World!'); } } \ No newline at end of file diff --git a/src/Http/GenericRequest.php b/src/Http/GenericRequest.php new file mode 100644 index 0000000..c758a40 --- /dev/null +++ b/src/Http/GenericRequest.php @@ -0,0 +1,30 @@ +method; + } + + public function getUri(): string + { + return $this->uri; + } + + public function getBody(): array + { + return $this->body; + } +} \ No newline at end of file diff --git a/src/Http/GenericResponse.php b/src/Http/GenericResponse.php new file mode 100644 index 0000000..d80d350 --- /dev/null +++ b/src/Http/GenericResponse.php @@ -0,0 +1,23 @@ +status; + } + + public function getBody(): string + { + return $this->body; + } +} \ No newline at end of file diff --git a/src/Controller/GenericRouter.php b/src/Http/GenericRouter.php similarity index 89% rename from src/Controller/GenericRouter.php rename to src/Http/GenericRouter.php index 28f75b1..0332ce5 100644 --- a/src/Controller/GenericRouter.php +++ b/src/Http/GenericRouter.php @@ -1,6 +1,6 @@ add(GenericRouter::class); @@ -23,6 +24,6 @@ class BaseKernel $route = $router->getRoute($_SERVER['REQUEST_URI']); $controller = $container->get($route['controller']); $method = $route['method']; - echo $controller->$method(); + echo $controller->$method()->getBody(); } } \ No newline at end of file