Skip to content

Commit e26d812

Browse files
committed
[EventDispatcher] added a way to set the priority for event subscribers
1 parent fd2ec62 commit e26d812

3 files changed

+20
-7
lines changed

EventDispatcher.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,14 @@ public function removeListener($eventName, $listener)
127127
/**
128128
* @see EventDispatcherInterface::addSubscriber
129129
*/
130-
public function addSubscriber(EventSubscriberInterface $subscriber, $priority = 0)
130+
public function addSubscriber(EventSubscriberInterface $subscriber)
131131
{
132-
foreach ($subscriber->getSubscribedEvents() as $eventName => $method) {
133-
$this->addListener($eventName, array($subscriber, $method), $priority);
132+
foreach ($subscriber->getSubscribedEvents() as $eventName => $params) {
133+
if (is_string($params)) {
134+
$this->addListener($eventName, array($subscriber, $params));
135+
} else {
136+
$this->addListener($eventName, array($subscriber, $params[0]), $params[1]);
137+
}
134138
}
135139
}
136140

EventDispatcherInterface.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,10 @@ function addListener($eventName, $listener, $priority = 0);
5252
* interested in and added as a listener for these events.
5353
*
5454
* @param EventSubscriberInterface $subscriber The subscriber.
55-
* @param integer $priority The higher this value, the earlier an event
56-
* listener will be triggered in the chain.
57-
* Defaults to 0.
55+
*
56+
* @api
5857
*/
59-
function addSubscriber(EventSubscriberInterface $subscriber, $priority = 0);
58+
function addSubscriber(EventSubscriberInterface $subscriber);
6059

6160
/**
6261
* Removes an event listener from the specified events.

EventSubscriberInterface.php

+10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ interface EventSubscriberInterface
3939
/**
4040
* Returns an array of event names this subscriber wants to listen to.
4141
*
42+
* The array keys are event names and the value can be:
43+
*
44+
* * The method name to call (priority defaults to 0)
45+
* * An array composed of the method name to call and the priority
46+
*
47+
* For instance:
48+
*
49+
* * array('eventName' => 'methodName')
50+
* * array('eventName' => array('methodName', $priority))
51+
*
4252
* @return array The event names to listen to
4353
*/
4454
static function getSubscribedEvents();

0 commit comments

Comments
 (0)