Minor refactor of Router; various cleanup
This commit is contained in:
parent
76d952b076
commit
3a77d99e00
|
|
@ -6,8 +6,7 @@ class PrefixRoute extends Route
|
|||
{
|
||||
public function __construct($target, $methodMap)
|
||||
{
|
||||
$this->target = rtrim($target, "*");
|
||||
$this->methodMap = $methodMap;
|
||||
parent::__construct(rtrim($target, "*"), $methodMap);
|
||||
}
|
||||
|
||||
public function getType()
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ use WellRESTed\Routing\Route\RouteInterface;
|
|||
|
||||
class Router implements RouterInterface
|
||||
{
|
||||
/** @var string ServerRequestInterface attribute name for matched path variables */
|
||||
/** @var string attribute name for matched path variables */
|
||||
private $pathVariablesAttributeName;
|
||||
/** @var DispatcherInterface */
|
||||
private $dispatcher;
|
||||
|
|
@ -183,21 +183,30 @@ class Router implements RouterInterface
|
|||
$matches = array_filter(
|
||||
$prefixes,
|
||||
function ($prefix) use ($requestTarget) {
|
||||
return (strrpos($requestTarget, $prefix, -strlen($requestTarget)) !== false);
|
||||
return $this->startsWith($requestTarget, $prefix);
|
||||
}
|
||||
);
|
||||
|
||||
if ($matches) {
|
||||
if (count($matches) > 0) {
|
||||
// If there are multiple matches, sort them to find the one with the longest string length.
|
||||
// If there are multiple matches, sort them to find the one with
|
||||
// the longest string length.
|
||||
$compareByLength = function ($a, $b) {
|
||||
return strlen($b) - strlen($a);
|
||||
};
|
||||
usort($matches, $compareByLength);
|
||||
}
|
||||
$route = $this->prefixRoutes[$matches[0]];
|
||||
/** @var string $bestMatch */
|
||||
$bestMatch = $matches[0];
|
||||
$route = $this->prefixRoutes[$bestMatch];
|
||||
return $route;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function startsWith($haystack, $needle)
|
||||
{
|
||||
$length = strlen($needle);
|
||||
return (substr($haystack, 0, $length) === $needle);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@ class Transmitter implements TransmitterInterface
|
|||
{
|
||||
/** @var int */
|
||||
private $chunkSize = 0;
|
||||
/** @var DispatcherInterface */
|
||||
private $dispatcher;
|
||||
|
||||
public function __construct(DispatcherInterface $dispatcher = null)
|
||||
{
|
||||
|
|
@ -34,8 +36,10 @@ class Transmitter implements TransmitterInterface
|
|||
* @param ServerRequestInterface $request
|
||||
* @param ResponseInterface $response Response to output
|
||||
*/
|
||||
public function transmit(ServerRequestInterface $request, ResponseInterface $response)
|
||||
{
|
||||
public function transmit(
|
||||
ServerRequestInterface $request,
|
||||
ResponseInterface $response
|
||||
) {
|
||||
// Prepare the response for output.
|
||||
$response = $this->prepareResponse($request, $response);
|
||||
|
||||
|
|
@ -64,8 +68,10 @@ class Transmitter implements TransmitterInterface
|
|||
$this->chunkSize = $chunkSize;
|
||||
}
|
||||
|
||||
protected function prepareResponse(ServerRequestInterface $request, ResponseInterface $response)
|
||||
{
|
||||
protected function prepareResponse(
|
||||
ServerRequestInterface $request,
|
||||
ResponseInterface $response
|
||||
) {
|
||||
// Add a Content-length header to the response when all of these are true:
|
||||
//
|
||||
// - Response does not have a Content-length header
|
||||
|
|
|
|||
Loading…
Reference in New Issue