Skip to content

Commit

Permalink
Merge pull request #73 from pennyphp/feature/dispatch-callable
Browse files Browse the repository at this point in the history
Dispatcher must be a callable
  • Loading branch information
Gianluca Arbezzano committed Oct 2, 2015
2 parents 5d06703 + 48667b5 commit fcecbdf
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
13 changes: 8 additions & 5 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public static function buildContainer($config = [])
$builder = new DI\ContainerBuilder();
$builder->useAnnotations(true);
$builder->addDefinitions([
"event_manager" => DI\object('Zend\EventManager\EventManager'),
"dispatcher" => DI\object('GianArb\Penny\Dispatcher')
'event_manager' => DI\object('Zend\EventManager\EventManager'),
'dispatcher' => DI\object('GianArb\Penny\Dispatcher')
->constructor(DI\get('router')),
]);
$builder->addDefinitions($config);
Expand All @@ -94,6 +94,9 @@ public function getContainer()
private function getDispatcher()
{
$container = $this->container;
if (!is_callable($container->get('dispatcher'))) {
throw new \RuntimeException('Dispatcher must be a callable');
}

return $container->get('dispatcher');
}
Expand All @@ -113,14 +116,14 @@ private function getEventManager()
/**
* Application execution.
*
* @param mixed|null $request Representation of an outgoing, client-side request.
* @param mixed|null $request Representation of an outgoing, client-side request.
* @param mixed|null $response Representation of an outgoing, server-side response.
*
* @return mixed
*/
public function run($request = null, $response = null)
{
$request = $request ?: ServerRequestFactory::fromGlobals();
$request = $request ?: ServerRequestFactory::fromGlobals();
$response = $response ?: new Response();

$event = new HttpFlowEvent('bootstrap', $request, $response);
Expand All @@ -130,7 +133,7 @@ public function run($request = null, $response = null)
$httpFlow = $this->getEventManager();

try {
$routerInfo = $dispatcher->dispatch($request);
$routerInfo = call_user_func($dispatcher, $request);
} catch (Exception $exception) {
$event->setName('ERROR_DISPATCH');
$event->setException($exception);
Expand Down
5 changes: 3 additions & 2 deletions src/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ public function __construct(FastRouterDispatcherInterface $router)
/**
* Dispatching.
*
* @param RequestInterface $request Representation of an outgoing, client-side request.
* @param RequestInterface $request Representation of an outgoing,
* client-side request.
*
* @throws RouteNotFound If the route is not found.
* @throws MethodNotAllowed If the method is not allowed.
* @throws Exception If no one case is matched.
*
* @return array
*/
public function dispatch(RequestInterface $request)
public function __invoke(RequestInterface $request)
{
$router = $this->router;
$uri = $request->getUri();
Expand Down
20 changes: 20 additions & 0 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,24 @@ public function testMethodNotAllowed()
$response = $this->app->run($request, $response);
$this->assertEquals(405, $response->getStatusCode());
}

/**
* @expectedException \RuntimeException
*/
public function testDispatcherShouldBeCallable()
{
$request = (new Request())
->withUri(new Uri('/'))
->withMethod('POST');
$response = new Response();

$config = Loader::load();
$config['router'] = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $r) {
$r->addRoute('GET', '/', ['TestApp\Controller\Index', 'index']);
});
$config['dispatcher'] = new \StdClass();

$app = new App(App::buildContainer($config));
$app->run($request, $response);
}
}
6 changes: 3 additions & 3 deletions tests/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function testDispatchRouteNotFoundRequest()
->withMethod('GET');

$dispatcher = new Dispatcher($this->router);
$dispatcher->dispatch($request);
$dispatcher($request);
}

public function testDispatchMethodNotAllowedRequest()
Expand All @@ -52,7 +52,7 @@ public function testDispatchMethodNotAllowedRequest()
->withMethod('POST');

$dispatcher = new Dispatcher($this->router);
$dispatcher->dispatch($request);
$dispatcher($request);
}

public function testDispatchGot500Exception()
Expand All @@ -69,6 +69,6 @@ public function testDispatchGot500Exception()
])->shouldBeCalled();

$dispatcher = new Dispatcher($router->reveal());
$dispatcher->dispatch($request);
$dispatcher($request);
}
}
5 changes: 3 additions & 2 deletions tests/Utils/FastSymfonyDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ public function __construct($router)
$this->router = $router;
}

public function dispatch(Request $request)
public function __invoke(Request $request)
{
$routeInfo = $this->router->dispatch($request->getMethod(), $request->getPathInfo());
$routeInfo = $this->router
->dispatch($request->getMethod(), $request->getPathInfo());
switch ($routeInfo[0]) {
case \FastRoute\Dispatcher::NOT_FOUND:
throw new \GianArb\Penny\Exception\RouteNotFound();
Expand Down

0 comments on commit fcecbdf

Please sign in to comment.