2011-10-14 00:58:22 +04:00
|
|
|
<?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;
|
|
|
|
|
2011-10-28 18:49:01 +04:00
|
|
|
use Doctrine\Common\EventSubscriber;
|
|
|
|
use LogicException;
|
2011-10-14 00:58:22 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delegate events only for certain entities they are registered for.
|
|
|
|
*
|
2011-10-28 18:49:01 +04:00
|
|
|
* @link www.doctrine-project.org
|
|
|
|
* @author Benjamin Eberlei <kontakt@beberlei.de>
|
|
|
|
* @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
|
|
|
* @since 2.2
|
2011-10-14 00:58:22 +04:00
|
|
|
*/
|
|
|
|
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) {
|
2011-10-28 18:49:01 +04:00
|
|
|
throw new LogicException(
|
|
|
|
"Cannot add event listeners after EntityEventDelegator::getSubscribedEvents() " .
|
|
|
|
"is called once. This happens when you register the delegator with the event manager."
|
|
|
|
);
|
2011-10-14 00:58:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Picks the hash code related to that listener
|
2011-10-28 18:49:01 +04:00
|
|
|
$hash = spl_object_hash($listener);
|
|
|
|
$entities = array_flip((array) $entities);
|
2011-10-14 00:58:22 +04:00
|
|
|
|
|
|
|
foreach ((array) $events as $event) {
|
|
|
|
// Overrides listener if a previous one was associated already
|
|
|
|
// Prevents duplicate listeners on same event (same instance only)
|
2011-10-28 18:49:01 +04:00
|
|
|
$this->listeners[$event][$hash] = array(
|
|
|
|
'listener' => $listener,
|
|
|
|
'entities' => $entities
|
|
|
|
);
|
2011-10-14 00:58:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
2011-10-28 18:49:01 +04:00
|
|
|
* @param array $entities
|
2011-10-14 00:58:22 +04:00
|
|
|
*/
|
|
|
|
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;
|
2011-10-28 18:49:01 +04:00
|
|
|
|
2011-10-14 00:58:22 +04:00
|
|
|
return array_keys($this->listeners);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delegate the event to an appropriate listener
|
|
|
|
*
|
2011-10-28 18:49:01 +04:00
|
|
|
* @param string $eventName
|
|
|
|
* @param array $args
|
2011-10-14 00:58:22 +04:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __call($eventName, $args)
|
|
|
|
{
|
|
|
|
$event = $args[0];
|
2011-10-28 18:49:01 +04:00
|
|
|
|
2011-10-14 00:58:22 +04:00
|
|
|
foreach ($this->listeners[$eventName] AS $listenerData) {
|
|
|
|
$class = get_class($event->getEntity());
|
2011-10-28 18:49:01 +04:00
|
|
|
|
|
|
|
if ( ! isset($listenerData['entities'][$class])) continue;
|
|
|
|
|
|
|
|
$listenerData['listener']->$eventName($event);
|
2011-10-14 00:58:22 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|