CharCountBased.php
7 years ago
GroupCountBased.php
7 years ago
GroupPosBased.php
7 years ago
MarkBased.php
7 years ago
RegexBasedAbstract.php
7 years ago
RegexBasedAbstract.php
89 lines
| 1 | <?php |
| 2 | |
| 3 | namespace FastRoute\Dispatcher; |
| 4 | |
| 5 | use FastRoute\Dispatcher; |
| 6 | |
| 7 | abstract class RegexBasedAbstract implements Dispatcher |
| 8 | { |
| 9 | /** @var mixed[][] */ |
| 10 | protected $staticRouteMap = []; |
| 11 | |
| 12 | /** @var mixed[] */ |
| 13 | protected $variableRouteData = []; |
| 14 | |
| 15 | /** |
| 16 | * @return mixed[] |
| 17 | */ |
| 18 | abstract protected function dispatchVariableRoute($routeData, $uri); |
| 19 | |
| 20 | public function dispatch($httpMethod, $uri) |
| 21 | { |
| 22 | if (isset($this->staticRouteMap[$httpMethod][$uri])) { |
| 23 | $handler = $this->staticRouteMap[$httpMethod][$uri]; |
| 24 | return [self::FOUND, $handler, []]; |
| 25 | } |
| 26 | |
| 27 | $varRouteData = $this->variableRouteData; |
| 28 | if (isset($varRouteData[$httpMethod])) { |
| 29 | $result = $this->dispatchVariableRoute($varRouteData[$httpMethod], $uri); |
| 30 | if ($result[0] === self::FOUND) { |
| 31 | return $result; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // For HEAD requests, attempt fallback to GET |
| 36 | if ($httpMethod === 'HEAD') { |
| 37 | if (isset($this->staticRouteMap['GET'][$uri])) { |
| 38 | $handler = $this->staticRouteMap['GET'][$uri]; |
| 39 | return [self::FOUND, $handler, []]; |
| 40 | } |
| 41 | if (isset($varRouteData['GET'])) { |
| 42 | $result = $this->dispatchVariableRoute($varRouteData['GET'], $uri); |
| 43 | if ($result[0] === self::FOUND) { |
| 44 | return $result; |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | // If nothing else matches, try fallback routes |
| 50 | if (isset($this->staticRouteMap['*'][$uri])) { |
| 51 | $handler = $this->staticRouteMap['*'][$uri]; |
| 52 | return [self::FOUND, $handler, []]; |
| 53 | } |
| 54 | if (isset($varRouteData['*'])) { |
| 55 | $result = $this->dispatchVariableRoute($varRouteData['*'], $uri); |
| 56 | if ($result[0] === self::FOUND) { |
| 57 | return $result; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Find allowed methods for this URI by matching against all other HTTP methods as well |
| 62 | $allowedMethods = []; |
| 63 | |
| 64 | foreach ($this->staticRouteMap as $method => $uriMap) { |
| 65 | if ($method !== $httpMethod && isset($uriMap[$uri])) { |
| 66 | $allowedMethods[] = $method; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | foreach ($varRouteData as $method => $routeData) { |
| 71 | if ($method === $httpMethod) { |
| 72 | continue; |
| 73 | } |
| 74 | |
| 75 | $result = $this->dispatchVariableRoute($routeData, $uri); |
| 76 | if ($result[0] === self::FOUND) { |
| 77 | $allowedMethods[] = $method; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // If there are no allowed methods the route simply does not exist |
| 82 | if ($allowedMethods) { |
| 83 | return [self::METHOD_NOT_ALLOWED, $allowedMethods]; |
| 84 | } |
| 85 | |
| 86 | return [self::NOT_FOUND]; |
| 87 | } |
| 88 | } |
| 89 |