[DDC-1415] Add EntityEventDelegatee, allowing to restrict emitting events to certain entity classes only.
This commit is contained in:
parent
509e5fb12f
commit
a36a1624fb
110
lib/Doctrine/ORM/Event/EntityEventDelegator.php
Normal file
110
lib/Doctrine/ORM/Event/EntityEventDelegator.php
Normal file
@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals
|
||||
* and is licensed under the LGPL. For more information, see
|
||||
* <http://www.doctrine-project.org>.
|
||||
*/
|
||||
|
||||
namespace Doctrine\ORM\Event;
|
||||
|
||||
use \Doctrine\Common\EventSubscriber;
|
||||
use \LogicException;
|
||||
|
||||
/**
|
||||
* Delegate events only for certain entities they are registered for.
|
||||
*
|
||||
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
||||
* @since 2.2
|
||||
*/
|
||||
class EntityEventDelegator implements EventSubscriber
|
||||
{
|
||||
/**
|
||||
* Keeps track of all the event listeners.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $listeners = array();
|
||||
|
||||
/**
|
||||
* If frozen no new event listeners can be added.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private $frozen = false;
|
||||
|
||||
/**
|
||||
* Adds an event listener that listens on the specified events.
|
||||
*
|
||||
* @param string|array $events The event(s) to listen on.
|
||||
* @param string|array $entities The entities to trigger this listener for
|
||||
* @param object $listener The listener object.
|
||||
*/
|
||||
public function addEventListener($events, $entities, $listener)
|
||||
{
|
||||
if ($this->frozen) {
|
||||
throw new LogicException("Cannot add event listeners after EntityEventDelegator::getSubscribedEvents() " .
|
||||
"is called once. This happens when you register the delegator with the event manager.");
|
||||
}
|
||||
|
||||
// Picks the hash code related to that listener
|
||||
$hash = spl_object_hash($listener);
|
||||
|
||||
foreach ((array) $events as $event) {
|
||||
// Overrides listener if a previous one was associated already
|
||||
// Prevents duplicate listeners on same event (same instance only)
|
||||
$this->listeners[$event][$hash] = array('listener' => $listener, 'entities' => array_flip((array)$entities));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an EventSubscriber. The subscriber is asked for all the events he is
|
||||
* interested in and added as a listener for these events.
|
||||
*
|
||||
* @param Doctrine\Common\EventSubscriber $subscriber The subscriber.
|
||||
*/
|
||||
public function addEventSubscriber(EventSubscriber $subscriber, $entities)
|
||||
{
|
||||
$this->addEventListener($subscriber->getSubscribedEvents(), $entities, $subscriber);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of events this subscriber wants to listen to.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getSubscribedEvents()
|
||||
{
|
||||
$this->frozen = true;
|
||||
return array_keys($this->listeners);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegate the event to an appropriate listener
|
||||
*
|
||||
* @param $eventName
|
||||
* @param $event
|
||||
* @return void
|
||||
*/
|
||||
public function __call($eventName, $args)
|
||||
{
|
||||
$event = $args[0];
|
||||
foreach ($this->listeners[$eventName] AS $listenerData) {
|
||||
$class = get_class($event->getEntity());
|
||||
if (isset($listenerData['entities'][$class])) {
|
||||
$listenerData['listener']->$eventName($event);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
91
tests/Doctrine/Tests/ORM/Event/EntityEventDelegateeTest.php
Normal file
91
tests/Doctrine/Tests/ORM/Event/EntityEventDelegateeTest.php
Normal file
@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
namespace Doctrine\Tests\ORM\Event;
|
||||
|
||||
use Doctrine\ORM\Mapping\ClassMetadata;
|
||||
|
||||
/**
|
||||
* @group DDC-1415
|
||||
*/
|
||||
class EntityEventDelegatorTest extends \Doctrine\Tests\OrmTestCase
|
||||
{
|
||||
/**
|
||||
* @var \Doctrine\ORM\Event\EntityEventDelegator
|
||||
*/
|
||||
private $delegator;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->delegator = new \Doctrine\ORM\Event\EntityEventDelegator();
|
||||
}
|
||||
|
||||
public function testGetSubscribedEventsWhenEmpty()
|
||||
{
|
||||
$this->assertEquals(array(), $this->delegator->getSubscribedEvents());
|
||||
}
|
||||
|
||||
public function testAddListener()
|
||||
{
|
||||
$this->delegator->addEventListener('postLoad', 'stdClass', new DelegateeEventListener());
|
||||
$this->assertEquals(array('postLoad'), $this->delegator->getSubscribedEvents());
|
||||
}
|
||||
|
||||
public function testAddSubscriber()
|
||||
{
|
||||
$this->delegator->addEventSubscriber(new DelegateeEventListener(), 'stdClass');
|
||||
$this->assertEquals(array('postLoad'), $this->delegator->getSubscribedEvents());
|
||||
}
|
||||
|
||||
public function testAddListenerAfterFrozenThrowsException()
|
||||
{
|
||||
$this->delegator->getSubscribedEvents(); // freezes
|
||||
|
||||
$this->setExpectedException("LogicException", "Cannot add event listeners aft");
|
||||
$this->delegator->addEventListener('postLoad', 'stdClass', new DelegateeEventListener());
|
||||
}
|
||||
|
||||
public function testDelegateEvent()
|
||||
{
|
||||
$delegatee = new DelegateeEventListener();
|
||||
$this->delegator->addEventListener('postLoad', 'stdClass', $delegatee);
|
||||
|
||||
$event = new \Doctrine\ORM\Event\LifecycleEventArgs(new \stdClass(), $this->_getTestEntityManager());
|
||||
$this->delegator->postLoad($event);
|
||||
$this->delegator->postLoad($event);
|
||||
|
||||
$this->assertEquals(2, $delegatee->postLoad);
|
||||
}
|
||||
|
||||
public function testDelegatePickEntity()
|
||||
{
|
||||
$delegatee = new DelegateeEventListener();
|
||||
$this->delegator->addEventListener('postLoad', 'stdClass', $delegatee);
|
||||
|
||||
$event1 = new \Doctrine\ORM\Event\LifecycleEventArgs(new \stdClass(), $this->_getTestEntityManager());
|
||||
$event2 = new \Doctrine\ORM\Event\LifecycleEventArgs(new \Doctrine\Tests\Models\CMS\CmsUser(), $this->_getTestEntityManager());
|
||||
$this->delegator->postLoad($event1);
|
||||
$this->delegator->postLoad($event2);
|
||||
|
||||
$this->assertEquals(1, $delegatee->postLoad);
|
||||
}
|
||||
}
|
||||
|
||||
class DelegateeEventListener implements \Doctrine\Common\EventSubscriber
|
||||
{
|
||||
public $postLoad = 0;
|
||||
|
||||
public function postLoad($args)
|
||||
{
|
||||
$this->postLoad++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of events this subscriber wants to listen to.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getSubscribedEvents()
|
||||
{
|
||||
return array('postLoad');
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user