Do not allow routing to continue after a dispatched StaticRoute returns null

This commit is contained in:
PJ Dietz 2015-01-02 12:31:02 -05:00
parent 78fe57d736
commit caef817535
4 changed files with 12 additions and 38 deletions

View File

@ -1,10 +1,10 @@
<?php <?php
/** /**
* pjdietz\WellRESTed\Interfaces\ResponseInterface * pjdietz\WellRESTed\Interfaces\Route\StaticRouteInterface
* *
* @author PJ Dietz <pj@pjdietz.com> * @author PJ Dietz <pj@pjdietz.com>
* @copyright Copyright 2014 by PJ Dietz * @copyright Copyright 2015 by PJ Dietz
* @license MIT * @license MIT
*/ */

View File

@ -99,7 +99,7 @@ class Router implements HandlerInterface
* A route for an exact match to a path. * A route for an exact match to a path.
* *
* @param string|array $paths Path component of the URI or a list of paths * @param string|array $paths Path component of the URI or a list of paths
* @param string $handler Fully qualified name to an autoloadable handler class. * @param string $handler Fully qualified name to an autoloadable handler class
*/ */
public function setStaticRoute($paths, $handler) public function setStaticRoute($paths, $handler)
{ {
@ -180,10 +180,7 @@ class Router implements HandlerInterface
// First check if there is a handler for this exact path. // First check if there is a handler for this exact path.
$handler = $this->getStaticHandler($path); $handler = $this->getStaticHandler($path);
if ($handler) { if ($handler) {
$reponse = $this->tryResponse($handler, $request, $args); return $this->tryResponse($handler, $request, $args);
if ($reponse) {
return $reponse;
}
} }
// Try each of the routes. // Try each of the routes.

View File

@ -11,7 +11,6 @@
namespace pjdietz\WellRESTed\Routes; namespace pjdietz\WellRESTed\Routes;
use InvalidArgumentException; use InvalidArgumentException;
use pjdietz\WellRESTed\Interfaces\HandlerInterface;
use pjdietz\WellRESTed\Interfaces\RequestInterface; use pjdietz\WellRESTed\Interfaces\RequestInterface;
use pjdietz\WellRESTed\Interfaces\Routes\StaticRouteInterface; use pjdietz\WellRESTed\Interfaces\Routes\StaticRouteInterface;
@ -21,24 +20,24 @@ use pjdietz\WellRESTed\Interfaces\Routes\StaticRouteInterface;
class StaticRoute extends BaseRoute implements StaticRouteInterface class StaticRoute extends BaseRoute implements StaticRouteInterface
{ {
/** @var array List of static URI paths */ /** @var array List of static URI paths */
protected $paths; private $paths;
/** /**
* Create a new StaticRoute for a given path or paths and a handler class. * Create a new StaticRoute for a given path or paths and a handler class.
* *
* @param string|array $paths Path or list of paths the request must match * @param string|array $prefixes Path or list of paths the request must match
* @param string $targetClassName Fully qualified name to an autoloadable handler class. * @param string $targetClassName Fully qualified name to an autoloadable handler class.
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public function __construct($paths, $targetClassName) public function __construct($prefixes, $targetClassName)
{ {
parent::__construct($targetClassName); parent::__construct($targetClassName);
if (is_string($paths)) { if (is_string($prefixes)) {
$this->paths = array($paths); $this->paths = array($prefixes);
} elseif (is_array($paths)) { } elseif (is_array($prefixes)) {
$this->paths = $paths; $this->paths = $prefixes;
} else { } else {
throw new InvalidArgumentException("$paths must be a string or array of string"); throw new InvalidArgumentException("$prefixes must be a string or array of string");
} }
} }

View File

@ -48,20 +48,6 @@ class RouterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(200, $resp->getStatusCode()); $this->assertEquals(200, $resp->getStatusCode());
} }
public function testTryRoutesAfterNullStaticRoute()
{
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any())
->method('getPath')
->will($this->returnValue("/cat/"));
$router = new Router();
$router->setStaticRoute("/cat/", __NAMESPACE__ . '\\NullResponseHandler');
$router->addRoute(new TemplateRoute("/cat/*", __NAMESPACE__ . '\\RouterTestHandler'));
$resp = $router->getResponse($mockRequest);
$this->assertEquals(200, $resp->getStatusCode());
}
public function testRespondWithDefaultErrorForException() public function testRespondWithDefaultErrorForException()
{ {
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); $mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
@ -362,11 +348,3 @@ class InjectionHandler implements HandlerInterface
return $response; return $response;
} }
} }
class NullResponseHandler implements HandlerInterface
{
public function getResponse(RequestInterface $request, array $args = null)
{
return null;
}
}