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 @@
+<?php
+
+namespace Ardent\Undercurrent\Http;
+
+class GenericRequest implements RequestInterface
+{
+
+    public function __construct(
+        private readonly MethodEnum $method,
+        private readonly string $uri,
+        private readonly array  $body,
+    )
+    {
+    }
+
+    public function getMethod(): MethodEnum
+    {
+        return $this->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 @@
+<?php
+
+namespace Ardent\Undercurrent\Http;
+
+class GenericResponse implements ResponseInterface
+{
+    public function __construct(
+        private readonly string     $body,
+        private readonly StatusEnum $status = StatusEnum::OK,
+    )
+    {
+    }
+
+    public function getStatus(): StatusEnum
+    {
+        return $this->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 @@
 <?php

-namespace Ardent\Undercurrent\Controller;
+namespace Ardent\Undercurrent\Http;

 use App\Controller\BaseController;
 use Ardent\Undercurrent\Attribute\Route;
diff --git a/src/Http/MethodEnum.php b/src/Http/MethodEnum.php
new file mode 100644
index 0000000..76948c9
--- /dev/null
+++ b/src/Http/MethodEnum.php
@@ -0,0 +1,16 @@
+<?php
+
+namespace Ardent\Undercurrent\Http;
+
+enum MethodEnum: string
+{
+    case GET = 'GET';
+    case POST = 'POST';
+//    case PUT = 'PUT';
+//    case PATCH = 'PATCH';
+//    case DELETE = 'DELETE';
+//    case HEAD = 'HEAD';
+//    case OPTIONS = 'OPTIONS';
+//    case TRACE = 'TRACE';
+//    case CONNECT = 'CONNECT';
+}
diff --git a/src/Http/RequestInterface.php b/src/Http/RequestInterface.php
new file mode 100644
index 0000000..0f564b8
--- /dev/null
+++ b/src/Http/RequestInterface.php
@@ -0,0 +1,12 @@
+<?php
+
+namespace Ardent\Undercurrent\Http;
+
+interface RequestInterface
+{
+    public function getMethod(): MethodEnum;
+
+    public function getUri(): string;
+
+    public function getBody(): array;
+}
\ No newline at end of file
diff --git a/src/Http/ResponseInterface.php b/src/Http/ResponseInterface.php
new file mode 100644
index 0000000..abdc290
--- /dev/null
+++ b/src/Http/ResponseInterface.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace Ardent\Undercurrent\Http;
+
+interface ResponseInterface
+{
+    public function getStatus(): StatusEnum;
+
+    public function getBody(): string;
+}
\ No newline at end of file
diff --git a/src/Http/RouterInterface.php b/src/Http/RouterInterface.php
new file mode 100644
index 0000000..186baac
--- /dev/null
+++ b/src/Http/RouterInterface.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace Ardent\Undercurrent\Http;
+
+interface RouterInterface
+{
+    public function dispatch(RequestInterface $request): ResponseInterface;
+}
\ No newline at end of file
diff --git a/src/Http/StatusEnum.php b/src/Http/StatusEnum.php
new file mode 100644
index 0000000..6dce100
--- /dev/null
+++ b/src/Http/StatusEnum.php
@@ -0,0 +1,10 @@
+<?php
+
+namespace Ardent\Undercurrent\Http;
+
+enum StatusEnum: int
+{
+    case OK = 200;
+    case FORBIDDEN = 403;
+    case NOT_FOUND = 404;
+}
diff --git a/src/Kernel/BaseKernel.php b/src/Kernel/BaseKernel.php
index 6bc8ed5..05ecf06 100644
--- a/src/Kernel/BaseKernel.php
+++ b/src/Kernel/BaseKernel.php
@@ -4,11 +4,12 @@ namespace Ardent\Undercurrent\Kernel;

 use App\Controller\BaseController;
 use Ardent\Undercurrent\Container\GenericContainer;
-use Ardent\Undercurrent\Controller\GenericRouter;
+use Ardent\Undercurrent\Http\GenericRouter;
+use Ardent\Undercurrent\Http\ResponseInterface;

 class BaseKernel
 {
-    public function __invoke()
+    public function __invoke(): void
     {
         $container = new GenericContainer();
         $container->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
This commit is contained in:
Tim 2023-08-07 14:23:00 +02:00
parent 68ccd37f07
commit e9a636554f
11 changed files with 119 additions and 6 deletions

View File

@ -1,6 +1,7 @@
Todos: Todos:
- [ ] Collect routes properly - [ ] Collect routes properly
- [ ] Process routes correctly - [ ] Process routes correctly
- [ ] Add request and response objects

View File

@ -3,12 +3,14 @@
namespace App\Controller; namespace App\Controller;
use Ardent\Undercurrent\Attribute\Route; use Ardent\Undercurrent\Attribute\Route;
use Ardent\Undercurrent\Http\GenericResponse;
use Ardent\Undercurrent\Http\ResponseInterface;
class BaseController class BaseController
{ {
#[Route('/hello')] #[Route('/hello')]
public function HelloWorld(): string public function HelloWorld(): ResponseInterface
{ {
return 'Hello, World!'; return new GenericResponse('Hello World!');
} }
} }

View File

@ -0,0 +1,30 @@
<?php
namespace Ardent\Undercurrent\Http;
class GenericRequest implements RequestInterface
{
public function __construct(
private readonly MethodEnum $method,
private readonly string $uri,
private readonly array $body,
)
{
}
public function getMethod(): MethodEnum
{
return $this->method;
}
public function getUri(): string
{
return $this->uri;
}
public function getBody(): array
{
return $this->body;
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace Ardent\Undercurrent\Http;
class GenericResponse implements ResponseInterface
{
public function __construct(
private readonly string $body,
private readonly StatusEnum $status = StatusEnum::OK,
)
{
}
public function getStatus(): StatusEnum
{
return $this->status;
}
public function getBody(): string
{
return $this->body;
}
}

View File

@ -1,6 +1,6 @@
<?php <?php
namespace Ardent\Undercurrent\Controller; namespace Ardent\Undercurrent\Http;
use App\Controller\BaseController; use App\Controller\BaseController;
use Ardent\Undercurrent\Attribute\Route; use Ardent\Undercurrent\Attribute\Route;

16
src/Http/MethodEnum.php Normal file
View File

@ -0,0 +1,16 @@
<?php
namespace Ardent\Undercurrent\Http;
enum MethodEnum: string
{
case GET = 'GET';
case POST = 'POST';
// case PUT = 'PUT';
// case PATCH = 'PATCH';
// case DELETE = 'DELETE';
// case HEAD = 'HEAD';
// case OPTIONS = 'OPTIONS';
// case TRACE = 'TRACE';
// case CONNECT = 'CONNECT';
}

View File

@ -0,0 +1,12 @@
<?php
namespace Ardent\Undercurrent\Http;
interface RequestInterface
{
public function getMethod(): MethodEnum;
public function getUri(): string;
public function getBody(): array;
}

View File

@ -0,0 +1,10 @@
<?php
namespace Ardent\Undercurrent\Http;
interface ResponseInterface
{
public function getStatus(): StatusEnum;
public function getBody(): string;
}

View File

@ -0,0 +1,8 @@
<?php
namespace Ardent\Undercurrent\Http;
interface RouterInterface
{
public function dispatch(RequestInterface $request): ResponseInterface;
}

10
src/Http/StatusEnum.php Normal file
View File

@ -0,0 +1,10 @@
<?php
namespace Ardent\Undercurrent\Http;
enum StatusEnum: int
{
case OK = 200;
case FORBIDDEN = 403;
case NOT_FOUND = 404;
}

View File

@ -4,11 +4,12 @@ namespace Ardent\Undercurrent\Kernel;
use App\Controller\BaseController; use App\Controller\BaseController;
use Ardent\Undercurrent\Container\GenericContainer; use Ardent\Undercurrent\Container\GenericContainer;
use Ardent\Undercurrent\Controller\GenericRouter; use Ardent\Undercurrent\Http\GenericRouter;
use Ardent\Undercurrent\Http\ResponseInterface;
class BaseKernel class BaseKernel
{ {
public function __invoke() public function __invoke(): void
{ {
$container = new GenericContainer(); $container = new GenericContainer();
$container->add(GenericRouter::class); $container->add(GenericRouter::class);
@ -23,6 +24,6 @@ class BaseKernel
$route = $router->getRoute($_SERVER['REQUEST_URI']); $route = $router->getRoute($_SERVER['REQUEST_URI']);
$controller = $container->get($route['controller']); $controller = $container->get($route['controller']);
$method = $route['method']; $method = $route['method'];
echo $controller->$method(); echo $controller->$method()->getBody();
} }
} }