Skip to content

Commit

Permalink
prevent infinite loop on historyEntry deletion #3576
Browse files Browse the repository at this point in the history
  • Loading branch information
numew committed Jan 23, 2025
1 parent 61d61de commit 9f47f05
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/EventListener/EntityHistoryListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#[AsDoctrineListener(event: Events::postPersist)]
#[AsDoctrineListener(event: Events::postUpdate)]
#[AsDoctrineListener(event: Events::preRemove)]
readonly class EntityHistoryListener
class EntityHistoryListener
{
public const array FIELDS_TO_IGNORE = [
'lastLoginAt',
Expand All @@ -27,13 +27,15 @@
'lastSuiviIsPublic',
];

private array $deletedObjects;

public function __construct(
private HistoryEntryManager $historyEntryManager,
private EntityManagerInterface $entityManager,
private EntityComparator $entityComparator,
private LoggerInterface $logger,
private readonly HistoryEntryManager $historyEntryManager,
private readonly EntityManagerInterface $entityManager,
private readonly EntityComparator $entityComparator,
private readonly LoggerInterface $logger,
#[Autowire(env: 'HISTORY_TRACKING_ENABLE')]
private string $historyTrackingEnable,
private readonly string $historyTrackingEnable,
) {
}

Expand Down Expand Up @@ -102,13 +104,16 @@ protected function processEntity(LifecycleEventArgs $eventArgs, HistoryEntryEven
}
}
if (HistoryEntryEvent::DELETE === $event) {
$changes = $this->entityComparator->getEntityPropertiesAndValueNormalized($entity);
$className = $this->entityManager->getMetadataFactory()->getMetadataFor($entity::class)->getName();
if (!isset($this->deletedObjects[$className][$entity->getId()])) {
$this->deletedObjects[$className][$entity->getId()] = $entity;
$changes = $this->entityComparator->getEntityPropertiesAndValueNormalized($entity);
}
}

if (in_array($event, [HistoryEntryEvent::UPDATE, HistoryEntryEvent::DELETE]) && empty($changes)) {
return;
}

$this->saveEntityHistory($event, $entity, $changes);
}

Expand Down

0 comments on commit 9f47f05

Please sign in to comment.