Skip to content

Commit

Permalink
Short-circuit the event compat check with ORM 3
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Jun 9, 2024
1 parent 74cc0ce commit 69e3666
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/SoftDeleteable/SoftDeleteableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\UnitOfWork as MongoDBUnitOfWork;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
use Doctrine\Persistence\Event\ManagerEventArgs;
use Doctrine\Persistence\Mapping\ClassMetadata;
Expand Down Expand Up @@ -92,7 +93,7 @@ public function onFlush(EventArgs $args)

if ($evm->hasListeners(self::PRE_SOFT_DELETE)) {
// @todo: in the next major remove check and only instantiate the event
$preSoftDeleteEventArgs = $this->hasToDispatchNewEvent($evm, self::PRE_SOFT_DELETE, PreSoftDeleteEventArgs::class)
$preSoftDeleteEventArgs = $this->hasToDispatchNewEvent($om, $evm, self::PRE_SOFT_DELETE, PreSoftDeleteEventArgs::class)
? new PreSoftDeleteEventArgs($object, $om)
: $ea->createLifecycleEventArgsInstance($object, $om);

Expand All @@ -116,7 +117,7 @@ public function onFlush(EventArgs $args)

if ($evm->hasListeners(self::POST_SOFT_DELETE)) {
// @todo: in the next major remove check and only instantiate the event
$postSoftDeleteEventArgs = $this->hasToDispatchNewEvent($evm, self::POST_SOFT_DELETE, PostSoftDeleteEventArgs::class)
$postSoftDeleteEventArgs = $this->hasToDispatchNewEvent($om, $evm, self::POST_SOFT_DELETE, PostSoftDeleteEventArgs::class)
? new PostSoftDeleteEventArgs($object, $om)
: $ea->createLifecycleEventArgsInstance($object, $om);

Expand Down Expand Up @@ -149,8 +150,12 @@ protected function getNamespace()
}

/** @param class-string $eventClass */
private function hasToDispatchNewEvent(EventManager $eventManager, string $eventName, string $eventClass): bool
private function hasToDispatchNewEvent(ObjectManager $objectManager, EventManager $eventManager, string $eventName, string $eventClass): bool
{
if ($objectManager instanceof EntityManagerInterface && !class_exists(LifecycleEventArgs::class)) {
return true;
}

foreach ($eventManager->getListeners($eventName) as $listener) {
$reflMethod = new \ReflectionMethod($listener, $eventName);

Expand Down

0 comments on commit 69e3666

Please sign in to comment.