@@ -26,27 +26,32 @@ public function __construct(
2626 private readonly Request $ request ,
2727 ) {}
2828
29- public function get (string $ path , RequestHandlerInterface $ handler ): void
29+ /** @param class-string<RequestHandlerInterface>|RequestHandlerInterface $handler */
30+ public function get (string $ path , string |RequestHandlerInterface $ handler ): void
3031 {
3132 $ this ->register ($ path , HttpMethod::GET , $ handler );
3233 }
3334
34- public function post (string $ path , RequestHandlerInterface $ handler ): void
35+ /** @param class-string<RequestHandlerInterface>|RequestHandlerInterface $handler */
36+ public function post (string $ path , string |RequestHandlerInterface $ handler ): void
3537 {
3638 $ this ->register ($ path , HttpMethod::POST , $ handler );
3739 }
3840
39- public function put (string $ path , RequestHandlerInterface $ handler ): void
41+ /** @param class-string<RequestHandlerInterface>|RequestHandlerInterface $handler */
42+ public function put (string $ path , string |RequestHandlerInterface $ handler ): void
4043 {
4144 $ this ->register ($ path , HttpMethod::PUT , $ handler );
4245 }
4346
44- public function patch (string $ path , RequestHandlerInterface $ handler ): void
47+ /** @param class-string<RequestHandlerInterface>|RequestHandlerInterface $handler */
48+ public function patch (string $ path , string |RequestHandlerInterface $ handler ): void
4549 {
4650 $ this ->register ($ path , HttpMethod::PATCH , $ handler );
4751 }
4852
49- public function delete (string $ path , RequestHandlerInterface $ handler ): void
53+ /** @param class-string<RequestHandlerInterface>|RequestHandlerInterface $handler */
54+ public function delete (string $ path , string |RequestHandlerInterface $ handler ): void
5055 {
5156 $ this ->register ($ path , HttpMethod::DELETE , $ handler );
5257 }
@@ -56,7 +61,8 @@ public function redirect(string $from, string $to, int $status = 302): void
5661 $ this ->get ($ from , new RedirectHandler ($ to , $ status ));
5762 }
5863
59- private function register (string $ path , HttpMethod $ httpMethod , RequestHandlerInterface $ handler ): void
64+ /** @param class-string<RequestHandlerInterface>|RequestHandlerInterface $handler */
65+ private function register (string $ path , HttpMethod $ httpMethod , string |RequestHandlerInterface $ handler ): void
6066 {
6167 $ this ->routes [] = new Route ($ path , $ httpMethod , $ handler );
6268 }
@@ -65,8 +71,14 @@ public function handleRequest(): Response
6571 {
6672 try {
6773 $ route = $ this ->getRoute ();
74+
6875 $ this ->request ->setParams ($ route ->getParams ());
69- $ response = $ route ->getHandler ()->handle ($ this ->request );
76+
77+ /** @var class-string<RequestHandlerInterface>|RequestHandlerInterface $handler */
78+ $ handler = $ route ->getHandler ();
79+ $ response = is_string ($ handler )
80+ ? (new $ handler )->handle ($ this ->request )
81+ : $ handler ->handle ($ this ->request );
7082 } catch (NotFoundException ) {
7183 $ response = (new NotFoundHandler )->handle ($ this ->request );
7284 } catch (MethodNotAllowedException ) {
0 commit comments