33
44namespace ZankoKhaledi \PhpSimpleRouter ;
55
6+
7+ use Dotenv \Dotenv ;
68use Exception as ExceptionAlias ;
79use ZankoKhaledi \PhpSimpleRouter \Interfaces \IMiddleware ;
810use ZankoKhaledi \PhpSimpleRouter \Interfaces \IRoute ;
@@ -29,6 +31,7 @@ final class Router implements IRoute
2931 private ?string $ prefix = null ;
3032 private ?array $ routes = [];
3133 private array $ middlewares = [];
34+ private ?string $ host = null ;
3235
3336
3437 const GET = 'GET ' ;
@@ -37,15 +40,20 @@ final class Router implements IRoute
3740 const PATCH = 'PATCH ' ;
3841 const DELETE = 'DELETE ' ;
3942
43+ const HOSTNAME = 'localhost ' ;
4044
4145 /**
4246 *
4347 * @throws ExceptionAlias
4448 */
4549 private function __construct ()
4650 {
51+ $ env = Dotenv::createImmutable (dirname (__DIR__ ));
52+ $ env ->load ();
53+
4754 $ this ->serverMode = php_sapi_name ();
4855 $ this ->uri = $ this ->serverMode === 'cli-server ' ? parse_url ($ _SERVER ['REQUEST_URI ' ], PHP_URL_PATH ) : null ;
56+ $ this ->host = $ _ENV ['HOSTNAME ' ] ?? static ::HOSTNAME ;
4957 }
5058
5159 /**
@@ -68,26 +76,6 @@ private static function getInstance(): ?static
6876 return static ::$ instance ;
6977 }
7078
71- /**
72- * @param array $attributes
73- * @param callable $callback
74- * @return void
75- */
76- public static function group (array $ attributes , callable $ callback ): void
77- {
78- if (array_key_exists ('prefix ' , $ attributes )) {
79- static ::getInstance ()->prefix = $ attributes ['prefix ' ];
80- }
81- if (array_key_exists ('middleware ' , $ attributes )) {
82- static ::getInstance ()->middlewares = is_array ($ attributes ['middleware ' ]) && count ($ attributes ['middleware ' ]) ? [...$ attributes ['middleware ' ]] : $ attributes ['middleware ' ];
83- }
84-
85- call_user_func ($ callback );
86-
87- static ::getInstance ()->prefix = null ;
88- static ::getInstance ()->middlewares = [];
89- }
90-
9179
9280 /**
9381 * @param string $path
@@ -178,7 +166,7 @@ public static function executeRoutes(): void
178166 public function middleware (array $ middlewares ): IRoute
179167 {
180168 foreach ($ this ->routes as $ index => $ route ) {
181- if ($ this -> routes [ $ index ] ['path ' ] === $ this ->path ) {
169+ if ($ route [ ' domain ' ] . $ route ['path ' ] === $ this -> host . $ this ->path ) {
182170 $ this ->routes [$ index ]['middlewares ' ] = [...$ middlewares , ...$ this ->middlewares ];
183171 }
184172 }
@@ -195,7 +183,7 @@ public function where(string $pattern): IRoute
195183 preg_match ($ pattern , $ this ->uri , $ matches );
196184
197185 foreach ($ this ->routes as $ index => $ route ) {
198- if ($ route ['path ' ] === $ this ->path ) {
186+ if ($ route ['domain ' ] . $ route [ ' path ' ] === $ this -> host . $ this ->path ) {
199187 $ this ->routes [$ index ]['valid ' ] = count ($ matches ) > 0 ;
200188 }
201189 }
@@ -204,6 +192,31 @@ public function where(string $pattern): IRoute
204192 }
205193
206194
195+ /**
196+ * @param array $attributes
197+ * @param callable $callback
198+ * @return void
199+ */
200+ public static function group (array $ attributes , callable $ callback ): void
201+ {
202+ if (array_key_exists ('prefix ' , $ attributes )) {
203+ static ::getInstance ()->prefix = $ attributes ['prefix ' ];
204+ }
205+ if (array_key_exists ('middleware ' , $ attributes )) {
206+ static ::getInstance ()->middlewares = is_array ($ attributes ['middleware ' ]) && count ($ attributes ['middleware ' ]) ? [...$ attributes ['middleware ' ]] : $ attributes ['middleware ' ];
207+ }
208+ if (array_key_exists ('domain ' , $ attributes )) {
209+ static ::getInstance ()->host = $ attributes ['domain ' ];
210+ }
211+
212+ call_user_func ($ callback );
213+
214+ static ::getInstance ()->prefix = null ;
215+ static ::getInstance ()->middlewares = [];
216+ static ::getInstance ()->host = $ _ENV ['HOSTNAME ' ] ?? static ::HOSTNAME ;
217+ }
218+
219+
207220 /**
208221 * @param string $method
209222 * @param mixed $path
@@ -221,7 +234,7 @@ private function addRoute(string $method, mixed $path, callable|array $callback)
221234 $ this ->path = $ this ->prefix !== null ? $ this ->prefix . $ path : $ path ;
222235
223236 foreach ($ this ->routes as $ index => $ route ) {
224- if ($ this -> routes [ $ index ] ['path ' ] === $ this ->path && $ this ->routes [ $ index ] ['method ' ] === $ method ) {
237+ if ($ route [ ' domain ' ] . $ route ['path ' ] === $ this ->host . $ this ->path && $ route ['method ' ] === $ method ) {
225238 throw new ExceptionAlias ("route $ path added before. " );
226239 }
227240 }
@@ -231,7 +244,8 @@ private function addRoute(string $method, mixed $path, callable|array $callback)
231244 'path ' => $ this ->path ,
232245 'callback ' => $ callback ,
233246 'middlewares ' => [...$ this ->middlewares ],
234- 'valid ' => true
247+ 'valid ' => true ,
248+ 'domain ' => $ this ->host
235249 ];
236250
237251 return $ this ;
@@ -244,13 +258,17 @@ private function addRoute(string $method, mixed $path, callable|array $callback)
244258 private function serve (): void
245259 {
246260 foreach ($ this ->routes as $ index => $ route ) {
247- if ($ this ->handleDynamicRouteParamsAndPath ($ route ['path ' ], $ this ->uri ) && $ route ['method ' ] === $ _SERVER ['REQUEST_METHOD ' ] && $ route ['valid ' ]) {
248- $ this ->handleRoute ($ route ['callback ' ], $ route ['middlewares ' ]);
249- return ;
261+ if ($ this ->handleDynamicRouteParamsAndPath ($ route ['path ' ], $ this ->uri ) &&
262+ $ route ['method ' ] === $ _SERVER ['REQUEST_METHOD ' ] &&
263+ $ route ['valid ' ]
264+ ) {
265+ if ($ this ->checkDomain ($ route ['domain ' ])) {
266+ $ this ->handleRoute ($ route ['callback ' ], $ route ['middlewares ' ]);
267+ return ;
268+ }
250269 }
251270 }
252271
253-
254272 header ('Location:/route-not-found ' );
255273 exit ();
256274 }
@@ -269,30 +287,40 @@ private function handleRoute(array|callable $callback, array $middlewares): void
269287 foreach ($ middlewares as $ middleware ) {
270288 $ instance = (new $ middleware );
271289 if ($ instance instanceof IMiddleware) {
272- $ instance ->handle ($ this ->request );
290+ $ instance ->handle ($ this ->request , fn (Request $ request ) => $ this ->handleCallback ($ callback , $ request ));
291+ return ;
273292 } else {
274293 throw new ExceptionAlias ("$ middleware must be type of IMiddleware interface. " );
275294 }
276295 }
277296
278- $ this ->handleCallback ($ callback );
297+ $ this ->handleCallback ($ callback, $ this -> request );
279298 }
280299
281300
282301 /**
283302 * @param callable|array $callback
284303 * @return void
285304 */
286- private function handleCallback (callable |array $ callback ): void
305+ private function handleCallback (callable |array $ callback, Request $ request ): void
287306 {
288307 is_array ($ callback ) ?
289- call_user_func_array ([new $ callback [0 ], $ callback [1 ]], [$ this -> request ]) :
290- call_user_func ($ callback , $ this -> request );
308+ call_user_func_array ([new $ callback [0 ], $ callback [1 ]], [$ request ]) :
309+ call_user_func ($ callback , $ request );
291310 }
292311
312+ /**
313+ * @param string $domain
314+ * @return bool
315+ */
316+ private function checkDomain (string $ domain ): bool
317+ {
318+ return $ domain === parse_url ($ _SERVER ['HTTP_HOST ' ], PHP_URL_HOST );
319+ }
293320
294321 /**
295322 * @param string $route
323+ * @param string $uri
296324 * @return bool
297325 */
298326 private function handleDynamicRouteParamsAndPath (string $ route , string $ uri ): bool
0 commit comments