Commit 33f17eec authored by Dave Marshall's avatar Dave Marshall Committed by Fabien Potencier

Event helpers add listeners directly after boot

Fixes #748
parent e0aa6a89
...@@ -267,6 +267,11 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte ...@@ -267,6 +267,11 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
*/ */
public function on($eventName, $callback, $priority = 0) public function on($eventName, $callback, $priority = 0)
{ {
if ($this->booted) {
$this['dispatcher']->addListener($eventName, $callback, $priority);
return;
}
$this['dispatcher'] = $this->share($this->extend('dispatcher', function ($dispatcher, $app) use ($callback, $priority, $eventName) { $this['dispatcher'] = $this->share($this->extend('dispatcher', function ($dispatcher, $app) use ($callback, $priority, $eventName) {
$dispatcher->addListener($eventName, $callback, $priority); $dispatcher->addListener($eventName, $callback, $priority);
......
...@@ -38,4 +38,23 @@ class LazyDispatcherTest extends \PHPUnit_Framework_TestCase ...@@ -38,4 +38,23 @@ class LazyDispatcherTest extends \PHPUnit_Framework_TestCase
$this->assertTrue($dispatcherCreated); $this->assertTrue($dispatcherCreated);
} }
/** @test */
public function eventHelpersShouldDirectlyAddListenersAfterBoot()
{
$app = new Application();
$fired = false;
$app->get("/", function () use ($app, &$fired) {
$app->finish(function () use (&$fired) {
$fired = true;
});
});
$request = Request::create('/');
$response = $app->handle($request);
$app->terminate($request, $response);
$this->assertTrue($fired, 'Event was not fired');
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment