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)
|
public function __construct($target, $methodMap)
|
||||||
{
|
{
|
||||||
$this->target = rtrim($target, "*");
|
parent::__construct(rtrim($target, "*"), $methodMap);
|
||||||
$this->methodMap = $methodMap;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getType()
|
public function getType()
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ use WellRESTed\Routing\Route\RouteInterface;
|
||||||
|
|
||||||
class Router implements RouterInterface
|
class Router implements RouterInterface
|
||||||
{
|
{
|
||||||
/** @var string ServerRequestInterface attribute name for matched path variables */
|
/** @var string attribute name for matched path variables */
|
||||||
private $pathVariablesAttributeName;
|
private $pathVariablesAttributeName;
|
||||||
/** @var DispatcherInterface */
|
/** @var DispatcherInterface */
|
||||||
private $dispatcher;
|
private $dispatcher;
|
||||||
|
|
@ -183,21 +183,30 @@ class Router implements RouterInterface
|
||||||
$matches = array_filter(
|
$matches = array_filter(
|
||||||
$prefixes,
|
$prefixes,
|
||||||
function ($prefix) use ($requestTarget) {
|
function ($prefix) use ($requestTarget) {
|
||||||
return (strrpos($requestTarget, $prefix, -strlen($requestTarget)) !== false);
|
return $this->startsWith($requestTarget, $prefix);
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
if ($matches) {
|
if ($matches) {
|
||||||
if (count($matches) > 0) {
|
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) {
|
$compareByLength = function ($a, $b) {
|
||||||
return strlen($b) - strlen($a);
|
return strlen($b) - strlen($a);
|
||||||
};
|
};
|
||||||
usort($matches, $compareByLength);
|
usort($matches, $compareByLength);
|
||||||
}
|
}
|
||||||
$route = $this->prefixRoutes[$matches[0]];
|
/** @var string $bestMatch */
|
||||||
|
$bestMatch = $matches[0];
|
||||||
|
$route = $this->prefixRoutes[$bestMatch];
|
||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
return null;
|
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 */
|
/** @var int */
|
||||||
private $chunkSize = 0;
|
private $chunkSize = 0;
|
||||||
|
/** @var DispatcherInterface */
|
||||||
|
private $dispatcher;
|
||||||
|
|
||||||
public function __construct(DispatcherInterface $dispatcher = null)
|
public function __construct(DispatcherInterface $dispatcher = null)
|
||||||
{
|
{
|
||||||
|
|
@ -34,8 +36,10 @@ class Transmitter implements TransmitterInterface
|
||||||
* @param ServerRequestInterface $request
|
* @param ServerRequestInterface $request
|
||||||
* @param ResponseInterface $response Response to output
|
* @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.
|
// Prepare the response for output.
|
||||||
$response = $this->prepareResponse($request, $response);
|
$response = $this->prepareResponse($request, $response);
|
||||||
|
|
||||||
|
|
@ -64,8 +68,10 @@ class Transmitter implements TransmitterInterface
|
||||||
$this->chunkSize = $chunkSize;
|
$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:
|
// Add a Content-length header to the response when all of these are true:
|
||||||
//
|
//
|
||||||
// - Response does not have a Content-length header
|
// - Response does not have a Content-length header
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue