Skip to content
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

[2.3][EventDispatcher] Moved EventDispatcher awareness to separate baseclass #7582

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Symfony/Component/EventDispatcher/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

2.3.0
-----

* [BC BREAK] added EventDispatcherAwareEvent: moved methods from Event object

2.1.0
-----

Expand All @@ -14,3 +19,4 @@ CHANGELOG
* added the possibility for subscribers to subscribe several times for the
same event
* added ImmutableEventDispatcher

31 changes: 2 additions & 29 deletions src/Symfony/Component/EventDispatcher/Event.php
Expand Up @@ -24,6 +24,8 @@
* @author Jonathan Wage <jonwage@gmail.com>
* @author Roman Borschel <roman@code-factory.org>
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Drak <drak@zikula.org>
*
* @api
*/
Expand All @@ -34,11 +36,6 @@ class Event
*/
private $propagationStopped = false;

/**
* @var EventDispatcher Dispatcher that dispatched this event
*/
private $dispatcher;

/**
* @var string This event's name
*/
Expand Down Expand Up @@ -71,30 +68,6 @@ public function stopPropagation()
$this->propagationStopped = true;
}

/**
* Stores the EventDispatcher that dispatches this Event
*
* @param EventDispatcherInterface $dispatcher
*
* @api
*/
public function setDispatcher(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}

/**
* Returns the EventDispatcher that dispatches this Event
*
* @return EventDispatcherInterface
*
* @api
*/
public function getDispatcher()
{
return $this->dispatcher;
}

/**
* Gets the event's name.
*
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/EventDispatcher/EventDispatcher.php
Expand Up @@ -43,7 +43,9 @@ public function dispatch($eventName, Event $event = null)
$event = new Event();
}

$event->setDispatcher($this);
if ($event instanceof EventDispatcherAwareEvent) {
Copy link
Member

Choose a reason for hiding this comment

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

I would do the check based on an interface, so that you can extend another event class and add the method

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with @stof

$event->setDispatcher($this);
}
$event->setName($eventName);

if (!isset($this->listeners[$eventName])) {
Expand Down
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\EventDispatcher;

/**
* Event is the base class for classes containing event data.
*
* This class contains no event data. It is used by events that do not pass
* state information to an event handler when an event is raised.
*
* You can call the method stopPropagation() to abort the execution of
* further listeners in your event listener.
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*
* @api
*/
class EventDispatcherAwareEvent extends Event
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe abstract?

Copy link
Author

Choose a reason for hiding this comment

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

No, not here. It's the same principal as Event which can be used as is (and is also not Abstract nor implements an Interface).

{
/**
* @var EventDispatcher Dispatcher that dispatched this event
*/
private $dispatcher;

/**
* Stores the EventDispatcher that dispatches this Event
*
* @param EventDispatcherInterface $dispatcher
*
* @api
*/
public function setDispatcher(EventDispatcherInterface $dispatcher)
{
$this->dispatcher = $dispatcher;
}

/**
* Returns the EventDispatcher that dispatches this Event
*
* @return EventDispatcherInterface
*
* @api
*/
public function getDispatcher()
{
return $this->dispatcher;
}
}
Expand Up @@ -164,7 +164,6 @@ public function testHasListenersOnLazyLoad()
$dispatcher = new ContainerAwareEventDispatcher($container);
$dispatcher->addListenerService('onEvent', array('service.listener', 'onEvent'));

$event->setDispatcher($dispatcher);
$event->setName('onEvent');

$service
Expand Down
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\EventDispatcher\Tests;

use Symfony\Component\EventDispatcher\EventDispatcherAwareEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;

/**
* Test class for EventDispatcherAwareEvent.
*/
class EventDispatcherAwareEventTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Symfony\Component\EventDispatcher\EventDispatcherAwareEvent
*/
protected $event;

/**
* @var \Symfony\Component\EventDispatcher\EventDispatcher
*/
protected $dispatcher;

/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->event = new EventDispatcherAwareEvent;
$this->dispatcher = new EventDispatcher();
}

/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
$this->event = null;
$this->dispatcher = null;
}

public function testSetDispatcher()
{
$this->event->setDispatcher($this->dispatcher);
$this->assertSame($this->dispatcher, $this->event->getDispatcher());
}

public function testGetDispatcher()
{
$this->assertNull($this->event->getDispatcher());
}
}
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\EventDispatcher\Tests;

use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventDispatcherAwareEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand Down Expand Up @@ -237,14 +238,23 @@ public function testRemoveSubscriberWithMultipleListeners()

public function testEventReceivesTheDispatcherInstance()
{
$test = $this;
$event = new EventDispatcherAwareEvent();
$this->dispatcher->addListener('test', function ($event) use (&$dispatcher) {
$dispatcher = $event->getDispatcher();
});
$this->dispatcher->dispatch('test');
$this->dispatcher->dispatch('test', $event);
$this->assertSame($this->dispatcher, $dispatcher);
}

public function testEventDoesNotReceiveTheDispatcherInstance()
{
$this->dispatcher->addListener('test', function ($event) use (&$dispatcher) {
$dispatcher = method_exists($event, 'getDispatcher') ? $event->getDispatcher() : null;
Copy link
Contributor

Choose a reason for hiding this comment

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

Define the dispatcher in the outer scope, defaulting to null?

Copy link
Author

Choose a reason for hiding this comment

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

six and two threes. It simply follows the form of the testEventReceivesTheDispatcherInstance() test-case.

});
$this->dispatcher->dispatch('test');
$this->assertNull($dispatcher);
}

/**
* @see https://bugs.php.net/bug.php?id=62976
*
Expand Down
13 changes: 1 addition & 12 deletions src/Symfony/Component/EventDispatcher/Tests/EventTest.php
Expand Up @@ -46,7 +46,7 @@ protected function setUp()
protected function tearDown()
{
$this->event = null;
$this->eventDispatcher = null;
$this->dispatcher = null;
}

public function testIsPropagationStopped()
Expand All @@ -60,17 +60,6 @@ public function testStopPropagationAndIsPropagationStopped()
$this->assertTrue($this->event->isPropagationStopped());
}

public function testSetDispatcher()
{
$this->event->setDispatcher($this->dispatcher);
$this->assertSame($this->dispatcher, $this->event->getDispatcher());
}

public function testGetDispatcher()
{
$this->assertNull($this->event->getDispatcher());
}

public function testGetName()
{
$this->assertNull($this->event->getName());
Expand Down