Dispatcher creates DispatchStack for array
This commit is contained in:
parent
3786cfaade
commit
3811b9085f
|
|
@ -22,6 +22,8 @@ class Dispatcher implements DispatcherInterface
|
|||
$middleware = $middleware($request, $response, $next);
|
||||
} elseif (is_string($middleware)) {
|
||||
$middleware = new $middleware();
|
||||
} elseif (is_array($middleware)) {
|
||||
$middleware = $this->getDispatchStack($middleware);
|
||||
}
|
||||
if ($middleware instanceof MiddlewareInterface) {
|
||||
return $middleware->dispatch($request, $response, $next);
|
||||
|
|
@ -31,4 +33,13 @@ class Dispatcher implements DispatcherInterface
|
|||
throw new DispatchException("Unable to dispatch middleware.");
|
||||
}
|
||||
}
|
||||
|
||||
protected function getDispatchStack($middlewares)
|
||||
{
|
||||
$stack = new DispatchStack($this);
|
||||
foreach ($middlewares as $middleware) {
|
||||
$stack->add($middleware);
|
||||
}
|
||||
return $stack;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -74,7 +74,19 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* @expectedException \InvalidArgumentException
|
||||
* @uses WellRESTed\Dispatching\DispatchStack
|
||||
*/
|
||||
public function testDispatchesArrayAsDispatchStack()
|
||||
{
|
||||
$middleware = new DispatcherTest_Middleware();
|
||||
|
||||
$dispatcher = new Dispatcher();
|
||||
$response = $dispatcher->dispatch([$middleware], $this->request->reveal(), $this->response->reveal(), $this->next);
|
||||
$this->assertEquals(200, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \WellRESTed\Dispatching\DispatchException
|
||||
*/
|
||||
public function testThrowsExceptionWhenUnableToDispatch()
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in New Issue