Permalink
Browse files

call listeners in UoW

  • Loading branch information...
FabioBatSilva authored and fabio.silva committed Jul 31, 2012
1 parent ccc0a2a commit 315f7ba43b9c2226c9d801142ade87c4f5a8f56a
@@ -22,15 +22,21 @@
use Exception;
use InvalidArgumentException;
use UnexpectedValueException;
+
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\NotifyPropertyChanged;
use Doctrine\Common\PropertyChangedListener;
use Doctrine\Common\Persistence\ObjectManagerAware;
-use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Proxy\Proxy;
+use Doctrine\ORM\Event\LifecycleEventArgs;
+use Doctrine\ORM\Event\PreUpdateEventArgs;
+use Doctrine\ORM\Event\PreFlushEventArgs;
+use Doctrine\ORM\Event\OnFlushEventArgs;
+use Doctrine\ORM\Event\PostFlushEventArgs;
+
/**
* The UnitOfWork is responsible for tracking changes to objects during an
* "object-level" transaction and for writing out changes to the database
@@ -267,7 +273,7 @@ public function commit($entity = null)
{
// Raise preFlush
if ($this->evm->hasListeners(Events::preFlush)) {
- $this->evm->dispatchEvent(Events::preFlush, new Event\PreFlushEventArgs($this->em));
+ $this->evm->dispatchEvent(Events::preFlush, new PreFlushEventArgs($this->em));
}
// Compute changes done since last commit.
@@ -511,6 +517,11 @@ public function computeChangeSet(ClassMetadata $class, $entity)
$class->invokeLifecycleCallbacks(Events::preFlush, $entity);
}
+ // Fire PreFlush entity listeners
+ if (isset($class->entityListeners[Events::preFlush])) {
+ $class->dispatchEntityListeners(Events::preFlush, $entity, new PreFlushEventArgs($this->em));
+ }
+
$actualData = array();
foreach ($class->reflFields as $name => $refProp) {
@@ -798,21 +809,32 @@ private function computeAssociationChanges($assoc, $value)
}
/**
- * @param ClassMetadata $class
+ * @param \Doctrine\ORM\Mapping\ClassMetadata $class
* @param object $entity
*
* @return void
*/
private function persistNew($class, $entity)
{
- $oid = spl_object_hash($entity);
+ $oid = spl_object_hash($entity);
+ $hasListeners = $this->evm->hasListeners(Events::prePersist);
+ $hasEntityListeners = isset($class->entityListeners[Events::prePersist]);
+ $hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::prePersist]);
+
+ if ($hasListeners || $hasEntityListeners) {
+ $event = new LifecycleEventArgs($entity, $this->em);
+ }
- if (isset($class->lifecycleCallbacks[Events::prePersist])) {
+ if ($hasLifecycleCallbacks) {
$class->invokeLifecycleCallbacks(Events::prePersist, $entity);
}
- if ($this->evm->hasListeners(Events::prePersist)) {
- $this->evm->dispatchEvent(Events::prePersist, new LifecycleEventArgs($entity, $this->em));
+ if ($hasEntityListeners) {
+ $class->dispatchEntityListeners(Events::prePersist, $entity, $event);
+ }
+
+ if ($hasListeners) {
+ $this->evm->dispatchEvent(Events::prePersist, $event);
}
$idGen = $class->idGenerator;
@@ -913,6 +935,7 @@ private function executeInserts($class)
$entities = array();
$hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postPersist]);
+ $hasEntityListeners = isset($class->entityListeners[Events::postPersist]);
$hasListeners = $this->evm->hasListeners(Events::postPersist);
foreach ($this->entityInsertions as $oid => $entity) {
@@ -924,7 +947,7 @@ private function executeInserts($class)
unset($this->entityInsertions[$oid]);
- if ($hasLifecycleCallbacks || $hasListeners) {
+ if ($hasLifecycleCallbacks || $hasEntityListeners || $hasListeners) {
$entities[] = $entity;
}
}
@@ -946,14 +969,23 @@ private function executeInserts($class)
$this->addToIdentityMap($entity);
}
}
-
+
foreach ($entities as $entity) {
+
+ if ($hasListeners || $hasEntityListeners) {
+ $event = new LifecycleEventArgs($entity, $this->em);
+ }
+
if ($hasLifecycleCallbacks) {
$class->invokeLifecycleCallbacks(Events::postPersist, $entity);
}
+ if ($hasEntityListeners) {
+ $class->dispatchEntityListeners(Events::postPersist, $entity, $event);
+ }
+
if ($hasListeners) {
- $this->evm->dispatchEvent(Events::postPersist, new LifecycleEventArgs($entity, $this->em));
+ $this->evm->dispatchEvent(Events::postPersist, $event);
}
}
}
@@ -971,27 +1003,38 @@ private function executeUpdates($class)
$persister = $this->getEntityPersister($className);
$hasPreUpdateLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::preUpdate]);
+ $hasPreUpdateEntityListeners = isset($class->entityListeners[Events::preUpdate]);
$hasPreUpdateListeners = $this->evm->hasListeners(Events::preUpdate);
$hasPostUpdateLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postUpdate]);
+ $hasPostUpdateEntityListeners = isset($class->entityListeners[Events::postUpdate]);
$hasPostUpdateListeners = $this->evm->hasListeners(Events::postUpdate);
foreach ($this->entityUpdates as $oid => $entity) {
if ($this->em->getClassMetadata(get_class($entity))->name !== $className) {
continue;
}
+ if ($hasPreUpdateListeners || $hasPreUpdateEntityListeners) {
+ $preEvent = new PreUpdateEventArgs($entity, $this->em, $this->entityChangeSets[$oid]);
+ }
+
+ if ($hasPostUpdateListeners || $hasPostUpdateEntityListeners) {
+ $postEvent = new LifecycleEventArgs($entity, $this->em);
+ }
+
if ($hasPreUpdateLifecycleCallbacks) {
$class->invokeLifecycleCallbacks(Events::preUpdate, $entity);
$this->recomputeSingleEntityChangeSet($class, $entity);
}
+ if ($hasPreUpdateEntityListeners) {
+ $class->dispatchEntityListeners(Events::preUpdate, $entity, $preEvent);
+ }
+
if ($hasPreUpdateListeners) {
- $this->evm->dispatchEvent(
- Events::preUpdate,
- new Event\PreUpdateEventArgs($entity, $this->em, $this->entityChangeSets[$oid])
- );
+ $this->evm->dispatchEvent(Events::preUpdate, $preEvent);
}
if (!empty($this->entityChangeSets[$oid])) {
@@ -1004,8 +1047,12 @@ private function executeUpdates($class)
$class->invokeLifecycleCallbacks(Events::postUpdate, $entity);
}
+ if ($hasPostUpdateEntityListeners) {
+ $class->dispatchEntityListeners(Events::postUpdate, $entity, $postEvent);
+ }
+
if ($hasPostUpdateListeners) {
- $this->evm->dispatchEvent(Events::postUpdate, new LifecycleEventArgs($entity, $this->em));
+ $this->evm->dispatchEvent(Events::postUpdate, $postEvent);
}
}
}
@@ -1022,8 +1069,9 @@ private function executeDeletions($class)
$className = $class->name;
$persister = $this->getEntityPersister($className);
- $hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postRemove]);
- $hasListeners = $this->evm->hasListeners(Events::postRemove);
+ $hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postRemove]);
+ $hasEntityListeners = isset($class->entityListeners[Events::postRemove]);
+ $hasListeners = $this->evm->hasListeners(Events::postRemove);
foreach ($this->entityDeletions as $oid => $entity) {
if ($this->em->getClassMetadata(get_class($entity))->name !== $className) {
@@ -1046,12 +1094,20 @@ private function executeDeletions($class)
$class->reflFields[$class->identifier[0]]->setValue($entity, null);
}
+ if ($hasListeners || $hasEntityListeners) {
+ $event = new LifecycleEventArgs($entity, $this->em);
+ }
+
if ($hasLifecycleCallbacks) {
$class->invokeLifecycleCallbacks(Events::postRemove, $entity);
}
+ if ($hasEntityListeners) {
+ $class->dispatchEntityListeners(Events::postRemove, $entity, $event);
+ }
+
if ($hasListeners) {
- $this->evm->dispatchEvent(Events::postRemove, new LifecycleEventArgs($entity, $this->em));
+ $this->evm->dispatchEvent(Events::postRemove, $event);
}
}
}
@@ -1691,12 +1747,24 @@ private function doRemove($entity, array &$visited)
break;
case self::STATE_MANAGED:
- if (isset($class->lifecycleCallbacks[Events::preRemove])) {
+ $hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::preRemove]);
+ $hasEntityListeners = isset($class->entityListeners[Events::preRemove]);
+ $hasListeners = $this->evm->hasListeners(Events::preRemove);
+
+ if ($hasListeners || $hasEntityListeners) {
+ $event = new LifecycleEventArgs($entity, $this->em);
+ }
+
+ if ($hasLifecycleCallbacks) {
$class->invokeLifecycleCallbacks(Events::preRemove, $entity);
}
- if ($this->evm->hasListeners(Events::preRemove)) {
- $this->evm->dispatchEvent(Events::preRemove, new LifecycleEventArgs($entity, $this->em));
+ if ($hasEntityListeners) {
+ $class->dispatchEntityListeners(Events::preRemove, $entity, $event);
+ }
+
+ if ($hasListeners) {
+ $this->evm->dispatchEvent(Events::preRemove, $event);
}
$this->scheduleForDelete($entity);
@@ -2695,13 +2763,24 @@ public function createEntity($className, array $data, &$hints = array())
}
if ($overrideLocalValues) {
- if (isset($class->lifecycleCallbacks[Events::postLoad])) {
+ $hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::postLoad]);
+ $hasEntityListeners = isset($class->entityListeners[Events::postLoad]);
+ $hasListeners = $this->evm->hasListeners(Events::postLoad);
+
+ if ($hasListeners || $hasEntityListeners) {
+ $event = new LifecycleEventArgs($entity, $this->em);
+ }
+
+ if ($hasLifecycleCallbacks) {
$class->invokeLifecycleCallbacks(Events::postLoad, $entity);
}
+ if ($hasEntityListeners) {
+ $class->dispatchEntityListeners(Events::postLoad, $entity, $event);
+ }
- if ($this->evm->hasListeners(Events::postLoad)) {
- $this->evm->dispatchEvent(Events::postLoad, new LifecycleEventArgs($entity, $this->em));
+ if ($hasListeners) {
+ $this->evm->dispatchEvent(Events::postLoad, $event);
}
}
@@ -3177,14 +3256,14 @@ public function isReadOnly($object)
private function dispatchOnFlushEvent()
{
if ($this->evm->hasListeners(Events::onFlush)) {
- $this->evm->dispatchEvent(Events::onFlush, new Event\OnFlushEventArgs($this->em));
+ $this->evm->dispatchEvent(Events::onFlush, new OnFlushEventArgs($this->em));
}
}
private function dispatchPostFlushEvent()
{
if ($this->evm->hasListeners(Events::postFlush)) {
- $this->evm->dispatchEvent(Events::postFlush, new Event\PostFlushEventArgs($this->em));
+ $this->evm->dispatchEvent(Events::postFlush, new PostFlushEventArgs($this->em));
}
}
}
@@ -4,8 +4,19 @@
class ContractSubscriber
{
- static public $prePersistCalls;
static public $postPersistCalls;
+ static public $prePersistCalls;
+
+ static public $postUpdateCalls;
+ static public $preUpdateCalls;
+
+ static public $postRemoveCalls;
+ static public $preRemoveCalls;
+
+ static public $preFlushCalls;
+
+ static public $postLoadCalls;
+
static public $instances;
public function __construct()
@@ -28,4 +39,53 @@ public function prePersistHandler(CompanyContract $contract)
{
self::$prePersistCalls[] = func_get_args();
}
+
+ /**
+ * @PostUpdate
+ */
+ public function postUpdateHandler(CompanyContract $contract)
+ {
+ self::$postUpdateCalls[] = func_get_args();
+ }
+
+ /**
+ * @PreUpdate
+ */
+ public function preUpdateHandler(CompanyContract $contract)
+ {
+ self::$preUpdateCalls[] = func_get_args();
+ }
+
+ /**
+ * @PostRemove
+ */
+ public function postRemoveHandler(CompanyContract $contract)
+ {
+ self::$postRemoveCalls[] = func_get_args();
+ }
+
+ /**
+ * @PreRemove
+ */
+ public function preRemoveHandler(CompanyContract $contract)
+ {
+ self::$preRemoveCalls[] = func_get_args();
+ }
+
+ /**
+ * @PreFlush
+ */
+ public function preFlushHandler(CompanyContract $contract)
+ {
+ self::$preFlushCalls[] = func_get_args();
+ }
+
+ /**
+ * @PostLoad
+ */
+ public function postLoadHandler(CompanyContract $contract)
+ {
+ self::$postLoadCalls[] = func_get_args();
+ }
+
}
Oops, something went wrong.

0 comments on commit 315f7ba

Please sign in to comment.