diff --git a/lib/Proem/Bootstrap/Dispatch.php b/lib/Proem/Bootstrap/Dispatch.php index 2bdd138..dbcd8c7 100644 --- a/lib/Proem/Bootstrap/Dispatch.php +++ b/lib/Proem/Bootstrap/Dispatch.php @@ -56,7 +56,7 @@ public function in(AssetManagerInterface $assetManager) // Trigger an event allowing client code to override defaults. $assetManager->resolve('eventManager')->trigger( - new Event('proem.in.dispatch'), + new Event('proem.in.setup.dispatch'), function ($responseEvent) use ($assetManager) { // Check for a customized Dispatch\Dispatcher. if ($responseEvent->has('dispatcherAsset')) { @@ -65,8 +65,20 @@ function ($responseEvent) use ($assetManager) { } ); - // Dispatch this request. - $assetManager->resolve('dispatcher')->handle($assetManager->resolve('request')); + // Trigger an event allowing client code to override the way the dispatcher is called. + $assetManager->resolve('eventManager')->trigger( + new Event('proem.in.dispatch'), + function ($responseEvent) use ($assetManager) { + // Check for a customised dispatch handler. + if ($responseEvent->has('dispatchHandler') && $responseEvent->get('dispatcherHandler') instanceof \Closure) { + $action = $responseEvent->get('dispatcherHandler'); + $action($assetManager->resolve('request')); + } else { + // Dispatch using the default handler. + $assetManager->resolve('dispatcher')->process($assetManager->resolve('request')); + } + } + ); } /** diff --git a/lib/Proem/Bootstrap/Request.php b/lib/Proem/Bootstrap/Request.php index 90e484c..435f624 100644 --- a/lib/Proem/Bootstrap/Request.php +++ b/lib/Proem/Bootstrap/Request.php @@ -58,7 +58,7 @@ public function in(AssetManagerInterface $assetManager) // Trigger an event allowing client code to override defaults. $assetManager->resolve('eventManager')->trigger( - new Event('proem.in.request'), + new Event('proem.in.setup.request'), function ($responseEvent) use ($assetManager) { if ($responseEvent->has('requestAsset')) { $assetManager->override('request', $responseEvent->get('requestAsset')); diff --git a/lib/Proem/Bootstrap/Route.php b/lib/Proem/Bootstrap/Route.php index eba21aa..4c5e76e 100644 --- a/lib/Proem/Bootstrap/Route.php +++ b/lib/Proem/Bootstrap/Route.php @@ -63,7 +63,7 @@ public function in(AssetManagerInterface $assetManager) // Trigger an event allowing client code to override defaults. $assetManager->resolve('eventManager')->trigger( - new Event('proem.in.route'), + new Event('proem.in.setup.route'), function ($responseEvent) use ($assetManager) { if ($responseEvent->has('routeManagerAsset')) { $assetManager->override('routeManager', $responseEvent->get('routeManagerAsset')); diff --git a/lib/Proem/Bootstrap/Tests/DispatchTest.php b/lib/Proem/Bootstrap/Tests/DispatchTest.php index 0f0f037..9c39cfe 100644 --- a/lib/Proem/Bootstrap/Tests/DispatchTest.php +++ b/lib/Proem/Bootstrap/Tests/DispatchTest.php @@ -36,13 +36,13 @@ public function testCanInstantiate() $this->assertInstanceOf('Proem\Bootstrap\Dispatch', new Dispatch); } - public function testCanTriggerEvent() + public function testTriggersEvents() { $eventManager = m::mock('Proem\Signal\EventManagerInterface'); $eventManager ->shouldReceive('trigger') ->with('Proem\Signal\EventInterface', 'closure') - ->once(); + ->twice(); $assetManager = m::mock('Proem\Service\AssetManagerInterface'); @@ -59,30 +59,9 @@ public function testCanTriggerEvent() $assetManager ->shouldReceive('resolve') ->with('eventManager') - ->once() + ->twice() ->andReturn($eventManager); - $request = m::mock('Proem\Http\Request'); - - $dispatcher = m::mock('Proem\Dispatch\Dispatcher'); - $dispatcher - ->shouldReceive('handle') - ->with($request) - ->once() - ->andReturn(true); - - $assetManager - ->shouldReceive('resolve') - ->with('dispatcher') - ->once() - ->andReturn($dispatcher); - - $assetManager - ->shouldReceive('resolve') - ->with('request') - ->once() - ->andReturn($request); - $dispatch = new Dispatch; $dispatch->in($assetManager); }