44
55namespace PHPAether \Core \HTTP ;
66
7+ use Closure ;
78use PHPAether \Enums \HTTPRequestMethod ;
89use PHPAether \Enums \RequestType ;
910use 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
0 commit comments