mirror of
https://github.com/stan220/WildcardEventDispatcher.git
synced 2026-09-08 15:28:37 +00:00
Allow subscribing to multiple events like Symfony EventDispatcher does
This commit is contained in:
committed by
Jeremy Mikola
parent
e6b272951d
commit
62eb20340a
@@ -92,10 +92,14 @@ class WildcardEventDispatcher implements EventDispatcherInterface
|
||||
public function addSubscriber(EventSubscriberInterface $subscriber)
|
||||
{
|
||||
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
|
||||
if (is_array($params)) {
|
||||
$this->addListener($eventName, array($subscriber, $params[0]), $params[1]);
|
||||
} else {
|
||||
if (is_string($params)) {
|
||||
$this->addListener($eventName, array($subscriber, $params));
|
||||
} elseif (is_string($params[0])) {
|
||||
$this->addListener($eventName, array($subscriber, $params[0]), isset($params[1]) ? $params[1] : 0);
|
||||
} else {
|
||||
foreach ($params as $listener) {
|
||||
$this->addListener($eventName, array($subscriber, $listener[0]), isset($listener[1]) ? $listener[1] : 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -106,7 +110,13 @@ class WildcardEventDispatcher implements EventDispatcherInterface
|
||||
public function removeSubscriber(EventSubscriberInterface $subscriber)
|
||||
{
|
||||
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
|
||||
$this->removeListener($eventName, array($subscriber, is_array($params) ? $params[0] : $params));
|
||||
if (is_array($params) && is_array($params[0])) {
|
||||
foreach ($params as $listener) {
|
||||
$this->removeListener($eventName, array($subscriber, $listener[0]));
|
||||
}
|
||||
} else {
|
||||
$this->removeListener($eventName, array($subscriber, is_string($params) ? $params : $params[0]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -115,27 +115,35 @@ class EventDispatcherTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
$subscriber = new TestEventSubscriber();
|
||||
|
||||
$i = 0;
|
||||
$defaultPriority = 0;
|
||||
$numSubscribedEvents = count($subscriber->getSubscribedEvents());
|
||||
$this->innerDispatcher->expects($this->at(0))
|
||||
->method('addListener')
|
||||
->with('core.request', array($subscriber, 'onRequest'), 0);
|
||||
$this->innerDispatcher->expects($this->at(1))
|
||||
->method('addListener')
|
||||
->with('core.exception', array($subscriber, 'onException'), 10);
|
||||
$this->innerDispatcher->expects($this->at(2))
|
||||
->method('addListener')
|
||||
->with('core.multi', array($subscriber, 'onMulti1'), 10);
|
||||
$this->innerDispatcher->expects($this->at(3))
|
||||
->method('addListener')
|
||||
->with('core.multi', array($subscriber, 'onMulti2'), 20);
|
||||
|
||||
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
|
||||
$method = is_array($params) ? $params[0] : $params;
|
||||
$priority = is_array($params) ? $params[1] : $defaultPriority;
|
||||
$this->innerDispatcher->expects($this->at(4))
|
||||
->method('removeListener')
|
||||
->with('core.request', array($subscriber, 'onRequest'));
|
||||
$this->innerDispatcher->expects($this->at(5))
|
||||
->method('removeListener')
|
||||
->with('core.exception', array($subscriber, 'onException'));
|
||||
$this->innerDispatcher->expects($this->at(6))
|
||||
->method('removeListener')
|
||||
->with('core.multi', array($subscriber, 'onMulti1'));
|
||||
$this->innerDispatcher->expects($this->at(7))
|
||||
->method('removeListener')
|
||||
->with('core.multi', array($subscriber, 'onMulti2'));
|
||||
|
||||
$this->innerDispatcher->expects($this->at($i))
|
||||
->method('addListener')
|
||||
->with($eventName, array($subscriber, $method), $priority);
|
||||
|
||||
$this->innerDispatcher->expects($this->at($numSubscribedEvents + $i))
|
||||
->method('removeListener')
|
||||
->with($eventName, array($subscriber, $method));
|
||||
|
||||
++$i;
|
||||
}
|
||||
|
||||
$this->dispatcher->addSubscriber($subscriber, $priority);
|
||||
$this->dispatcher->removeSubscriber($subscriber, $priority);
|
||||
$this->dispatcher->addSubscriber($subscriber);
|
||||
$this->dispatcher->removeSubscriber($subscriber);
|
||||
}
|
||||
|
||||
private function getMockEventDispatcher()
|
||||
@@ -151,6 +159,7 @@ class TestEventSubscriber implements EventSubscriberInterface
|
||||
return array(
|
||||
'core.request' => 'onRequest',
|
||||
'core.exception' => array('onException', 10),
|
||||
'core.multi' => array(array('onMulti1', 10), array('onMulti2', 20)),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user