1616use Psr \Http \Server \MiddlewareInterface ;
1717use Psr \Http \Server \RequestHandlerInterface ;
1818use Slim \Interfaces \EmitterInterface ;
19- use Slim \Interfaces \RouteCollectionInterface ;
19+ use Slim \Interfaces \RouterInterface ;
2020use Slim \Interfaces \ServerRequestCreatorInterface ;
21- use Slim \RequestHandler \MiddlewareRequestHandler ;
21+ use Slim \Middleware \EndpointMiddleware ;
22+ use Slim \Middleware \ErrorExceptionMiddleware ;
23+ use Slim \Middleware \HtmlExceptionMiddleware ;
24+ use Slim \Middleware \JsonExceptionMiddleware ;
25+ use Slim \Middleware \RoutingMiddleware ;
2226use Slim \Routing \Route ;
2327use Slim \Routing \RouteCollectionTrait ;
2428use Slim \Routing \RouteGroup ;
25- use Slim \Routing \Router ;
2629
2730/**
2831 * App
3134 * running the application. It provides methods for defining routes, adding middleware, and managing
3235 * the application's lifecycle, including handling HTTP requests and emitting responses.
3336 *
34- * @template TContainerInterface of (ContainerInterface|null)
35- *
3637 * @api
3738 */
38- class App implements RequestHandlerInterface, RouteCollectionInterface
39+ class App implements RequestHandlerInterface
3940{
4041 use RouteCollectionTrait;
4142
@@ -64,7 +65,7 @@ class App implements RequestHandlerInterface, RouteCollectionInterface
6465 /**
6566 * The router instance for handling route definitions and matching.
6667 */
67- private Router $ router ;
68+ private RouterInterface $ router ;
6869
6970 /**
7071 * The emitter instance for sending the HTTP response to the client.
@@ -84,14 +85,12 @@ public function __construct(ContainerInterface $container)
8485 $ this ->container = $ container ;
8586 $ this ->serverRequestCreator = $ container ->get (ServerRequestCreatorInterface::class);
8687 $ this ->requestHandler = $ container ->get (RequestHandlerInterface::class);
87- $ this ->router = $ container ->get (Router ::class);
88+ $ this ->router = $ container ->get (RouterInterface ::class);
8889 $ this ->emitter = $ container ->get (EmitterInterface::class);
8990 }
9091
9192 /**
9293 * Get the dependency injection container.
93- *
94- * @return ContainerInterface The DI container instance
9594 */
9695 public function getContainer (): ContainerInterface
9796 {
@@ -101,7 +100,7 @@ public function getContainer(): ContainerInterface
101100 /**
102101 * Define a new route with the specified HTTP methods and URI pattern.
103102 *
104- * @param array $methods The HTTP methods the route should respond to
103+ * @param array<string> $methods The HTTP methods the route should respond to
105104 * @param string $path The URI pattern for the route
106105 * @param callable|string $handler The route handler callable or controller method
107106 *
@@ -125,22 +124,9 @@ public function group(string $path, callable $handler): RouteGroup
125124 return $ this ->router ->group ($ path , $ handler );
126125 }
127126
128- /**
129- * Get the base path used for routing.
130- *
131- * @return string The base path used for routing
132- */
133- public function getBasePath (): string
134- {
135- return $ this ->router ->getBasePath ();
136- }
137-
138127 /**
139128 * Set the base path used for routing.
140- *
141- * @param string $basePath The base path to use for routing
142- *
143- * @return self The current App instance for method chaining
129+ * @param string $basePath
144130 */
145131 public function setBasePath (string $ basePath ): self
146132 {
@@ -149,8 +135,17 @@ public function setBasePath(string $basePath): self
149135 return $ this ;
150136 }
151137
138+ /**
139+ * Get the base path used for routing.
140+ */
141+ public function getBasePath (): string
142+ {
143+ return $ this ->router ->getBasePath ();
144+ }
145+
152146 /**
153147 * Add a new middleware to the stack.
148+ * @param MiddlewareInterface|callable|string $middleware
154149 */
155150 public function add (MiddlewareInterface |callable |string $ middleware ): self
156151 {
@@ -161,10 +156,7 @@ public function add(MiddlewareInterface|callable|string $middleware): self
161156
162157 /**
163158 * Add a new middleware to the application's middleware stack.
164- *
165- * @param MiddlewareInterface $middleware The middleware to add
166- *
167- * @return self The current App instance for method chaining
159+ * @param MiddlewareInterface $middleware
168160 */
169161 public function addMiddleware (MiddlewareInterface $ middleware ): self
170162 {
@@ -173,42 +165,56 @@ public function addMiddleware(MiddlewareInterface $middleware): self
173165 return $ this ;
174166 }
175167
168+ /**
169+ * Add routing middleware.
170+ *
171+ * @return self
172+ */
173+ public function addRoutingMiddleware (): self
174+ {
175+ return $ this
176+ ->add (RoutingMiddleware::class)
177+ ->add (EndpointMiddleware::class);
178+ }
179+
180+ /**
181+ * Add set of default error handling middleware.
182+ *
183+ * @return self
184+ */
185+ public function addErrorMiddleware (): self
186+ {
187+ return $ this
188+ ->add (ErrorExceptionMiddleware::class)
189+ ->add (HtmlExceptionMiddleware::class)
190+ ->add (JsonExceptionMiddleware::class);
191+ }
192+
176193 /**
177194 * Run the Slim application.
178195 *
179196 * This method traverses the application's middleware stack, processes the incoming HTTP request,
180197 * and emits the resultant HTTP response to the client.
181- *
182- * @param ServerRequestInterface|null $request The HTTP request to handle.
183- * If null, it creates a request from globals.
184- *
185- * @return void
198+ * @param ?ServerRequestInterface $request
186199 */
187200 public function run (?ServerRequestInterface $ request = null ): void
188201 {
189202 if (!$ request ) {
190203 $ request = $ this ->serverRequestCreator ->createServerRequestFromGlobals ();
191204 }
192205
193- $ response = $ this ->handle ($ request );
194-
195- $ this ->emitter ->emit ($ response );
206+ $ this ->emitter ->emit ($ this ->handle ($ request ));
196207 }
197208
198209 /**
199210 * Handle an incoming HTTP request.
200211 *
201212 * This method processes the request through the application's middleware stack and router,
202213 * returning the resulting HTTP response.
203- *
204- * @param ServerRequestInterface $request The HTTP request to handle
205- *
206- * @return ResponseInterface The HTTP response
214+ * @param ServerRequestInterface $request
207215 */
208216 public function handle (ServerRequestInterface $ request ): ResponseInterface
209217 {
210- $ request = $ request ->withAttribute (MiddlewareRequestHandler::MIDDLEWARE , $ this ->router ->getMiddlewareStack ());
211-
212218 return $ this ->requestHandler ->handle ($ request );
213219 }
214220}
0 commit comments