1
0
Fork 0
mirror of synced 2025-04-03 13:23:37 +03:00

give event to lifecycle callbacks

This commit is contained in:
Fabio B. Silva 2012-07-31 20:10:36 -03:00 committed by fabio.silva
parent dbd0697c2c
commit c6adcda567
4 changed files with 33 additions and 25 deletions

View file

@ -2434,10 +2434,10 @@ class ClassMetadataInfo implements ClassMetadata
* *
* @return void * @return void
*/ */
public function invokeLifecycleCallbacks($lifecycleEvent, $entity) public function invokeLifecycleCallbacks($lifecycleEvent, $entity, EventArgs $event)
{ {
foreach ($this->lifecycleCallbacks[$lifecycleEvent] as $callback) { foreach ($this->lifecycleCallbacks[$lifecycleEvent] as $callback) {
$entity->$callback(); $entity->$callback($event);
} }
} }

View file

@ -502,7 +502,13 @@ class UnitOfWork implements PropertyChangedListener
*/ */
public function computeChangeSet(ClassMetadata $class, $entity) public function computeChangeSet(ClassMetadata $class, $entity)
{ {
$oid = spl_object_hash($entity); $oid = spl_object_hash($entity);
$hasEntityListeners = isset($class->entityListeners[Events::preFlush]);
$hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::preFlush]);
if ($hasEntityListeners || $hasLifecycleCallbacks) {
$event = new PreFlushEventArgs($entity, $this->em);
}
if (isset($this->readOnlyObjects[$oid])) { if (isset($this->readOnlyObjects[$oid])) {
return; return;
@ -513,13 +519,13 @@ class UnitOfWork implements PropertyChangedListener
} }
// Fire PreFlush lifecycle callbacks // Fire PreFlush lifecycle callbacks
if (isset($class->lifecycleCallbacks[Events::preFlush])) { if ($hasLifecycleCallbacks) {
$class->invokeLifecycleCallbacks(Events::preFlush, $entity); $class->invokeLifecycleCallbacks(Events::preFlush, $entity, $event);
} }
// Fire PreFlush entity listeners // Fire PreFlush entity listeners
if (isset($class->entityListeners[Events::preFlush])) { if ($hasEntityListeners) {
$class->dispatchEntityListeners(Events::preFlush, $entity, new PreFlushEventArgs($this->em)); $class->dispatchEntityListeners(Events::preFlush, $entity, $event);
} }
$actualData = array(); $actualData = array();
@ -821,12 +827,12 @@ class UnitOfWork implements PropertyChangedListener
$hasEntityListeners = isset($class->entityListeners[Events::prePersist]); $hasEntityListeners = isset($class->entityListeners[Events::prePersist]);
$hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::prePersist]); $hasLifecycleCallbacks = isset($class->lifecycleCallbacks[Events::prePersist]);
if ($hasListeners || $hasEntityListeners) { if ($hasListeners || $hasEntityListeners || $hasLifecycleCallbacks) {
$event = new LifecycleEventArgs($entity, $this->em); $event = new LifecycleEventArgs($entity, $this->em);
} }
if ($hasLifecycleCallbacks) { if ($hasLifecycleCallbacks) {
$class->invokeLifecycleCallbacks(Events::prePersist, $entity); $class->invokeLifecycleCallbacks(Events::prePersist, $entity, $event);
} }
if ($hasEntityListeners) { if ($hasEntityListeners) {
@ -972,12 +978,12 @@ class UnitOfWork implements PropertyChangedListener
foreach ($entities as $entity) { foreach ($entities as $entity) {
if ($hasListeners || $hasEntityListeners) { if ($hasListeners || $hasEntityListeners | $hasLifecycleCallbacks) {
$event = new LifecycleEventArgs($entity, $this->em); $event = new LifecycleEventArgs($entity, $this->em);
} }
if ($hasLifecycleCallbacks) { if ($hasLifecycleCallbacks) {
$class->invokeLifecycleCallbacks(Events::postPersist, $entity); $class->invokeLifecycleCallbacks(Events::postPersist, $entity, $event);
} }
if ($hasEntityListeners) { if ($hasEntityListeners) {
@ -1015,16 +1021,16 @@ class UnitOfWork implements PropertyChangedListener
continue; continue;
} }
if ($hasPreUpdateListeners || $hasPreUpdateEntityListeners) { if ($hasPreUpdateListeners || $hasPreUpdateEntityListeners || $hasPreUpdateLifecycleCallbacks) {
$preEvent = new PreUpdateEventArgs($entity, $this->em, $this->entityChangeSets[$oid]); $preEvent = new PreUpdateEventArgs($entity, $this->em, $this->entityChangeSets[$oid]);
} }
if ($hasPostUpdateListeners || $hasPostUpdateEntityListeners) { if ($hasPostUpdateListeners || $hasPostUpdateEntityListeners || $hasPostUpdateLifecycleCallbacks) {
$postEvent = new LifecycleEventArgs($entity, $this->em); $postEvent = new LifecycleEventArgs($entity, $this->em);
} }
if ($hasPreUpdateLifecycleCallbacks) { if ($hasPreUpdateLifecycleCallbacks) {
$class->invokeLifecycleCallbacks(Events::preUpdate, $entity); $class->invokeLifecycleCallbacks(Events::preUpdate, $entity, $preEvent);
$this->recomputeSingleEntityChangeSet($class, $entity); $this->recomputeSingleEntityChangeSet($class, $entity);
} }
@ -1044,7 +1050,7 @@ class UnitOfWork implements PropertyChangedListener
unset($this->entityUpdates[$oid]); unset($this->entityUpdates[$oid]);
if ($hasPostUpdateLifecycleCallbacks) { if ($hasPostUpdateLifecycleCallbacks) {
$class->invokeLifecycleCallbacks(Events::postUpdate, $entity); $class->invokeLifecycleCallbacks(Events::postUpdate, $entity, $postEvent);
} }
if ($hasPostUpdateEntityListeners) { if ($hasPostUpdateEntityListeners) {
@ -1094,12 +1100,12 @@ class UnitOfWork implements PropertyChangedListener
$class->reflFields[$class->identifier[0]]->setValue($entity, null); $class->reflFields[$class->identifier[0]]->setValue($entity, null);
} }
if ($hasListeners || $hasEntityListeners) { if ($hasListeners || $hasEntityListeners || $hasLifecycleCallbacks) {
$event = new LifecycleEventArgs($entity, $this->em); $event = new LifecycleEventArgs($entity, $this->em);
} }
if ($hasLifecycleCallbacks) { if ($hasLifecycleCallbacks) {
$class->invokeLifecycleCallbacks(Events::postRemove, $entity); $class->invokeLifecycleCallbacks(Events::postRemove, $entity, $event);
} }
if ($hasEntityListeners) { if ($hasEntityListeners) {
@ -1751,12 +1757,12 @@ class UnitOfWork implements PropertyChangedListener
$hasEntityListeners = isset($class->entityListeners[Events::preRemove]); $hasEntityListeners = isset($class->entityListeners[Events::preRemove]);
$hasListeners = $this->evm->hasListeners(Events::preRemove); $hasListeners = $this->evm->hasListeners(Events::preRemove);
if ($hasListeners || $hasEntityListeners) { if ($hasListeners || $hasEntityListeners || $hasLifecycleCallbacks) {
$event = new LifecycleEventArgs($entity, $this->em); $event = new LifecycleEventArgs($entity, $this->em);
} }
if ($hasLifecycleCallbacks) { if ($hasLifecycleCallbacks) {
$class->invokeLifecycleCallbacks(Events::preRemove, $entity); $class->invokeLifecycleCallbacks(Events::preRemove, $entity, $event);
} }
if ($hasEntityListeners) { if ($hasEntityListeners) {
@ -2767,12 +2773,12 @@ class UnitOfWork implements PropertyChangedListener
$hasEntityListeners = isset($class->entityListeners[Events::postLoad]); $hasEntityListeners = isset($class->entityListeners[Events::postLoad]);
$hasListeners = $this->evm->hasListeners(Events::postLoad); $hasListeners = $this->evm->hasListeners(Events::postLoad);
if ($hasListeners || $hasEntityListeners) { if ($hasListeners || $hasEntityListeners || $hasLifecycleCallbacks) {
$event = new LifecycleEventArgs($entity, $this->em); $event = new LifecycleEventArgs($entity, $this->em);
} }
if ($hasLifecycleCallbacks) { if ($hasLifecycleCallbacks) {
$class->invokeLifecycleCallbacks(Events::postLoad, $entity); $class->invokeLifecycleCallbacks(Events::postLoad, $entity, $event);
} }
if ($hasEntityListeners) { if ($hasEntityListeners) {

View file

@ -10,7 +10,7 @@ require_once __DIR__ . '/../../TestInit.php';
/** /**
* @group DDC-1955 * @group DDC-1955
*/ */
class EntityListenersDispatcherTest extends \Doctrine\Tests\OrmFunctionalTestCase class EntityListenersTest extends \Doctrine\Tests\OrmFunctionalTestCase
{ {
protected function setUp() protected function setUp()

View file

@ -2,7 +2,7 @@
namespace Doctrine\Tests\ORM\Functional\Ticket; namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\ORM\UnitOfWork; use Doctrine\ORM\Event\LifecycleEventArgs;
/** /**
* @group DDC-1707 * @group DDC-1707
@ -25,9 +25,11 @@ class DDC1707Test extends \Doctrine\Tests\OrmFunctionalTestCase
public function testPostLoadOnChild() public function testPostLoadOnChild()
{ {
$class = $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1707Child'); $class = $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1707Child');
$entity = new DDC1707Child(); $entity = new DDC1707Child();
$class->invokeLifecycleCallbacks(\Doctrine\ORM\Events::postLoad, $entity); $event = new LifecycleEventArgs($entity, $this->_em);
$class->invokeLifecycleCallbacks(\Doctrine\ORM\Events::postLoad, $entity, $event);
$this->assertTrue($entity->postLoad); $this->assertTrue($entity->postLoad);
} }