1414
1515namespace MaplePHP \Emitron ;
1616
17- use MaplePHP \Http \ResponseFactory ;
17+ use Psr \Http \Message \ ResponseInterface ;
1818use Psr \Http \Message \ServerRequestInterface ;
1919use Psr \Http \Message \StreamInterface ;
20- use MaplePHP \Log \InvalidArgumentException ;
20+ use MaplePHP \Emitron \Enums \DispatchCodes ;
21+ use MaplePHP \Http \ResponseFactory ;
2122
2223class Kernel extends AbstractKernel
2324{
@@ -30,18 +31,19 @@ class Kernel extends AbstractKernel
3031 */
3132 public function run (ServerRequestInterface $ request , ?StreamInterface $ stream = null ): void
3233 {
33-
3434 $ this ->dispatchConfig ->getRouter ()->dispatch (function ($ data , $ args , $ middlewares ) use ($ request , $ stream ) {
3535
36- //$dispatchCode = $data[0] ?? RouterDispatcher::FOUND;
3736
37+ $ dispatchCode = (int )($ data [0 ] ?? DispatchCodes::FOUND ->value );
38+ if ($ dispatchCode !== DispatchCodes::FOUND ->value ) {
39+ $ data ['handler ' ] = function (ServerRequestInterface $ req , ResponseInterface $ res ): ResponseInterface
40+ {
41+ return $ res ->withStatus (404 );
42+ };
43+ }
44+ //$dispatchCode = $data[0] ?? RouterDispatcher::FOUND;
3845 [$ data , $ args , $ middlewares ] = $ this ->reMap ($ data , $ args , $ middlewares );
3946
40- if (!isset ($ data ['handler ' ])) {
41- throw new InvalidArgumentException ("Missing 'handler' key. " );
42- }
43-
44-
4547 $ this ->container ->set ("request " , $ request );
4648 $ this ->container ->set ("args " , $ args );
4749 $ this ->container ->set ("configuration " , $ this ->getDispatchConfig ());
@@ -50,24 +52,13 @@ public function run(ServerRequestInterface $request, ?StreamInterface $stream =
5052 $ factory = new ResponseFactory ($ bodyStream );
5153 $ finalHandler = new ControllerRequestHandler ($ factory , $ data ['handler ' ] ?? []);
5254
53-
5455 $ response = $ this ->initRequestHandler (
5556 request: $ request ,
5657 stream: $ bodyStream ,
5758 finalHandler: $ finalHandler ,
5859 middlewares: $ middlewares
5960 );
6061
61- /*
62- if ($dispatchCode === Dispatcher::NOT_FOUND) {
63- $response = $response->withStatus(404);
64- }
65-
66- if ($dispatchCode === Dispatcher::METHOD_NOT_ALLOWED) {
67- $response = $response->withStatus(405);
68- }
69- */
70-
7162 $ this ->createEmitter ()->emit ($ response , $ request );
7263 });
7364 }
@@ -84,6 +75,10 @@ function reMap($data, $args, $middlewares)
8475 if (!is_array ($ middlewares )) {
8576 $ middlewares = [];
8677 }
78+
79+ if (!is_array ($ args )) {
80+ $ args = [];
81+ }
8782 return [$ data , $ args , $ middlewares ];
8883 }
8984}
0 commit comments