diff --git a/src/Routing/Route/PrefixRoute.php b/src/Routing/Route/PrefixRoute.php index 287529a..37a0d0f 100644 --- a/src/Routing/Route/PrefixRoute.php +++ b/src/Routing/Route/PrefixRoute.php @@ -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() diff --git a/src/Routing/Router.php b/src/Routing/Router.php index 8ec4bf2..cf009a2 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -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); + } } diff --git a/src/Transmission/Transmitter.php b/src/Transmission/Transmitter.php index 2d2be36..7dfe965 100644 --- a/src/Transmission/Transmitter.php +++ b/src/Transmission/Transmitter.php @@ -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