| 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 FastRoute\Dispatcher\GroupCountBased;
|
|
|
14 |
|
|
|
15 |
class FastRouteDispatcher extends GroupCountBased
|
|
|
16 |
{
|
|
|
17 |
/**
|
|
|
18 |
* @var string[][]
|
|
|
19 |
*/
|
|
|
20 |
private array $allowedMethods = [];
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* @param string $httpMethod
|
|
|
24 |
* @param string $uri
|
|
|
25 |
*
|
|
|
26 |
* @return array{int, string|null, array<string, string>}
|
|
|
27 |
*/
|
|
|
28 |
public function dispatch($httpMethod, $uri): array
|
|
|
29 |
{
|
|
|
30 |
$routingResults = $this->routingResults($httpMethod, $uri);
|
|
|
31 |
if ($routingResults[0] === self::FOUND) {
|
|
|
32 |
return $routingResults;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
// For HEAD requests, attempt fallback to GET
|
|
|
36 |
if ($httpMethod === 'HEAD') {
|
|
|
37 |
$routingResults = $this->routingResults('GET', $uri);
|
|
|
38 |
if ($routingResults[0] === self::FOUND) {
|
|
|
39 |
return $routingResults;
|
|
|
40 |
}
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
// If nothing else matches, try fallback routes
|
|
|
44 |
$routingResults = $this->routingResults('*', $uri);
|
|
|
45 |
if ($routingResults[0] === self::FOUND) {
|
|
|
46 |
return $routingResults;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
if (!empty($this->getAllowedMethods($uri))) {
|
|
|
50 |
return [self::METHOD_NOT_ALLOWED, null, []];
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
return [self::NOT_FOUND, null, []];
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* @param string $httpMethod
|
|
|
58 |
* @param string $uri
|
|
|
59 |
*
|
|
|
60 |
* @return array{int, string|null, array<string, string>}
|
|
|
61 |
*/
|
|
|
62 |
private function routingResults(string $httpMethod, string $uri): array
|
|
|
63 |
{
|
|
|
64 |
if (isset($this->staticRouteMap[$httpMethod][$uri])) {
|
|
|
65 |
/** @var string $routeIdentifier */
|
|
|
66 |
$routeIdentifier = $this->staticRouteMap[$httpMethod][$uri];
|
|
|
67 |
return [self::FOUND, $routeIdentifier, []];
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
if (isset($this->variableRouteData[$httpMethod])) {
|
|
|
71 |
/** @var array{0: int, 1?: string, 2?: array<string, string>} $result */
|
|
|
72 |
$result = $this->dispatchVariableRoute($this->variableRouteData[$httpMethod], $uri);
|
|
|
73 |
if ($result[0] === self::FOUND) {
|
|
|
74 |
/** @var array{int, string, array<string, string>} $result */
|
|
|
75 |
return [self::FOUND, $result[1], $result[2]];
|
|
|
76 |
}
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
return [self::NOT_FOUND, null, []];
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* @param string $uri
|
|
|
84 |
*
|
|
|
85 |
* @return string[]
|
|
|
86 |
*/
|
|
|
87 |
public function getAllowedMethods(string $uri): array
|
|
|
88 |
{
|
|
|
89 |
if (isset($this->allowedMethods[$uri])) {
|
|
|
90 |
return $this->allowedMethods[$uri];
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
$allowedMethods = [];
|
|
|
94 |
foreach ($this->staticRouteMap as $method => $uriMap) {
|
|
|
95 |
if (isset($uriMap[$uri])) {
|
|
|
96 |
$allowedMethods[$method] = true;
|
|
|
97 |
}
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
foreach ($this->variableRouteData as $method => $routeData) {
|
|
|
101 |
$result = $this->dispatchVariableRoute($routeData, $uri);
|
|
|
102 |
if ($result[0] === self::FOUND) {
|
|
|
103 |
$allowedMethods[$method] = true;
|
|
|
104 |
}
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
return $this->allowedMethods[$uri] = array_keys($allowedMethods);
|
|
|
108 |
}
|
|
|
109 |
}
|