Dispatcher creates DispatchStack for array

This commit is contained in:
PJ Dietz 2015-05-10 13:53:15 -04:00
parent 3786cfaade
commit 3811b9085f
2 changed files with 24 additions and 1 deletions

View File

@ -22,6 +22,8 @@ class Dispatcher implements DispatcherInterface
$middleware = $middleware($request, $response, $next); $middleware = $middleware($request, $response, $next);
} elseif (is_string($middleware)) { } elseif (is_string($middleware)) {
$middleware = new $middleware(); $middleware = new $middleware();
} elseif (is_array($middleware)) {
$middleware = $this->getDispatchStack($middleware);
} }
if ($middleware instanceof MiddlewareInterface) { if ($middleware instanceof MiddlewareInterface) {
return $middleware->dispatch($request, $response, $next); return $middleware->dispatch($request, $response, $next);
@ -31,4 +33,13 @@ class Dispatcher implements DispatcherInterface
throw new DispatchException("Unable to dispatch middleware."); throw new DispatchException("Unable to dispatch middleware.");
} }
} }
protected function getDispatchStack($middlewares)
{
$stack = new DispatchStack($this);
foreach ($middlewares as $middleware) {
$stack->add($middleware);
}
return $stack;
}
} }

View File

@ -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() public function testThrowsExceptionWhenUnableToDispatch()
{ {