diff --git a/lib/Doctrine/ORM/Event/EntityEventDelegator.php b/lib/Doctrine/ORM/Event/EntityEventDelegator.php index d7c46e68e..09532bc4b 100644 --- a/lib/Doctrine/ORM/Event/EntityEventDelegator.php +++ b/lib/Doctrine/ORM/Event/EntityEventDelegator.php @@ -19,14 +19,16 @@ namespace Doctrine\ORM\Event; -use \Doctrine\Common\EventSubscriber; -use \LogicException; +use Doctrine\Common\EventSubscriber; +use LogicException; /** * Delegate events only for certain entities they are registered for. * - * @author Benjamin Eberlei - * @since 2.2 + * @link www.doctrine-project.org + * @author Benjamin Eberlei + * @author Guilherme Blanco + * @since 2.2 */ class EntityEventDelegator implements EventSubscriber { @@ -54,17 +56,23 @@ class EntityEventDelegator implements EventSubscriber 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."); + 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); + $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' => array_flip((array)$entities)); + $this->listeners[$event][$hash] = array( + 'listener' => $listener, + 'entities' => $entities + ); } } @@ -73,6 +81,7 @@ class EntityEventDelegator implements EventSubscriber * 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) { @@ -87,24 +96,27 @@ class EntityEventDelegator implements EventSubscriber public function getSubscribedEvents() { $this->frozen = true; + return array_keys($this->listeners); } /** * Delegate the event to an appropriate listener * - * @param $eventName - * @param $event + * @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])) { - $listenerData['listener']->$eventName($event); - } + + if ( ! isset($listenerData['entities'][$class])) continue; + + $listenerData['listener']->$eventName($event); } } } diff --git a/lib/Doctrine/ORM/Event/LifecycleEventArgs.php b/lib/Doctrine/ORM/Event/LifecycleEventArgs.php index a5dd39cfd..0c91d8475 100644 --- a/lib/Doctrine/ORM/Event/LifecycleEventArgs.php +++ b/lib/Doctrine/ORM/Event/LifecycleEventArgs.php @@ -19,42 +19,59 @@ namespace Doctrine\ORM\Event; +use Doctrine\Common\EventArgs; +use Doctrine\ORM\EntityManager; + /** * Lifecycle Events are triggered by the UnitOfWork during lifecycle transitions * of entities. * - * @since 2.0 + * @link www.doctrine-project.org + * @since 2.0 * @author Roman Borschel * @author Benjamin Eberlei */ -class LifecycleEventArgs extends \Doctrine\Common\EventArgs +class LifecycleEventArgs extends EventArgs { /** - * @var EntityManager + * @var Doctrine\ORM\EntityManager */ - private $_em; + private $em; /** * @var object */ - private $_entity; + private $entity; - public function __construct($entity, $em) + /** + * Constructor + * + * @param object $entity + * @param Doctrine\ORM\EntityManager $em + */ + public function __construct($entity, EntityManager $em) { - $this->_entity = $entity; - $this->_em = $em; + $this->entity = $entity; + $this->em = $em; } + /** + * Retireve associated Entity. + * + * @return object + */ public function getEntity() { - return $this->_entity; + return $this->entity; } /** - * @return EntityManager + * Retrieve associated EntityManager. + * + * @return Doctrine\ORM\EntityManager */ public function getEntityManager() { - return $this->_em; + return $this->em; } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Event/LoadClassMetadataEventArgs.php b/lib/Doctrine/ORM/Event/LoadClassMetadataEventArgs.php index f00520a20..a87f45cc3 100644 --- a/lib/Doctrine/ORM/Event/LoadClassMetadataEventArgs.php +++ b/lib/Doctrine/ORM/Event/LoadClassMetadataEventArgs.php @@ -1,9 +1,25 @@ . + */ namespace Doctrine\ORM\Event; use Doctrine\Common\EventArgs; - use Doctrine\ORM\Mapping\ClassMetadataInfo; use Doctrine\ORM\EntityManager; @@ -11,32 +27,36 @@ use Doctrine\ORM\EntityManager; * Class that holds event arguments for a loadMetadata event. * * @author Jonathan H. Wage - * @since 2.0 + * @since 2.0 */ class LoadClassMetadataEventArgs extends EventArgs { /** - * @var ClassMetadata + * @var Doctrine\ORM\Mapping\ClassMetadata */ private $classMetadata; /** - * @var EntityManager + * @var Doctrine\ORM\EntityManager */ private $em; /** - * @param ClassMetadataInfo $classMetadata - * @param EntityManager $em + * Constructor. + * + * @param Doctrine\ORM\Mapping\ClassMetadataInfo $classMetadata + * @param Doctrine\ORM\EntityManager $em */ public function __construct(ClassMetadataInfo $classMetadata, EntityManager $em) { $this->classMetadata = $classMetadata; - $this->em = $em; + $this->em = $em; } /** - * @return ClassMetadataInfo + * Retrieve associated ClassMetadata. + * + * @return Doctrine\ORM\Mapping\ClassMetadataInfo */ public function getClassMetadata() { @@ -44,7 +64,9 @@ class LoadClassMetadataEventArgs extends EventArgs } /** - * @return EntityManager + * Retrieve associated EntityManager. + * + * @return Doctrine\ORM\EntityManager */ public function getEntityManager() { diff --git a/lib/Doctrine/ORM/Event/OnClearEventArgs.php b/lib/Doctrine/ORM/Event/OnClearEventArgs.php index 60ce4b3eb..49b5e8695 100644 --- a/lib/Doctrine/ORM/Event/OnClearEventArgs.php +++ b/lib/Doctrine/ORM/Event/OnClearEventArgs.php @@ -15,7 +15,7 @@ * This software consists of voluntary contributions made by many individuals * and is licensed under the LGPL. For more information, see * . -*/ + */ namespace Doctrine\ORM\Event; @@ -23,16 +23,15 @@ namespace Doctrine\ORM\Event; * Provides event arguments for the onClear event. * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL - * @link www.doctrine-project.com + * @link www.doctrine-project.org * @since 2.0 - * @version $Revision$ * @author Roman Borschel * @author Benjamin Eberlei */ class OnClearEventArgs extends \Doctrine\Common\EventArgs { /** - * @var \Doctrine\ORM\EntityManager + * @var Doctrine\ORM\EntityManager */ private $em; @@ -42,16 +41,21 @@ class OnClearEventArgs extends \Doctrine\Common\EventArgs private $entityClass; /** - * @param \Doctrine\ORM\EntityManager $em + * Constructor. + * + * @param Doctrine\ORM\EntityManager $em + * @param string $entityClass Optional entity class */ public function __construct($em, $entityClass = null) { - $this->em = $em; + $this->em = $em; $this->entityClass = $entityClass; } /** - * @return \Doctrine\ORM\EntityManager + * Retrieve associated EntityManager. + * + * @return Doctrine\ORM\EntityManager */ public function getEntityManager() { @@ -75,6 +79,6 @@ class OnClearEventArgs extends \Doctrine\Common\EventArgs */ public function clearsAllEntities() { - return $this->entityClass === null; + return ($this->entityClass === null); } } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Event/OnFlushEventArgs.php b/lib/Doctrine/ORM/Event/OnFlushEventArgs.php index 1b4cb9ba8..5e6e839fe 100644 --- a/lib/Doctrine/ORM/Event/OnFlushEventArgs.php +++ b/lib/Doctrine/ORM/Event/OnFlushEventArgs.php @@ -21,37 +21,45 @@ namespace Doctrine\ORM\Event; +use Doctrine\ORM\EntityManager; + /** * Provides event arguments for the preFlush event. * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL - * @link www.doctrine-project.com + * @link www.doctrine-project.org * @since 2.0 - * @version $Revision$ * @author Roman Borschel * @author Benjamin Eberlei */ class OnFlushEventArgs extends \Doctrine\Common\EventArgs { /** - * @var EntityManager + * @var Doctirne\ORM\EntityManager */ - private $_em; + private $em; - //private $_entitiesToPersist = array(); - //private $_entitiesToRemove = array(); + //private $entitiesToPersist = array(); + //private $entitiesToRemove = array(); - public function __construct($em) + /** + * Constructor. + * + * @param Doctrine\ORM\EntityManager $em + */ + public function __construct(EntityManager $em) { - $this->_em = $em; + $this->em = $em; } /** - * @return EntityManager + * Retrieve associated EntityManager. + * + * @return Doctrine\ORM\EntityManager */ public function getEntityManager() { - return $this->_em; + return $this->em; } /* diff --git a/lib/Doctrine/ORM/Event/PostFlushEventArgs.php b/lib/Doctrine/ORM/Event/PostFlushEventArgs.php index 92e88ae21..f500ad92f 100644 --- a/lib/Doctrine/ORM/Event/PostFlushEventArgs.php +++ b/lib/Doctrine/ORM/Event/PostFlushEventArgs.php @@ -17,9 +17,10 @@ * This software consists of voluntary contributions made by many individuals * and is licensed under the LGPL. For more information, see * . -*/ + */ namespace Doctrine\ORM\Event; + use Doctrine\ORM\EntityManager; use Doctrine\Common\EventArgs; @@ -27,20 +28,21 @@ use Doctrine\Common\EventArgs; * Provides event arguments for the postFlush event. * * @license http://www.opensource.org/licenses/lgpl-license.php LGPL - * @link www.doctrine-project.com + * @link www.doctrine-project.org * @since 2.0 - * @version $Revision$ * @author Daniel Freudenberger */ class PostFlushEventArgs extends EventArgs { /** - * @var EntityManager + * @var Doctrine\ORM\EntityManager */ private $em; /** - * @param EntityManager $em + * Constructor. + * + * @param Doctrine\ORM\EntityManager $em */ public function __construct(EntityManager $em) { @@ -48,7 +50,9 @@ class PostFlushEventArgs extends EventArgs } /** - * @return EntityManager + * Retrieve associated EntityManager. + * + * @return Doctrine\ORM\EntityManager */ public function getEntityManager() { diff --git a/lib/Doctrine/ORM/Event/PreUpdateEventArgs.php b/lib/Doctrine/ORM/Event/PreUpdateEventArgs.php index ab1cc15de..35539591a 100644 --- a/lib/Doctrine/ORM/Event/PreUpdateEventArgs.php +++ b/lib/Doctrine/ORM/Event/PreUpdateEventArgs.php @@ -1,4 +1,23 @@ . + */ namespace Doctrine\ORM\Event; @@ -8,42 +27,50 @@ use Doctrine\Common\EventArgs, /** * Class that holds event arguments for a preInsert/preUpdate event. * + * @author Guilherme Blanco * @author Roman Borschel * @author Benjamin Eberlei - * @since 2.0 + * @since 2.0 */ class PreUpdateEventArgs extends LifecycleEventArgs { /** * @var array */ - private $_entityChangeSet; + private $entityChangeSet; /** - * + * Constructor. + * * @param object $entity - * @param EntityManager $em + * @param Doctrine\ORM\EntityManager $em * @param array $changeSet */ - public function __construct($entity, $em, array &$changeSet) + public function __construct($entity, EntityManager $em, array &$changeSet) { parent::__construct($entity, $em); - $this->_entityChangeSet = &$changeSet; - } - - public function getEntityChangeSet() - { - return $this->_entityChangeSet; + + $this->entityChangeSet = &$changeSet; } /** - * Field has a changeset? + * Retrieve entity changeset. + * + * @return array + */ + public function getEntityChangeSet() + { + return $this->entityChangeSet; + } + + /** + * Check if field has a changeset. * - * @return bool + * @return boolean */ public function hasChangedField($field) { - return isset($this->_entityChangeSet[$field]); + return isset($this->entityChangeSet[$field]); } /** @@ -54,9 +81,9 @@ class PreUpdateEventArgs extends LifecycleEventArgs */ public function getOldValue($field) { - $this->_assertValidField($field); + $this->assertValidField($field); - return $this->_entityChangeSet[$field][0]; + return $this->entityChangeSet[$field][0]; } /** @@ -67,9 +94,9 @@ class PreUpdateEventArgs extends LifecycleEventArgs */ public function getNewValue($field) { - $this->_assertValidField($field); + $this->assertValidField($field); - return $this->_entityChangeSet[$field][1]; + return $this->entityChangeSet[$field][1]; } /** @@ -80,18 +107,24 @@ class PreUpdateEventArgs extends LifecycleEventArgs */ public function setNewValue($field, $value) { - $this->_assertValidField($field); + $this->assertValidField($field); - $this->_entityChangeSet[$field][1] = $value; + $this->entityChangeSet[$field][1] = $value; } - private function _assertValidField($field) + /** + * Assert the field exists in changeset. + * + * @param string $field + */ + private function assertValidField($field) { - if (!isset($this->_entityChangeSet[$field])) { - throw new \InvalidArgumentException( - "Field '".$field."' is not a valid field of the entity ". - "'".get_class($this->getEntity())."' in PreUpdateEventArgs." - ); + if ( ! isset($this->entityChangeSet[$field])) { + throw new \InvalidArgumentException(sprintf( + 'Field "%s" is not a valid field of the entity "%s" in PreUpdateEventArgs.', + $field, + get_class($this->getEntity()) + )); } } }