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\Middleware;
|
|
|
12 |
|
|
|
13 |
use Psr\Http\Message\ResponseInterface;
|
|
|
14 |
use Psr\Http\Message\ServerRequestInterface;
|
|
|
15 |
use Psr\Http\Server\MiddlewareInterface;
|
|
|
16 |
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
17 |
use RuntimeException;
|
|
|
18 |
use Slim\Exception\HttpMethodNotAllowedException;
|
|
|
19 |
use Slim\Exception\HttpNotFoundException;
|
|
|
20 |
use Slim\Interfaces\RouteParserInterface;
|
|
|
21 |
use Slim\Interfaces\RouteResolverInterface;
|
|
|
22 |
use Slim\Routing\RouteContext;
|
|
|
23 |
use Slim\Routing\RoutingResults;
|
|
|
24 |
|
|
|
25 |
class RoutingMiddleware implements MiddlewareInterface
|
|
|
26 |
{
|
|
|
27 |
protected RouteResolverInterface $routeResolver;
|
|
|
28 |
|
|
|
29 |
protected RouteParserInterface $routeParser;
|
|
|
30 |
|
|
|
31 |
public function __construct(RouteResolverInterface $routeResolver, RouteParserInterface $routeParser)
|
|
|
32 |
{
|
|
|
33 |
$this->routeResolver = $routeResolver;
|
|
|
34 |
$this->routeParser = $routeParser;
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
/**
|
|
|
38 |
* @throws HttpNotFoundException
|
|
|
39 |
* @throws HttpMethodNotAllowedException
|
|
|
40 |
* @throws RuntimeException
|
|
|
41 |
*/
|
|
|
42 |
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
|
|
43 |
{
|
|
|
44 |
$request = $this->performRouting($request);
|
|
|
45 |
return $handler->handle($request);
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Perform routing
|
|
|
50 |
*
|
|
|
51 |
* @param ServerRequestInterface $request PSR7 Server Request
|
|
|
52 |
*
|
|
|
53 |
* @throws HttpNotFoundException
|
|
|
54 |
* @throws HttpMethodNotAllowedException
|
|
|
55 |
* @throws RuntimeException
|
|
|
56 |
*/
|
|
|
57 |
public function performRouting(ServerRequestInterface $request): ServerRequestInterface
|
|
|
58 |
{
|
|
|
59 |
$request = $request->withAttribute(RouteContext::ROUTE_PARSER, $this->routeParser);
|
|
|
60 |
|
|
|
61 |
$routingResults = $this->resolveRoutingResultsFromRequest($request);
|
|
|
62 |
$routeStatus = $routingResults->getRouteStatus();
|
|
|
63 |
|
|
|
64 |
$request = $request->withAttribute(RouteContext::ROUTING_RESULTS, $routingResults);
|
|
|
65 |
|
|
|
66 |
switch ($routeStatus) {
|
|
|
67 |
case RoutingResults::FOUND:
|
|
|
68 |
$routeArguments = $routingResults->getRouteArguments();
|
|
|
69 |
$routeIdentifier = $routingResults->getRouteIdentifier() ?? '';
|
|
|
70 |
$route = $this->routeResolver
|
|
|
71 |
->resolveRoute($routeIdentifier)
|
|
|
72 |
->prepare($routeArguments);
|
|
|
73 |
return $request->withAttribute(RouteContext::ROUTE, $route);
|
|
|
74 |
|
|
|
75 |
case RoutingResults::NOT_FOUND:
|
|
|
76 |
throw new HttpNotFoundException($request);
|
|
|
77 |
|
|
|
78 |
case RoutingResults::METHOD_NOT_ALLOWED:
|
|
|
79 |
$exception = new HttpMethodNotAllowedException($request);
|
|
|
80 |
$exception->setAllowedMethods($routingResults->getAllowedMethods());
|
|
|
81 |
throw $exception;
|
|
|
82 |
|
|
|
83 |
default:
|
|
|
84 |
throw new RuntimeException('An unexpected error occurred while performing routing.');
|
|
|
85 |
}
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
/**
|
|
|
89 |
* Resolves the route from the given request
|
|
|
90 |
*/
|
|
|
91 |
protected function resolveRoutingResultsFromRequest(ServerRequestInterface $request): RoutingResults
|
|
|
92 |
{
|
|
|
93 |
return $this->routeResolver->computeRoutingResults(
|
|
|
94 |
$request->getUri()->getPath(),
|
|
|
95 |
$request->getMethod()
|
|
|
96 |
);
|
|
|
97 |
}
|
|
|
98 |
}
|