Skip to content

Commit d855e87

Browse files
committed
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
2 parents d385232 + 3108d91 commit d855e87

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

Event.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public function stopPropagation()
7676
*
7777
* @param EventDispatcherInterface $dispatcher
7878
*
79+
* @deprecated since 2.3 remove in 3.0
80+
*
7981
* @api
8082
*/
8183
public function setDispatcher(EventDispatcherInterface $dispatcher)
@@ -88,6 +90,8 @@ public function setDispatcher(EventDispatcherInterface $dispatcher)
8890
*
8991
* @return EventDispatcherInterface
9092
*
93+
* @deprecated since 2.3 remove in 3.0
94+
*
9195
* @api
9296
*/
9397
public function getDispatcher()
@@ -100,6 +104,8 @@ public function getDispatcher()
100104
*
101105
* @return string
102106
*
107+
* @deprecated since 2.3 remove in 3.0
108+
*
103109
* @api
104110
*/
105111
public function getName()
@@ -112,6 +118,8 @@ public function getName()
112118
*
113119
* @param string $name The event name.
114120
*
121+
* @deprecated since 2.3 remove in 3.0
122+
*
115123
* @api
116124
*/
117125
public function setName($name)

EventDispatcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ public function removeSubscriber(EventSubscriberInterface $subscriber)
161161
protected function doDispatch($listeners, $eventName, Event $event)
162162
{
163163
foreach ($listeners as $listener) {
164-
call_user_func($listener, $event);
164+
call_user_func($listener, $event, $eventName, $this);
165165
if ($event->isPropagationStopped()) {
166166
break;
167167
}

Tests/EventDispatcherTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class EventDispatcherTest extends \PHPUnit_Framework_TestCase
2323
const preBar = 'pre.bar';
2424
const postBar = 'post.bar';
2525

26+
/**
27+
* @var EventDispatcher
28+
*/
2629
private $dispatcher;
2730

2831
private $listener;
@@ -237,14 +240,24 @@ public function testRemoveSubscriberWithMultipleListeners()
237240

238241
public function testEventReceivesTheDispatcherInstance()
239242
{
240-
$test = $this;
241243
$this->dispatcher->addListener('test', function ($event) use (&$dispatcher) {
242244
$dispatcher = $event->getDispatcher();
243245
});
244246
$this->dispatcher->dispatch('test');
245247
$this->assertSame($this->dispatcher, $dispatcher);
246248
}
247249

250+
public function testEventReceivesTheDispatcherInstanceAsArgument()
251+
{
252+
$listener = new TestWithDispatcher();
253+
$this->dispatcher->addListener('test', array($listener, 'foo'));
254+
$this->assertNull($listener->name);
255+
$this->assertNull($listener->dispatcher);
256+
$this->dispatcher->dispatch('test');
257+
$this->assertEquals('test', $listener->name);
258+
$this->assertSame($this->dispatcher, $listener->dispatcher);
259+
}
260+
248261
/**
249262
* @see https://bugs.php.net/bug.php?id=62976
250263
*
@@ -289,6 +302,18 @@ public function postFoo(Event $e)
289302
}
290303
}
291304

305+
class TestWithDispatcher
306+
{
307+
public $name;
308+
public $dispatcher;
309+
310+
public function foo(Event $e, $name, $dispatcher)
311+
{
312+
$this->name = $name;
313+
$this->dispatcher = $dispatcher;
314+
}
315+
}
316+
292317
class TestEventSubscriber implements EventSubscriberInterface
293318
{
294319
public static function getSubscribedEvents()

0 commit comments

Comments
 (0)