Skip to content

Commit 2de66af

Browse files
Merge pull request #15 from FranklinEkemezie/dev
Dev
2 parents a48a06c + 17e678b commit 2de66af

36 files changed

+284
-200
lines changed

app/Attributes/Route.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace PHPAether\Attributes;
45

5-
use Attribute;
6+
use \Attribute;
67
use PHPAether\Enums\RequestMethod;
78

89
#[Attribute(Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]

app/Controllers/Controller.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace PHPAether\Controllers;
45

app/Core/App.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace PHPAether\Core;
55

66
use PHPAether\Core\HTTP\Request;
7+
use PHPAether\Core\HTTP\Response;
78
use PHPAether\Core\HTTP\Router;
89
use PHPAether\Exceptions\Exception;
910
use PHPAether\Exceptions\RouterExceptions\MethodNotAllowedException;
@@ -21,10 +22,37 @@ public function __construct(
2122
{
2223
}
2324

25+
/**
26+
* Resolve the route action to a callable
27+
* @throws RouterException
28+
*/
29+
public function resolveRouteAction(mixed $action): callable
30+
{
31+
if (is_callable($action)) {
32+
return $action;
33+
}
34+
35+
if (is_array($action) && count($action) === 2) {
36+
[$class_or_object, $method] = $action;
37+
if (is_string($class_or_object) && class_exists($class_or_object)) {
38+
$instance = new $class_or_object();
39+
if (is_callable([$instance, $method])) {
40+
return [$instance, $method];
41+
}
42+
}
43+
44+
if (is_callable([$class_or_object, $method])) {
45+
return [$class_or_object, $method];
46+
}
47+
}
48+
49+
throw new RouterException('Invalid route action');
50+
}
51+
2452
/**
2553
* @throws Exception
2654
*/
27-
public function run(Request $request): string
55+
public function run(Request $request): Response|string
2856
{
2957

3058
try {
@@ -53,10 +81,8 @@ public function run(Request $request): string
5381
return "An error occurred";
5482
}
5583

56-
// Ensure the action is callable
57-
if (! is_callable($action)) {
58-
throw new Exception("Invalid request handler. Route action is not callable");
59-
}
84+
// Resolve the route action
85+
$action = $this->resolveRouteAction($action);
6086

6187
// Resolve middlewares
6288
foreach ($middlewares as $middleware) {
@@ -65,6 +91,6 @@ public function run(Request $request): string
6591
$middleware->resolve();
6692
}
6793

68-
return $action($request);
94+
return call_user_func($action, $request);
6995
}
7096
}

app/Core/Database/Database.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
declare(strict_types=1);
43

54
namespace PHPAether\Core\Database;

app/Core/HTTP/Request.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ public function __construct(?array $serverVariables=null, RequestType $requestTy
6363
/**
6464
* Get a request data
6565
* @param string|null $key
66-
* @return array
66+
* @return string|array
6767
*/
68-
public function getData(?string $key=null): array
68+
public function getData(?string $key=null): string|array
6969
{
7070
if (is_null($key)) return $this->data;
7171
if (! isset($this->data[$key])) {

app/Core/HTTP/Response.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace PHPAether\Core\HTTP;
45

56
use PHPAether\Enums\ResponseStatus;
7+
use PHPAether\Interfaces\RenderableBodyInterface;
68

79
class Response
810
{
911

1012
public function __construct(
1113
public readonly ResponseStatus $status,
12-
public readonly string $body,
14+
public readonly RenderableBodyInterface|string $body,
1315
public readonly array $headers=[]
1416
)
1517
{
@@ -25,7 +27,8 @@ public function send(): never
2527
http_response_code($this->status->value);
2628

2729
// send body
28-
echo $this->body;
30+
$body = $this->body;
31+
echo is_string($body) ? $body : $body->toString();
2932

3033
exit();
3134
}

app/Core/HTTP/Router.php

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace PHPAether\Core\HTTP;
66

7+
use Closure;
78
use PHPAether\Enums\HTTPRequestMethod;
89
use PHPAether\Enums\RequestType;
910
use PHPAether\Exceptions\RouterExceptions\MethodNotAllowedException;
@@ -16,6 +17,11 @@ class Router
1617
protected array $routes = [];
1718
private ?string $routePrefix = null;
1819

20+
public const REQUEST_ROUTE_INFO_ROUTE = 4320;
21+
public const REQUEST_ROUTE_INFO_ACTION = 4231;
22+
public const REQUEST_ROUTE_INFO_MIDDLEWARES = 4232;
23+
public const REQUEST_ROUTE_PARAMS = 4233;
24+
1925
public function __construct(
2026
protected RequestType $defaultRouteType=RequestType::WEB
2127
)
@@ -41,59 +47,59 @@ public function setDefaultRouteType(RequestType $requestType): self
4147
/**
4248
* Register GET route
4349
* @param string $route
44-
* @param callable $action
50+
* @param array|string|Closure $action
4551
* @param array $middlewares
4652
* @return self
4753
*/
48-
public function get(string $route, callable $action, array $middlewares=[]): self
54+
public function get(string $route, array|string|Closure $action, array $middlewares=[]): self
4955
{
5056
return $this->registerRoute($route, HTTPRequestMethod::GET, $action, $middlewares);
5157
}
5258

5359
/**
5460
* Register POST route
5561
* @param string $route
56-
* @param callable $action
62+
* @param array|string|Closure $action
5763
* @param array $middlewares
5864
* @return self
5965
*/
60-
public function post(string $route, callable $action, array $middlewares=[]): self
66+
public function post(string $route, array|string|Closure $action, array $middlewares=[]): self
6167
{
6268
return $this->registerRoute($route, HTTPRequestMethod::POST, $action, $middlewares);
6369
}
6470

6571
/**
6672
* Register PUT route
6773
* @param string $route
68-
* @param callable $action
74+
* @param array|string|Closure $action
6975
* @param array $middlewares
7076
* @return self
7177
*/
72-
public function put(string $route, callable $action, array $middlewares=[]): self
78+
public function put(string $route, array|string|Closure $action, array $middlewares=[]): self
7379
{
7480
return $this->registerRoute($route, HTTPRequestMethod::PUT, $action, $middlewares);
7581
}
7682

7783
/**
7884
* Register PATCH route
7985
* @param string $route
80-
* @param callable $action
86+
* @param array|string|Closure $action
8187
* @param array $middlewares
8288
* @return self
8389
*/
84-
public function patch(string $route, callable $action, array $middlewares=[]): self
90+
public function patch(string $route, array|string|Closure $action, array $middlewares=[]): self
8591
{
8692
return $this->registerRoute($route, HTTPRequestMethod::PATCH, $action, $middlewares);
8793
}
8894

8995
/**
9096
* Register DELETE route
9197
* @param string $route
92-
* @param callable $action
98+
* @param array|string|Closure $action
9399
* @param array $middlewares
94100
* @return self
95101
*/
96-
public function delete(string $route, callable $action, array $middlewares=[]): self
102+
public function delete(string $route, array|string|Closure $action, array $middlewares=[]): self
97103
{
98104
return $this->registerRoute($route, HTTPRequestMethod::DELETE, $action, $middlewares);
99105
}
@@ -102,11 +108,11 @@ public function delete(string $route, callable $action, array $middlewares=[]):
102108
* Register a web route
103109
* @param string $route
104110
* @param HTTPRequestMethod $method
105-
* @param callable $action
111+
* @param array|string|Closure $action
106112
* @param array $middlewares
107113
* @return $this
108114
*/
109-
public function registerWebRoute(string $route, HTTPRequestMethod $method, callable $action, array $middlewares=[]): self
115+
public function registerWebRoute(string $route, HTTPRequestMethod $method, array|string|Closure $action, array $middlewares=[]): self
110116
{
111117
return $this->registerRoute($route, $method, $action, $middlewares, RequestType::WEB);
112118
}
@@ -115,11 +121,11 @@ public function registerWebRoute(string $route, HTTPRequestMethod $method, calla
115121
* Register an API route
116122
* @param string $route
117123
* @param HTTPRequestMethod $method
118-
* @param callable $action
124+
* @param array|string|Closure $action
119125
* @param array $middlewares
120126
* @return $this
121127
*/
122-
public function registerApiRoute(string $route, HTTPRequestMethod $method, callable $action, array $middlewares=[]): self
128+
public function registerApiRoute(string $route, HTTPRequestMethod $method, array|string|Closure $action, array $middlewares=[]): self
123129
{
124130
return $this->registerRoute($route, $method, $action, $middlewares, RequestType::API);
125131
}
@@ -128,11 +134,11 @@ public function registerApiRoute(string $route, HTTPRequestMethod $method, calla
128134
* Register a CLI route
129135
* @param string $route
130136
* @param HTTPRequestMethod $method
131-
* @param callable $action
137+
* @param array|string|Closure $action
132138
* @param array $middlewares
133139
* @return $this
134140
*/
135-
public function registerCliRoute(string $route, HTTPRequestMethod $method, callable $action, array $middlewares=[]): self
141+
public function registerCliRoute(string $route, HTTPRequestMethod $method, array|string|Closure $action, array $middlewares=[]): self
136142
{
137143
return $this->registerRoute($route, $method, $action, $middlewares, RequestType::CLI);
138144
}
@@ -141,12 +147,12 @@ public function registerCliRoute(string $route, HTTPRequestMethod $method, calla
141147
* Register a route
142148
* @param string $route
143149
* @param HTTPRequestMethod $method
144-
* @param callable $action
150+
* @param array|string|Closure $action
145151
* @param array $middlewares
146152
* @param RequestType|null $requestType
147153
* @return $this
148154
*/
149-
public function registerRoute(string $route, HTTPRequestMethod $method, callable $action, array $middlewares=[], ?RequestType $requestType=null): self
155+
public function registerRoute(string $route, HTTPRequestMethod $method, array|string|Closure $action, array $middlewares=[], ?RequestType $requestType=null): self
150156
{
151157
$requestType ??= $this->defaultRouteType;
152158

@@ -340,6 +346,23 @@ private function matchRequestPathToRoute(string $path, string $route): array|fal
340346
return $params;
341347
}
342348

349+
/**
350+
* Get a value using the key from the request route info.
351+
* @param array $requestRouteInfo
352+
* @param int|null $option
353+
* @return mixed
354+
*/
355+
public static function requestRouteInfo(array $requestRouteInfo, ?int $option=null): mixed
356+
{
357+
return match ($option) {
358+
self::REQUEST_ROUTE_INFO_ROUTE, null=> $requestRouteInfo['route'],
359+
self::REQUEST_ROUTE_INFO_ACTION => $requestRouteInfo['action'],
360+
self::REQUEST_ROUTE_INFO_MIDDLEWARES=> $requestRouteInfo['middlewares'],
361+
self::REQUEST_ROUTE_PARAMS => $requestRouteInfo['params'],
362+
default => $requestRouteInfo
363+
};
364+
}
365+
343366
/**
344367
* Route a request
345368
* @param Request $request

app/Enums/HTTPRequestMethod.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace PHPAether\Enums;
45

app/Enums/ResponseStatus.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace PHPAether\Enums;
45

app/Exceptions/Exception.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace PHPAether\Exceptions;
45

0 commit comments

Comments
 (0)