From 03f4457abe15a7e899860df156d8b820396cb404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Ostroluck=C3=BD?= Date: Wed, 1 Mar 2017 20:04:03 +0100 Subject: [PATCH] [EventDispatcher] Added EventTrait This allows to easily make any custom class fit for EventDispatcher --- .../Debug/TraceableEventDispatcher.php | 15 ++++---- .../Component/EventDispatcher/Event.php | 31 +--------------- .../EventDispatcher/EventDispatcher.php | 10 ++--- .../EventDispatcherInterface.php | 14 +++---- .../EventDispatcher/EventInterface.php | 34 +++++++++++++++++ .../Component/EventDispatcher/EventTrait.php | 37 +++++++++++++++++++ .../ImmutableEventDispatcher.php | 2 +- .../Debug/TraceableEventDispatcher.php | 6 +-- .../Component/Workflow/Tests/WorkflowTest.php | 2 +- 9 files changed, 98 insertions(+), 53 deletions(-) create mode 100644 src/Symfony/Component/EventDispatcher/EventInterface.php create mode 100644 src/Symfony/Component/EventDispatcher/EventTrait.php diff --git a/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php b/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php index e74ac05f83ba4..3bea343c00251 100644 --- a/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php +++ b/src/Symfony/Component/EventDispatcher/Debug/TraceableEventDispatcher.php @@ -12,6 +12,7 @@ namespace Symfony\Component\EventDispatcher\Debug; use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\EventDispatcher\EventInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\Event; use Symfony\Component\Stopwatch\Stopwatch; @@ -118,7 +119,7 @@ public function hasListeners($eventName = null) /** * {@inheritdoc} */ - public function dispatch($eventName, Event $event = null) + public function dispatch($eventName, EventInterface $event = null) { if (null === $event) { $event = new Event(); @@ -220,20 +221,20 @@ public function __call($method, $arguments) /** * Called before dispatching the event. * - * @param string $eventName The event name - * @param Event $event The event + * @param string $eventName The event name + * @param EventInterface $event The event */ - protected function preDispatch($eventName, Event $event) + protected function preDispatch($eventName, EventInterface $event) { } /** * Called after dispatching the event. * - * @param string $eventName The event name - * @param Event $event The event + * @param string $eventName The event name + * @param EventInterface $event The event */ - protected function postDispatch($eventName, Event $event) + protected function postDispatch($eventName, EventInterface $event) { } diff --git a/src/Symfony/Component/EventDispatcher/Event.php b/src/Symfony/Component/EventDispatcher/Event.php index 9c56b2f55b8a7..c67e4d826c624 100644 --- a/src/Symfony/Component/EventDispatcher/Event.php +++ b/src/Symfony/Component/EventDispatcher/Event.php @@ -25,34 +25,7 @@ * @author Roman Borschel * @author Bernhard Schussek */ -class Event +class Event implements EventInterface { - /** - * @var bool Whether no further event listeners should be triggered - */ - private $propagationStopped = false; - - /** - * Returns whether further event listeners should be triggered. - * - * @see Event::stopPropagation() - * - * @return bool Whether propagation was already stopped for this event - */ - public function isPropagationStopped() - { - return $this->propagationStopped; - } - - /** - * Stops the propagation of the event to further event listeners. - * - * If multiple event listeners are connected to the same event, no - * further event listener will be triggered once any trigger calls - * stopPropagation(). - */ - public function stopPropagation() - { - $this->propagationStopped = true; - } + use EventTrait; } diff --git a/src/Symfony/Component/EventDispatcher/EventDispatcher.php b/src/Symfony/Component/EventDispatcher/EventDispatcher.php index 2c977f4731291..a02b08687c1fd 100644 --- a/src/Symfony/Component/EventDispatcher/EventDispatcher.php +++ b/src/Symfony/Component/EventDispatcher/EventDispatcher.php @@ -33,7 +33,7 @@ class EventDispatcher implements EventDispatcherInterface /** * {@inheritdoc} */ - public function dispatch($eventName, Event $event = null) + public function dispatch($eventName, EventInterface $event = null) { if (null === $event) { $event = new Event(); @@ -161,11 +161,11 @@ public function removeSubscriber(EventSubscriberInterface $subscriber) * This method can be overridden to add functionality that is executed * for each listener. * - * @param callable[] $listeners The event listeners - * @param string $eventName The name of the event to dispatch - * @param Event $event The event object to pass to the event handlers/listeners + * @param callable[] $listeners The event listeners + * @param string $eventName The name of the event to dispatch + * @param EventInterface $event The event object to pass to the event handlers/listeners */ - protected function doDispatch($listeners, $eventName, Event $event) + protected function doDispatch($listeners, $eventName, EventInterface $event) { foreach ($listeners as $listener) { if ($event->isPropagationStopped()) { diff --git a/src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php b/src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php index 08ebf3400e98f..b449ef7d5c201 100644 --- a/src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php +++ b/src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php @@ -23,15 +23,15 @@ interface EventDispatcherInterface /** * Dispatches an event to all registered listeners. * - * @param string $eventName The name of the event to dispatch. The name of - * the event is the name of the method that is - * invoked on listeners. - * @param Event $event The event to pass to the event handlers/listeners - * If not supplied, an empty Event instance is created. + * @param string $eventName The name of the event to dispatch. The name of + * the event is the name of the method that is + * invoked on listeners. + * @param EventInterface $event The event to pass to the event handlers/listeners + * If not supplied, an empty Event instance is created. * - * @return Event + * @return EventInterface */ - public function dispatch($eventName, Event $event = null); + public function dispatch($eventName, EventInterface $event = null); /** * Adds an event listener that listens on the specified events. diff --git a/src/Symfony/Component/EventDispatcher/EventInterface.php b/src/Symfony/Component/EventDispatcher/EventInterface.php new file mode 100644 index 0000000000000..d715f57d52473 --- /dev/null +++ b/src/Symfony/Component/EventDispatcher/EventInterface.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\EventDispatcher; + + +interface EventInterface +{ + /** + * Returns whether further event listeners should be triggered. + * + * @see EventInterface::stopPropagation() + * + * @return bool Whether propagation was already stopped for this event + */ + public function isPropagationStopped(); + + /** + * Stops the propagation of the event to further event listeners. + * + * If multiple event listeners are connected to the same event, no + * further event listener will be triggered once any trigger calls + * stopPropagation(). + */ + public function stopPropagation(); +} \ No newline at end of file diff --git a/src/Symfony/Component/EventDispatcher/EventTrait.php b/src/Symfony/Component/EventDispatcher/EventTrait.php new file mode 100644 index 0000000000000..7a6cd4a8b200e --- /dev/null +++ b/src/Symfony/Component/EventDispatcher/EventTrait.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\EventDispatcher; + + +trait EventTrait +{ + /** + * @var bool Whether no further event listeners should be triggered + */ + private $propagationStopped = false; + + /** + * @return bool + */ + public function isPropagationStopped() + { + return $this->propagationStopped; + } + + /** + * @return void + */ + public function stopPropagation() + { + $this->propagationStopped = true; + } +} \ No newline at end of file diff --git a/src/Symfony/Component/EventDispatcher/ImmutableEventDispatcher.php b/src/Symfony/Component/EventDispatcher/ImmutableEventDispatcher.php index 7f2be8d3145d6..d184fed4fc197 100644 --- a/src/Symfony/Component/EventDispatcher/ImmutableEventDispatcher.php +++ b/src/Symfony/Component/EventDispatcher/ImmutableEventDispatcher.php @@ -38,7 +38,7 @@ public function __construct(EventDispatcherInterface $dispatcher) /** * {@inheritdoc} */ - public function dispatch($eventName, Event $event = null) + public function dispatch($eventName, EventInterface $event = null) { return $this->dispatcher->dispatch($eventName, $event); } diff --git a/src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php b/src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php index fbc49dffc8daa..7fe6b09cede73 100644 --- a/src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php +++ b/src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php @@ -12,8 +12,8 @@ namespace Symfony\Component\HttpKernel\Debug; use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher as BaseTraceableEventDispatcher; +use Symfony\Component\EventDispatcher\EventInterface; use Symfony\Component\HttpKernel\KernelEvents; -use Symfony\Component\EventDispatcher\Event; /** * Collects some data about event listeners. @@ -27,7 +27,7 @@ class TraceableEventDispatcher extends BaseTraceableEventDispatcher /** * {@inheritdoc} */ - protected function preDispatch($eventName, Event $event) + protected function preDispatch($eventName, EventInterface $event) { switch ($eventName) { case KernelEvents::REQUEST: @@ -58,7 +58,7 @@ protected function preDispatch($eventName, Event $event) /** * {@inheritdoc} */ - protected function postDispatch($eventName, Event $event) + protected function postDispatch($eventName, EventInterface $event) { switch ($eventName) { case KernelEvents::CONTROLLER_ARGUMENTS: diff --git a/src/Symfony/Component/Workflow/Tests/WorkflowTest.php b/src/Symfony/Component/Workflow/Tests/WorkflowTest.php index a208c0d886ac0..774023557f570 100644 --- a/src/Symfony/Component/Workflow/Tests/WorkflowTest.php +++ b/src/Symfony/Component/Workflow/Tests/WorkflowTest.php @@ -305,7 +305,7 @@ class EventDispatcherMock implements \Symfony\Component\EventDispatcher\EventDis { public $dispatchedEvents = array(); - public function dispatch($eventName, \Symfony\Component\EventDispatcher\Event $event = null) + public function dispatch($eventName, \Symfony\Component\EventDispatcher\EventInterface $event = null) { $this->dispatchedEvents[] = $eventName; }