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

View File

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

View File

@ -2,7 +2,7 @@
namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\ORM\UnitOfWork;
use Doctrine\ORM\Event\LifecycleEventArgs;
/**
* @group DDC-1707
@ -25,9 +25,11 @@ class DDC1707Test extends \Doctrine\Tests\OrmFunctionalTestCase
public function testPostLoadOnChild()
{
$class = $this->_em->getClassMetadata(__NAMESPACE__ . '\DDC1707Child');
$class = $this->_em->getClassMetadata(__NAMESPACE__ . '\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);
}