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
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,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 | ||
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. Maybe abstract? 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. No, not here. It's the same principal as |
||
{ | ||
/** | ||
* @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; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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; | ||
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. Define the dispatcher in the outer scope, defaulting to null? 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. six and two threes. It simply follows the form of the |
||
}); | ||
$this->dispatcher->dispatch('test'); | ||
$this->assertNull($dispatcher); | ||
} | ||
|
||
/** | ||
* @see https://bugs.php.net/bug.php?id=62976 | ||
* | ||
|
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.
I would do the check based on an interface, so that you can extend another event class and add the method
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.
I agree with @stof