merged branch drak/edaware2 (PR #7852)
This PR was squashed before being merged into the master branch (closes #7852).
Discussion
----------
[2.3][EventDispatcher] Make events lighter
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | yes
| Tests pass? | yes
| Fixed tickets | na
| License | MIT
| Doc PR | symfony/symfony-docs#2476
I've taken the previous discussions and taking into consideration @fabpot does not want to break BC. This PR now provides the `EventDispatcher` as an argument to listeners. I've made a second separate commit which also passes the event name.
This PR is alternative to #7582
Commits
-------
e2bff32
[2.3][EventDispatcher] Make events lighter
This commit is contained in:
commit
6920a37a00
@ -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)
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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()
|
||||||
|
Reference in New Issue
Block a user