-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[EventDispatcher] Allow EventDispatcher to dispatch anonymous events #21962
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\EventDispatcher; | ||
|
||
/** | ||
* Dispatch events without the need to specify an event name. | ||
* | ||
* @author Daniel Santamaría <[email protected]> | ||
*/ | ||
interface AnonymousEventDispatcherInterface | ||
{ | ||
/** | ||
* Dispatches an event to all registered listeners. | ||
* | ||
* @param object $event The event to pass to the event handlers/listeners | ||
*/ | ||
public function dispatchAnonymousEvent($event); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,11 +24,13 @@ | |
* @author Fabien Potencier <[email protected]> | ||
* @author Jordi Boggiano <[email protected]> | ||
* @author Jordan Alliot <[email protected]> | ||
* @author Daniel Santamaría <[email protected]> | ||
*/ | ||
class EventDispatcher implements EventDispatcherInterface | ||
class EventDispatcher implements EventDispatcherInterface, AnonymousEventDispatcherInterface, EventPropagationTrackerInterface | ||
{ | ||
private $listeners = array(); | ||
private $sorted = array(); | ||
private $stoppedEvents = array(); | ||
|
||
/** | ||
* {@inheritdoc} | ||
|
@@ -46,6 +48,20 @@ public function dispatch($eventName, Event $event = null) | |
return $event; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function dispatchAnonymousEvent($event) | ||
{ | ||
if (!is_object($event)) { | ||
throw new \InvalidArgumentException(sprintf('$event must be an object ("%s" given)', gettype($event))); | ||
} | ||
|
||
if ($listeners = $this->getListeners(get_class($event))) { | ||
$this->doAnonymousEventDispatch($listeners, $event); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
@@ -175,6 +191,25 @@ protected function doDispatch($listeners, $eventName, Event $event) | |
} | ||
} | ||
|
||
/** | ||
* Triggers the listeners of an anonymous event. | ||
* | ||
* This method can be overridden to add functionality that is executed | ||
* for each listener. | ||
* | ||
* @param callable[] $listeners The event listeners | ||
* @param object $event The event object to pass to the event handlers/listeners | ||
*/ | ||
protected function doAnonymousEventDispatch($listeners, $event) | ||
{ | ||
foreach ($listeners as $listener) { | ||
if ($this->isPropagationStopped($event)) { | ||
break; | ||
} | ||
call_user_func($listener, $event, get_class($event), $this); | ||
} | ||
} | ||
|
||
/** | ||
* Sorts the internal list of listeners for the given event by priority. | ||
* | ||
|
@@ -185,4 +220,22 @@ private function sortListeners($eventName) | |
krsort($this->listeners[$eventName]); | ||
$this->sorted[$eventName] = call_user_func_array('array_merge', $this->listeners[$eventName]); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isPropagationStopped($event) | ||
{ | ||
return in_array($event, $this->stoppedEvents); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comparing the whole object graph for equality is a nightmare for performance |
||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function stopPropagation($event) | ||
{ | ||
if (!$this->isPropagationStopped($event)) { | ||
$this->stoppedEvents[] = $event; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. making the EventDispatcher stateful is a nogo (this is leaking memory by keeping a reference to the object btw) |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\EventDispatcher; | ||
|
||
/** | ||
* Keeps track of events whose propagation has been stopped. | ||
* | ||
* @author Daniel Santamaría <[email protected]> | ||
*/ | ||
interface EventPropagationTrackerInterface | ||
{ | ||
/** | ||
* Checks if the event propagation has been stopped. | ||
* | ||
* @param object $event | ||
* | ||
* @return bool | ||
*/ | ||
public function isPropagationStopped($event); | ||
|
||
/** | ||
* Stops the event the propagation. | ||
* | ||
* @param object $event | ||
*/ | ||
public function stopPropagation($event); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why removing the event name ? This limits the use cases, as it forces creating a new class for each place dispatching an event as the event class is used as event identifier.