diff --git a/src/Dispatching/Dispatcher.php b/src/Dispatching/Dispatcher.php index 3b8f671..45d33c7 100644 --- a/src/Dispatching/Dispatcher.php +++ b/src/Dispatching/Dispatcher.php @@ -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; + } } diff --git a/test/tests/unit/Dispatching/DispatcherTest.php b/test/tests/unit/Dispatching/DispatcherTest.php index d09fc27..c3b7c83 100644 --- a/test/tests/unit/Dispatching/DispatcherTest.php +++ b/test/tests/unit/Dispatching/DispatcherTest.php @@ -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() {