Commit e21c10f2 authored by Fabien Potencier's avatar Fabien Potencier

merged branch GromNaN/on2 (PR #441)

This PR was merged into the master branch.

Commits
-------

c4975cc4 Create Application::on() method to add an event listener

Discussion
----------

Add method Application::on($eventName, $callback, $priority)

Simplify PR #435

This method is a shortcut to add a listener to the event dispatcher.

``` php
<?php
use Symfony\Component\Security\Http\SecurityEvents;

$app->on(SecurityEvents::SWITCH_USER, function ($event) {
    // ...
});
```

---------------------------------------------------------------------------

by fabpot at 2012-10-24T20:34:12Z

Can you add some useful examples in the documentation before I merge this PR? Thanks.

---------------------------------------------------------------------------

by GromNaN at 2012-10-24T21:57:48Z

Which part of the documentation would be the most relevant ?

I can write a cookbook on how to send an email when someone login to the application.
parents 63f61ce5 c4975cc4
......@@ -240,6 +240,19 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
return $this['controllers']->delete($pattern, $to);
}
/**
* Adds an event listener that listens on the specified events.
*
* @param string $eventName The event to listen on
* @param callable $listener The listener
* @param integer $priority The higher this value, the earlier an event
* listener will be triggered in the chain (defaults to 0)
*/
public function on($eventName, $callback, $priority = 0)
{
return $this['dispatcher']->addListener($eventName, $callback, $priority);
}
/**
* Registers a before filter.
*
......
......@@ -106,6 +106,20 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('foobar', $response->getContent());
}
public function testOn()
{
$app = new Application();
$app['pass'] = false;
$app->on('test', function(Event $e) use ($app) {
$app['pass'] = true;
});
$app['dispatcher']->dispatch('test');
$this->assertTrue($app['pass']);
}
public function testAbort()
{
$app = new Application();
......
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