Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
991 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
/** | ||
* Slim Framework (https://slimframework.com) | ||
* | ||
* @link https://github.com/slimphp/Slim | ||
* @copyright Copyright (c) 2011-2018 Josh Lockhart | ||
* @license https://github.com/slimphp/Slim/blob/4.x/LICENSE.md (MIT License) | ||
*/ | ||
namespace Slim; | ||
|
||
use FastRoute\Dispatcher\GroupCountBased; | ||
|
||
class Dispatcher extends GroupCountBased | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $httpMethod; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $uri; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $allowedMethods = []; | ||
|
||
/** | ||
* @param string $httpMethod | ||
* @param string $uri | ||
* @return RoutingResults | ||
*/ | ||
public function dispatch($httpMethod, $uri) | ||
{ | ||
$this->httpMethod = $httpMethod; | ||
$this->uri = $uri; | ||
|
||
if (isset($this->staticRouteMap[$httpMethod][$uri])) { | ||
return $this->routingResults(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->getRouteStatus() === Dispatcher::FOUND) { | ||
return $routingResults; | ||
} | ||
} | ||
|
||
// For HEAD requests, attempt fallback to GET | ||
if ($httpMethod === 'HEAD') { | ||
if (isset($this->staticRouteMap['GET'][$uri])) { | ||
return $this->routingResults(self::FOUND, $this->staticRouteMap['GET'][$uri]); | ||
} | ||
if (isset($varRouteData['GET'])) { | ||
$result = $this->dispatchVariableRoute($varRouteData['GET'], $uri); | ||
return $this->routingResultsFromVariableRouteResults($result); | ||
} | ||
} | ||
|
||
// If nothing else matches, try fallback routes | ||
if (isset($this->staticRouteMap['*'][$uri])) { | ||
return $this->routingResults(self::FOUND, $this->staticRouteMap['*'][$uri]); | ||
} | ||
if (isset($varRouteData['*'])) { | ||
$result = $this->dispatchVariableRoute($varRouteData['*'], $uri); | ||
return $this->routingResultsFromVariableRouteResults($result); | ||
} | ||
|
||
if (count($this->getAllowedMethods($httpMethod, $uri))) { | ||
return $this->routingResults(self::METHOD_NOT_ALLOWED); | ||
} | ||
|
||
return $this->routingResults(self::NOT_FOUND); | ||
} | ||
|
||
/** | ||
* @param $status | ||
* @param null $handler | ||
* @param array $arguments | ||
* @return RoutingResults | ||
*/ | ||
protected function routingResults($status, $handler = null, $arguments = []) | ||
{ | ||
return new RoutingResults($this, $this->httpMethod, $this->uri, $status, $handler, $arguments); | ||
} | ||
|
||
/** | ||
* @param array $result | ||
* @return RoutingResults | ||
*/ | ||
protected function routingResultsFromVariableRouteResults($result) | ||
{ | ||
if ($result[0] === self::FOUND) { | ||
return $this->routingResults(self::FOUND, $result[1], $result[2]); | ||
} | ||
return $this->routingResults(self::NOT_FOUND); | ||
} | ||
|
||
/** | ||
* @param string $httpMethod | ||
* @param string $uri | ||
* @return array | ||
*/ | ||
public function getAllowedMethods($httpMethod, $uri) | ||
{ | ||
if (isset($this->allowedMethods[$uri])) { | ||
return $this->allowedMethods[$uri]; | ||
} | ||
|
||
$this->allowedMethods[$uri] = []; | ||
foreach ($this->staticRouteMap as $method => $uriMap) { | ||
if ($method !== $httpMethod && isset($uriMap[$uri])) { | ||
$this->allowedMethods[$uri][] = $method; | ||
} | ||
} | ||
|
||
$varRouteData = $this->variableRouteData; | ||
foreach ($varRouteData as $method => $routeData) { | ||
$result = $this->dispatchVariableRoute($routeData, $uri); | ||
if ($result[0] === self::FOUND) { | ||
$this->allowedMethods[$uri][] = $method; | ||
} | ||
} | ||
|
||
return $this->allowedMethods[$uri]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.