1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Slim Framework (https://slimframework.com)
|
|
|
5 |
*
|
|
|
6 |
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License)
|
|
|
7 |
*/
|
|
|
8 |
|
|
|
9 |
declare(strict_types=1);
|
|
|
10 |
|
|
|
11 |
namespace Slim\Routing;
|
|
|
12 |
|
|
|
13 |
use Psr\Http\Message\ResponseInterface;
|
|
|
14 |
use Psr\Http\Message\ServerRequestInterface;
|
|
|
15 |
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
16 |
use Slim\Exception\HttpMethodNotAllowedException;
|
|
|
17 |
use Slim\Exception\HttpNotFoundException;
|
|
|
18 |
use Slim\Interfaces\RouteCollectorProxyInterface;
|
|
|
19 |
use Slim\Interfaces\RouteParserInterface;
|
|
|
20 |
use Slim\Interfaces\RouteResolverInterface;
|
|
|
21 |
use Slim\Middleware\RoutingMiddleware;
|
|
|
22 |
|
|
|
23 |
class RouteRunner implements RequestHandlerInterface
|
|
|
24 |
{
|
|
|
25 |
private RouteResolverInterface $routeResolver;
|
|
|
26 |
|
|
|
27 |
private RouteParserInterface $routeParser;
|
|
|
28 |
|
|
|
29 |
private ?RouteCollectorProxyInterface $routeCollectorProxy;
|
|
|
30 |
|
|
|
31 |
public function __construct(
|
|
|
32 |
RouteResolverInterface $routeResolver,
|
|
|
33 |
RouteParserInterface $routeParser,
|
|
|
34 |
?RouteCollectorProxyInterface $routeCollectorProxy = null
|
|
|
35 |
) {
|
|
|
36 |
$this->routeResolver = $routeResolver;
|
|
|
37 |
$this->routeParser = $routeParser;
|
|
|
38 |
$this->routeCollectorProxy = $routeCollectorProxy;
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
/**
|
|
|
42 |
* This request handler is instantiated automatically in App::__construct()
|
|
|
43 |
* It is at the very tip of the middleware queue meaning it will be executed
|
|
|
44 |
* last and it detects whether or not routing has been performed in the user
|
|
|
45 |
* defined middleware stack. In the event that the user did not perform routing
|
|
|
46 |
* it is done here
|
|
|
47 |
*
|
|
|
48 |
* @throws HttpNotFoundException
|
|
|
49 |
* @throws HttpMethodNotAllowedException
|
|
|
50 |
*/
|
|
|
51 |
public function handle(ServerRequestInterface $request): ResponseInterface
|
|
|
52 |
{
|
|
|
53 |
// If routing hasn't been done, then do it now so we can dispatch
|
|
|
54 |
if ($request->getAttribute(RouteContext::ROUTING_RESULTS) === null) {
|
|
|
55 |
$routingMiddleware = new RoutingMiddleware($this->routeResolver, $this->routeParser);
|
|
|
56 |
$request = $routingMiddleware->performRouting($request);
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
if ($this->routeCollectorProxy !== null) {
|
|
|
60 |
$request = $request->withAttribute(
|
|
|
61 |
RouteContext::BASE_PATH,
|
|
|
62 |
$this->routeCollectorProxy->getBasePath()
|
|
|
63 |
);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/** @var Route $route */
|
|
|
67 |
$route = $request->getAttribute(RouteContext::ROUTE);
|
|
|
68 |
return $route->run($request);
|
|
|
69 |
}
|
|
|
70 |
}
|