Router optionally takes a DispatcherInterface on construction
This commit is contained in:
parent
3811b9085f
commit
f849a6ff89
|
|
@ -1,19 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
namespace WellRESTed\Dispatching;
|
|
||||||
|
|
||||||
interface DispatchProviderInterface
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* @return DispatcherInterface
|
|
||||||
*/
|
|
||||||
public function getDispatcher();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Return a DispatchStackInterface for a list array of middleware.
|
|
||||||
*
|
|
||||||
* @param mixed[] $middlewares
|
|
||||||
* @return DispatchStackInterface
|
|
||||||
*/
|
|
||||||
public function getDispatchStack($middlewares);
|
|
||||||
}
|
|
||||||
|
|
@ -4,15 +4,16 @@ namespace WellRESTed\Routing;
|
||||||
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use WellRESTed\Dispatching\DispatchProviderInterface;
|
use WellRESTed\Dispatching\Dispatcher;
|
||||||
|
use WellRESTed\Dispatching\DispatcherInterface;
|
||||||
use WellRESTed\Routing\Route\RouteFactory;
|
use WellRESTed\Routing\Route\RouteFactory;
|
||||||
use WellRESTed\Routing\Route\RouteFactoryInterface;
|
use WellRESTed\Routing\Route\RouteFactoryInterface;
|
||||||
use WellRESTed\Routing\Route\RouteInterface;
|
use WellRESTed\Routing\Route\RouteInterface;
|
||||||
|
|
||||||
class Router implements RouterInterface
|
class Router implements RouterInterface
|
||||||
{
|
{
|
||||||
/** @var DispatchProviderInterface */
|
/** @var DispatcherInterface */
|
||||||
private $dispatchProvider;
|
private $dispatcher;
|
||||||
/** @var RouteFactoryInterface */
|
/** @var RouteFactoryInterface */
|
||||||
private $factory;
|
private $factory;
|
||||||
/** @var RouteInterface[] Array of Route objects */
|
/** @var RouteInterface[] Array of Route objects */
|
||||||
|
|
@ -24,10 +25,13 @@ class Router implements RouterInterface
|
||||||
/** @var RouteInterface[] Hash array mapping path prefixes to routes */
|
/** @var RouteInterface[] Hash array mapping path prefixes to routes */
|
||||||
private $patternRoutes;
|
private $patternRoutes;
|
||||||
|
|
||||||
public function __construct(DispatchProviderInterface $dispatchProvider)
|
public function __construct(DispatcherInterface $dispatcher = null)
|
||||||
{
|
{
|
||||||
$this->dispatchProvider = $dispatchProvider;
|
if ($dispatcher === null) {
|
||||||
$this->factory = $this->getRouteFactory($this->dispatchProvider->getDispatcher());
|
$dispatcher = new Dispatcher();
|
||||||
|
}
|
||||||
|
$this->dispatcher = $dispatcher;
|
||||||
|
$this->factory = $this->getRouteFactory($this->dispatcher);
|
||||||
$this->routes = [];
|
$this->routes = [];
|
||||||
$this->staticRoutes = [];
|
$this->staticRoutes = [];
|
||||||
$this->prefixRoutes = [];
|
$this->prefixRoutes = [];
|
||||||
|
|
@ -65,9 +69,6 @@ class Router implements RouterInterface
|
||||||
public function register($method, $target, $middleware)
|
public function register($method, $target, $middleware)
|
||||||
{
|
{
|
||||||
$route = $this->getRouteForTarget($target);
|
$route = $this->getRouteForTarget($target);
|
||||||
if (is_array($middleware)) {
|
|
||||||
$middleware = $this->dispatchProvider->getDispatchStack($middleware);
|
|
||||||
}
|
|
||||||
$route->getMethodMap()->register($method, $middleware);
|
$route->getMethodMap()->register($method, $middleware);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,6 @@ use Prophecy\Argument;
|
||||||
use WellRESTed\Routing\Route\RouteInterface;
|
use WellRESTed\Routing\Route\RouteInterface;
|
||||||
use WellRESTed\Routing\Router;
|
use WellRESTed\Routing\Router;
|
||||||
|
|
||||||
// TODO: register with array of middleware creates stack
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @coversDefaultClass WellRESTed\Routing\Router
|
* @coversDefaultClass WellRESTed\Routing\Router
|
||||||
* @uses WellRESTed\Routing\Router
|
* @uses WellRESTed\Routing\Router
|
||||||
|
|
@ -16,7 +14,6 @@ use WellRESTed\Routing\Router;
|
||||||
class RouterTest extends \PHPUnit_Framework_TestCase
|
class RouterTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
private $dispatcher;
|
private $dispatcher;
|
||||||
private $dispatchProvider;
|
|
||||||
private $methodMap;
|
private $methodMap;
|
||||||
private $factory;
|
private $factory;
|
||||||
private $request;
|
private $request;
|
||||||
|
|
@ -55,9 +52,6 @@ class RouterTest extends \PHPUnit_Framework_TestCase
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->dispatchProvider = $this->prophesize('WellRESTed\Dispatching\DispatchProviderInterface');
|
|
||||||
$this->dispatchProvider->getDispatcher()->willReturn($this->dispatcher->reveal());
|
|
||||||
|
|
||||||
$this->router = $this->getMockBuilder('WellRESTed\Routing\Router')
|
$this->router = $this->getMockBuilder('WellRESTed\Routing\Router')
|
||||||
->setMethods(["getRouteFactory"])
|
->setMethods(["getRouteFactory"])
|
||||||
->disableOriginalConstructor()
|
->disableOriginalConstructor()
|
||||||
|
|
@ -65,7 +59,7 @@ class RouterTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->router->expects($this->any())
|
$this->router->expects($this->any())
|
||||||
->method("getRouteFactory")
|
->method("getRouteFactory")
|
||||||
->will($this->returnValue($this->factory->reveal()));
|
->will($this->returnValue($this->factory->reveal()));
|
||||||
$this->router->__construct($this->dispatchProvider->reveal());
|
$this->router->__construct($this->dispatcher->reveal());
|
||||||
}
|
}
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
|
|
@ -78,7 +72,19 @@ class RouterTest extends \PHPUnit_Framework_TestCase
|
||||||
*/
|
*/
|
||||||
public function testCreatesInstance()
|
public function testCreatesInstance()
|
||||||
{
|
{
|
||||||
$routeMap = new Router($this->dispatchProvider->reveal());
|
$routeMap = new Router($this->dispatcher->reveal());
|
||||||
|
$this->assertNotNull($routeMap);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers ::__construct
|
||||||
|
* @covers ::getRouteFactory
|
||||||
|
* @uses WellRESTed\Routing\Route\RouteFactory
|
||||||
|
* @uses WellRESTed\Dispatching\Dispatcher
|
||||||
|
*/
|
||||||
|
public function testCreatesInstanceWithDispatcherByDefault()
|
||||||
|
{
|
||||||
|
$routeMap = new Router();
|
||||||
$this->assertNotNull($routeMap);
|
$this->assertNotNull($routeMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -116,18 +122,6 @@ class RouterTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->methodMap->register("GET", "middleware")->shouldHaveBeenCalled();
|
$this->methodMap->register("GET", "middleware")->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @covers ::register
|
|
||||||
*/
|
|
||||||
public function testCreatesDispatchStackForMiddlewareArray()
|
|
||||||
{
|
|
||||||
$stack = $this->prophesize('WellRESTed\MiddlewareInterface');
|
|
||||||
$this->dispatchProvider->getDispatchStack(Argument::any())->willReturn($stack->reveal());
|
|
||||||
|
|
||||||
$this->router->register("GET", "/", ["middleware1", "middleware2"]);
|
|
||||||
$this->methodMap->register("GET", $stack->reveal())->shouldHaveBeenCalled();
|
|
||||||
}
|
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
// Dispatching
|
// Dispatching
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue