Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[EventDispatcher] Added EventTrait #21827

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)
{
}

Expand Down
31 changes: 2 additions & 29 deletions src/Symfony/Component/EventDispatcher/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,34 +25,7 @@
* @author Roman Borschel <[email protected]>
* @author Bernhard Schussek <[email protected]>
*/
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;
}
10 changes: 5 additions & 5 deletions src/Symfony/Component/EventDispatcher/EventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

@xabbuh xabbuh Mar 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gadelat In fact, changing the type hint here is a BC break.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How so? Users pass the event class here, which implements this interface.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every class implementing this interface must be updated too (see the fatal error you will get otherwise: https://3v4l.org/CQgFB).

Copy link
Contributor Author

@ostrolucky ostrolucky Mar 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true :( Are the downvotes then only because of this BC, or is there something else? Would you be more keen to merge this at least in 4.0?


/**
* Adds an event listener that listens on the specified events.
Expand Down
34 changes: 34 additions & 0 deletions src/Symfony/Component/EventDispatcher/EventInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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;


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();
}
37 changes: 37 additions & 0 deletions src/Symfony/Component/EventDispatcher/EventTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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;


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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Workflow/Tests/WorkflowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down