Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4.x - Refactor FastRouteDispatcher #2832

Merged
merged 1 commit into from Sep 9, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 24 additions & 33 deletions Slim/Routing/FastRouteDispatcher.php
Expand Up @@ -21,64 +21,56 @@ class FastRouteDispatcher extends GroupCountBased
/**
* @param string $httpMethod
* @param string $uri
*
* @return array
*/
public function dispatch($httpMethod, $uri): array
{
if (isset($this->staticRouteMap[$httpMethod][$uri])) {
return [self::FOUND, $this->staticRouteMap[$httpMethod][$uri], []];
}

$varRouteData = $this->variableRouteData;
if (isset($varRouteData[$httpMethod])) {
$result = $this->dispatchVariableRoute($varRouteData[$httpMethod], $uri);
$routingResults = $this->routingResultsFromVariableRouteResults($result);
if ($routingResults[0] === self::FOUND) {
return $routingResults;
}
$routingResults = $this->routingResults($httpMethod, $uri);
if ($routingResults[0] === self::FOUND) {
return $routingResults;
}

// For HEAD requests, attempt fallback to GET
if ($httpMethod === 'HEAD') {
if (isset($this->staticRouteMap['GET'][$uri])) {
return [self::FOUND, $this->staticRouteMap['GET'][$uri], []];
}
if (isset($varRouteData['GET'])) {
$result = $this->dispatchVariableRoute($varRouteData['GET'], $uri);
return $this->routingResultsFromVariableRouteResults($result);
$routingResults = $this->routingResults('GET', $uri);
if ($routingResults[0] === self::FOUND) {
return $routingResults;
}
}

// If nothing else matches, try fallback routes
if (isset($this->staticRouteMap['*'][$uri])) {
return [self::FOUND, $this->staticRouteMap['*'][$uri], []];
}
if (isset($varRouteData['*'])) {
$result = $this->dispatchVariableRoute($varRouteData['*'], $uri);
return $this->routingResultsFromVariableRouteResults($result);
$routingResults = $this->routingResults('*', $uri);
if ($routingResults[0] === self::FOUND) {
return $routingResults;
}

if (count($this->getAllowedMethods($uri))) {
if (!empty($this->getAllowedMethods($uri))) {
return [self::METHOD_NOT_ALLOWED, null, []];
}

return [self::NOT_FOUND, null, []];
}

/**
* @param array $result
* @return array
*/
protected function routingResultsFromVariableRouteResults(array $result): array
private function routingResults(string $httpMethod, string $uri): array
{
if ($result[0] === self::FOUND) {
return [self::FOUND, $result[1], $result[2]];
if (isset($this->staticRouteMap[$httpMethod][$uri])) {
return [self::FOUND, $this->staticRouteMap[$httpMethod][$uri], []];
}

if (isset($this->variableRouteData[$httpMethod])) {
$result = $this->dispatchVariableRoute($this->variableRouteData[$httpMethod], $uri);
if ($result[0] === self::FOUND) {
return [self::FOUND, $result[1], $result[2]];
}
}

return [self::NOT_FOUND, null, []];
}

/**
* @param string $uri
*
* @return array
*/
public function getAllowedMethods(string $uri): array
Expand All @@ -94,8 +86,7 @@ public function getAllowedMethods(string $uri): array
}
}

$varRouteData = $this->variableRouteData;
foreach ($varRouteData as $method => $routeData) {
foreach ($this->variableRouteData as $method => $routeData) {
$result = $this->dispatchVariableRoute($routeData, $uri);
if ($result[0] === self::FOUND) {
$this->allowedMethods[$uri][] = $method;
Expand Down