@@ -20,6 +20,9 @@ class SimpleDispatcher implements DispatcherInterface
2020 /** @var RouterInterface */
2121 private $ router ;
2222
23+ /** @var bool */
24+ private $ initialized ;
25+
2326 /**
2427 * some setting for self
2528 * @var array
@@ -53,9 +56,6 @@ class SimpleDispatcher implements DispatcherInterface
5356 // events
5457 ];
5558
56- /** @var bool */
57- private $ initialized ;
58-
5959 /**
6060 * object creator.
6161 * @param RouterInterface $router
@@ -115,7 +115,7 @@ public function dispatchUri(string $path = null, string $method = null)
115115 {
116116 $ path = $ path ?: $ _SERVER ['REQUEST_URI ' ];
117117
118- if (strpos ($ path , '? ' )) {
118+ if (\ strpos ($ path , '? ' )) {
119119 $ path = \parse_url ($ path , PHP_URL_PATH );
120120 }
121121
@@ -127,7 +127,6 @@ public function dispatchUri(string $path = null, string $method = null)
127127 $ method = $ method ?: $ _SERVER ['REQUEST_METHOD ' ];
128128
129129 list ($ status , $ path , $ info ) = $ this ->router ->match ($ path , $ method );
130-
131130 $ info ['requestMethod ' ] = $ method ;
132131
133132 return $ this ->dispatch ($ status , $ path , $ info );
@@ -153,19 +152,21 @@ public function dispatch(int $status, string $path, array $info)
153152
154153 // method not allowed
155154 if ($ status === RouterInterface::METHOD_NOT_ALLOWED ) {
155+ unset($ info ['requestMethod ' ]);
156156 return $ this ->handleNotAllowed ($ path , $ method , $ info );
157157 }
158158
159159 $ result = null ;
160160
161161 try {
162162 $ result = $ this ->callRouteHandler ($ path , $ method , $ info ['handler ' ], $ args );
163- } catch (\Exception $ e ) {
164- $ this ->handleException ($ e , $ path , $ info );
165- // throw new \RuntimeException($e->getMessage(), __LINE__, $e);
166163 } catch (\Throwable $ e ) {
167- // throw new \RuntimeException($e->getMessage(), __LINE__, $e);
168- $ this ->handleException ($ e , $ path , $ info );
164+ // trigger route exec_error event
165+ if ($ cb = $ this ->getOption (self ::ON_EXEC_ERROR )) {
166+ return $ this ->fireCallback ($ cb , [$ e , $ path , $ info ]);
167+ }
168+
169+ throw $ e ;
169170 }
170171
171172 return $ result ;
@@ -175,7 +176,7 @@ public function dispatch(int $status, string $path, array $info)
175176 * execute the matched Route Handler
176177 * @param string $path The route path
177178 * @param string $method The request method
178- * @param callable $handler The route path handler
179+ * @param callable|mixed $handler The route path handler
179180 * @param array $args Matched param from path
180181 * [
181182 * 'matches' => []
@@ -235,7 +236,7 @@ protected function callRouteHandler(string $path, string $method, $handler, arra
235236 }
236237
237238 // action method is not exist
238- if (!method_exists ($ controller , $ actionMethod )) {
239+ if (!\ method_exists ($ controller , $ actionMethod )) {
239240 return $ this ->handleNotFound ($ path , $ method , true );
240241 }
241242
@@ -288,7 +289,6 @@ protected function handleNotAllowed(string $path, string $method, array $methods
288289 // Run the 'NotAllowed' callback if the route was not found
289290 if (!$ handler = $ this ->getOption (self ::ON_METHOD_NOT_ALLOWED )) {
290291 $ handler = $ this ->defaultNotAllowedHandler ();
291-
292292 $ this ->setOption (self ::ON_METHOD_NOT_ALLOWED , $ handler );
293293
294294 // is a route path. like '/site/notFound'
@@ -308,25 +308,15 @@ protected function handleNotAllowed(string $path, string $method, array $methods
308308 return $ this ->fireCallback ($ handler , [$ path , $ method , $ methods ]);
309309 }
310310
311- /**
312- * @param \Exception|\Throwable $e
313- * @param string $path
314- * @param array $info
315- */
316- public function handleException ($ e , string $ path , array $ info )
317- {
318- // handle ...
319- }
320-
321311 /**
322312 * @return \Closure
323313 */
324314 protected function defaultNotFoundHandler (): \Closure
325315 {
326316 return function ($ path ) {
327317 $ protocol = $ _SERVER ['SERVER_PROTOCOL ' ] ?? 'HTTP/1.1 ' ;
328- header ($ protocol . ' 404 Not Found ' );
329- echo "<h1 style='width: 60 %; margin: 5% auto;'>:( 404<br>Page Not Found <code style='font-weight: normal;'> $ path</code></h1> " ;
318+ \ header ($ protocol . ' 404 Not Found ' );
319+ echo "<h1 style='width: 80 %; margin: 5% auto; text-align: center ;'>:( 404<br>Page Not Found <code style='font-weight: normal;'> $ path</code></h1> " ;
330320 };
331321 }
332322
@@ -336,14 +326,15 @@ protected function defaultNotFoundHandler(): \Closure
336326 protected function defaultNotAllowedHandler (): \Closure
337327 {
338328 return function ($ path , $ method , $ methods ) {
339- $ allow = implode (', ' , $ methods );
329+ $ allow = \ implode (', ' , $ methods );
340330 $ protocol = $ _SERVER ['SERVER_PROTOCOL ' ] ?? 'HTTP/1.1 ' ;
341- header ($ protocol . ' 405 Method Not Allowed ' );
331+ \ header ($ protocol . ' 405 Method Not Allowed ' );
342332
343333 echo <<<HTML
344- <div style="width: 500px; margin: 5% auto;">
345- <h1>:( Method not allowed <code style="font-weight: normal;font-size: 16px">for $ method $ path</code></h1>
346- <p style="font-size: 20px">Method not allowed. Must be one of: <strong> $ allow</strong></p>
334+ <div style="min-width: 500px;margin: 5% auto;width: 80%;text-align: center;">
335+ <h1>:( Method not allowed </h1>
336+ <p style="font-size: 20px">Method not allowed for <strong> $ method</strong> <code style="color: #d94141;font-size: smaller;"> $ path</code></p>
337+ <p>The request method must be one of <strong> $ allow</strong></p>
347338</div>
348339HTML ;
349340 };
@@ -362,13 +353,14 @@ public function on(string $event, $handler)
362353 }
363354
364355 /**
365- * @param callable $cb
356+ * @param callable|mixed $cb
366357 * string - func name, class name
367358 * array - [class, method]
368359 * object - Closure, Object
369360 *
370361 * @param array $args
371362 * @return mixed
363+ * @throws \InvalidArgumentException
372364 */
373365 protected function fireCallback ($ cb , array $ args = [])
374366 {
0 commit comments