. */ namespace Doctrine\ORM\Event; use Doctrine\Common\EventSubscriber; use LogicException; /** * Delegate events only for certain entities they are registered for. * * @link www.doctrine-project.org * @author Benjamin Eberlei * @author Guilherme Blanco * @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); $entities = array_flip((array) $entities); 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' => $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. * @param array $entities */ 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 string $eventName * @param array $args * @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])) continue; $listenerData['listener']->$eventName($event); } } }