[2.3][EventDispatcher] Make events lighter
This commit is contained in:
parent
1099858678
commit
e2bff32de7
@ -76,6 +76,8 @@ class Event
|
||||
*
|
||||
* @param EventDispatcherInterface $dispatcher
|
||||
*
|
||||
* @deprecated since 2.3 remove in 3.0
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function setDispatcher(EventDispatcherInterface $dispatcher)
|
||||
@ -88,6 +90,8 @@ class Event
|
||||
*
|
||||
* @return EventDispatcherInterface
|
||||
*
|
||||
* @deprecated since 2.3 remove in 3.0
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function getDispatcher()
|
||||
@ -100,6 +104,8 @@ class Event
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @deprecated since 2.3 remove in 3.0
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function getName()
|
||||
@ -112,6 +118,8 @@ class Event
|
||||
*
|
||||
* @param string $name The event name.
|
||||
*
|
||||
* @deprecated since 2.3 remove in 3.0
|
||||
*
|
||||
* @api
|
||||
*/
|
||||
public function setName($name)
|
||||
|
@ -161,7 +161,7 @@ class EventDispatcher implements EventDispatcherInterface
|
||||
protected function doDispatch($listeners, $eventName, Event $event)
|
||||
{
|
||||
foreach ($listeners as $listener) {
|
||||
call_user_func($listener, $event);
|
||||
call_user_func($listener, $event, $eventName, $this);
|
||||
if ($event->isPropagationStopped()) {
|
||||
break;
|
||||
}
|
||||
|
@ -23,6 +23,9 @@ class EventDispatcherTest extends \PHPUnit_Framework_TestCase
|
||||
const preBar = 'pre.bar';
|
||||
const postBar = 'post.bar';
|
||||
|
||||
/**
|
||||
* @var EventDispatcher
|
||||
*/
|
||||
private $dispatcher;
|
||||
|
||||
private $listener;
|
||||
@ -237,7 +240,6 @@ class EventDispatcherTest extends \PHPUnit_Framework_TestCase
|
||||
|
||||
public function testEventReceivesTheDispatcherInstance()
|
||||
{
|
||||
$test = $this;
|
||||
$this->dispatcher->addListener('test', function ($event) use (&$dispatcher) {
|
||||
$dispatcher = $event->getDispatcher();
|
||||
});
|
||||
@ -245,6 +247,17 @@ class EventDispatcherTest extends \PHPUnit_Framework_TestCase
|
||||
$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
|
||||
*
|
||||
@ -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
|
||||
{
|
||||
public static function getSubscribedEvents()
|
||||
|
Reference in New Issue
Block a user