[2.3][EventDispatcher] Make events lighter

This commit is contained in:
Drak 2013-04-25 16:35:06 +01:00 committed by Fabien Potencier
parent 1099858678
commit e2bff32de7
3 changed files with 35 additions and 2 deletions

View File

@ -76,6 +76,8 @@ class Event
* *
* @param EventDispatcherInterface $dispatcher * @param EventDispatcherInterface $dispatcher
* *
* @deprecated since 2.3 remove in 3.0
*
* @api * @api
*/ */
public function setDispatcher(EventDispatcherInterface $dispatcher) public function setDispatcher(EventDispatcherInterface $dispatcher)
@ -88,6 +90,8 @@ class Event
* *
* @return EventDispatcherInterface * @return EventDispatcherInterface
* *
* @deprecated since 2.3 remove in 3.0
*
* @api * @api
*/ */
public function getDispatcher() public function getDispatcher()
@ -100,6 +104,8 @@ class Event
* *
* @return string * @return string
* *
* @deprecated since 2.3 remove in 3.0
*
* @api * @api
*/ */
public function getName() public function getName()
@ -112,6 +118,8 @@ class Event
* *
* @param string $name The event name. * @param string $name The event name.
* *
* @deprecated since 2.3 remove in 3.0
*
* @api * @api
*/ */
public function setName($name) public function setName($name)

View File

@ -161,7 +161,7 @@ class EventDispatcher implements EventDispatcherInterface
protected function doDispatch($listeners, $eventName, Event $event) protected function doDispatch($listeners, $eventName, Event $event)
{ {
foreach ($listeners as $listener) { foreach ($listeners as $listener) {
call_user_func($listener, $event); call_user_func($listener, $event, $eventName, $this);
if ($event->isPropagationStopped()) { if ($event->isPropagationStopped()) {
break; break;
} }

View File

@ -23,6 +23,9 @@ class EventDispatcherTest extends \PHPUnit_Framework_TestCase
const preBar = 'pre.bar'; const preBar = 'pre.bar';
const postBar = 'post.bar'; const postBar = 'post.bar';
/**
* @var EventDispatcher
*/
private $dispatcher; private $dispatcher;
private $listener; private $listener;
@ -237,7 +240,6 @@ class EventDispatcherTest extends \PHPUnit_Framework_TestCase
public function testEventReceivesTheDispatcherInstance() public function testEventReceivesTheDispatcherInstance()
{ {
$test = $this;
$this->dispatcher->addListener('test', function ($event) use (&$dispatcher) { $this->dispatcher->addListener('test', function ($event) use (&$dispatcher) {
$dispatcher = $event->getDispatcher(); $dispatcher = $event->getDispatcher();
}); });
@ -245,6 +247,17 @@ class EventDispatcherTest extends \PHPUnit_Framework_TestCase
$this->assertSame($this->dispatcher, $dispatcher); $this->assertSame($this->dispatcher, $dispatcher);
} }
public function testEventReceivesTheDispatcherInstanceAsArgument()
{
$listener = new TestWithDispatcher();
$this->dispatcher->addListener('test', array($listener, 'foo'));
$this->assertNull($listener->name);
$this->assertNull($listener->dispatcher);
$this->dispatcher->dispatch('test');
$this->assertEquals('test', $listener->name);
$this->assertSame($this->dispatcher, $listener->dispatcher);
}
/** /**
* @see https://bugs.php.net/bug.php?id=62976 * @see https://bugs.php.net/bug.php?id=62976
* *
@ -289,6 +302,18 @@ class TestEventListener
} }
} }
class TestWithDispatcher
{
public $name;
public $dispatcher;
public function foo(Event $e, $name, $dispatcher)
{
$this->name = $name;
$this->dispatcher = $dispatcher;
}
}
class TestEventSubscriber implements EventSubscriberInterface class TestEventSubscriber implements EventSubscriberInterface
{ {
public static function getSubscribedEvents() public static function getSubscribedEvents()