8000 merged branch jalliot/subscriber-improv (PR #2148) · symfony/symfony@029223d · GitHub
[go: up one dir, main page]

Skip to content

Commit 029223d

Browse files
committed
merged branch jalliot/subscriber-improv (PR #2148)
Commits ------- 5146a1f [EventDispatcher] Added possibility for subscribers to subscribe several times for same event Discussion ---------- [EventDispatcher] Added possibility for subscribers to subscribe several times for same event [EventDispatcher] Added possibility for subscribers to subscribe several times for same event closes #2146 And it is of course fully BC :) --------------------------------------------------------------------------- by jalliot at 2011/09/09 17:34:07 -0700 If merged, #2021 will have to reflect the change too
2 parents ac31286 + 5146a1f commit 029223d

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

src/Symfony/Component/EventDispatcher/EventDispatcher.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* @author Bernhard Schussek <bschussek@gmail.com>
2424
* @author Fabien Potencier <fabien@symfony.com>
2525
* @author Jordi Boggiano <j.boggiano@seld.be>
26+
* @author Jordan Alliot <jordan.alliot@gmail.com>
2627
*
2728
* @api
2829
*/
@@ -116,8 +117,12 @@ public function addSubscriber(EventSubscriberInterface $subscriber)
116117
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
117118
if (is_string($params)) {
118119
$this->addListener($eventName, array($subscriber, $params));
119-
} else {
120+
} elseif (is_string($params[0])) {
120121
$this->addListener($eventName, array($subscriber, $params[0]), $params[1]);
122+
} else {
123+
foreach ($params as $listener) {
124+
$this->addListener($eventName, array($subscriber, $listener[0]), isset($listener[1]) ? $listener[1] : 0);
125+
}
121126
}
122127
}
123128
}
@@ -128,7 +133,13 @@ public function addSubscriber(EventSubscriberInterface $subscriber)
128133
public function removeSubscriber(EventSubscriberInterface $subscriber)
129134
{
130135
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
131-
$this->removeListener($eventName, array($subscriber, is_string($params) ? $params : $params[0]));
136+
if (is_array($params) && is_array($params[0])) {
137+
foreach ($params as $listener) {
138+
$this->removeListener($eventName, array($subscriber, $listener[0]));
139+
}
140+
} else {
141+
$this->removeListener($eventName, array($subscriber, is_string($params) ? $params : $params[0]));
142+
}
132143
}
133144
}
134145

src/Symfony/Component/EventDispatcher/EventSubscriberInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,14 @@ interface EventSubscriberInterface
3535
*
3636
* * The method name to call (priority defaults to 0)
3737
* * An array composed of the method name to call and the priority
38+
* * An array of arrays composed of the method names to call and respective
39+
* priorities, or 0 if unset
3840
*
3941
* For instance:
4042
*
4143
* * array('eventName' => 'methodName')
4244
* * array('eventName' => array('methodName', $priority))
45+
* * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
4346
*
4447
* @return array The event names to listen to
4548
*

tests/Symfony/Tests/Component/EventDispatcher/EventDispatcherTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,17 @@ public function testAddSubscriberWithPriorities()
188188
$this->assertInstanceOf('Symfony\Tests\Component\EventDispatcher\TestEventSubscriberWithPriorities', $listeners[0][0]);
189189
}
190190

191+
public function testAddSubscriberWithMultipleListeners()
192+
{
193+
$eventSubscriber = new TestEventSubscriberWithMultipleListeners();
194+
$this->dispatcher->addSubscriber($eventSubscriber);
195+
196+
$listeners = $this->dispatcher->getListeners('pre.foo');
197+
$this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
198+
$this->assertEquals(2, count($listeners));
199+
$this->assertEquals('preFoo2', $listeners[0][1]);
200+
}
201+
191202
public function testRemoveSubscriber()
192203
{
193204
$eventSubscriber = new TestEventSubscriber();
@@ -207,6 +218,16 @@ public function testRemoveSubscriberWithPriorities()
207218
$this->dispatcher->removeSubscriber($eventSubscriber);
208219
$this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
209220
}
221+
222+
public function testRemoveSubscriberWithMultipleListeners()
223+
{
224+
$eventSubscriber = new TestEventSubscriberWithMultipleListeners();
225+
$this->dispatcher->addSubscriber($eventSubscriber);
226+
$this->assertTrue($this->dispatcher->hasListeners(self::preFoo));
227+
$this->assertEquals(2, count($this->dispatcher->getListeners(self::preFoo)));
228+
$this->dispatcher->removeSubscriber($eventSubscriber);
229+
$this->assertFalse($this->dispatcher->hasListeners(self::preFoo));
230+
}
210231
}
211232

212233
class TestEventListener
@@ -244,3 +265,14 @@ public static function getSubscribedEvents()
244265
return array('pre.foo' => array('preFoo', 10));
245266
}
246267
}
268+
269+
class TestEventSubscriberWithMultipleListeners implements EventSubscriberInterface
270+
{
271+
public static function getSubscribedEvents()
272+
{
273+
return array('pre.foo' => array(
274+
array('preFoo1'),
275+
array('preFoo2', 10)
276+
));
277+
}
278+
}

0 commit comments

Comments
 (0)
0