1616use Doctrine \Persistence \Mapping \RuntimeReflectionService ;
1717use Psr \Log \LoggerInterface ;
1818use Psr \Log \NullLogger ;
19- use WeakReference ;
2019use Webfactory \Bundle \PolyglotBundle \Locale \DefaultLocaleProvider ;
2120
2221final class PolyglotListener
@@ -39,9 +38,9 @@ final class PolyglotListener
3938 private array $ translatedClasses = [];
4039
4140 /**
42- * @var array<WeakReference >
41+ * @var \WeakMap<object, true >
4342 */
44- private array $ entitiesWithTranslatables = [] ;
43+ private \ WeakMap $ entitiesWithTranslatables ;
4544
4645 /**
4746 * @var list<PersistentTranslatable>
@@ -53,6 +52,12 @@ public function __construct(
5352 private readonly LoggerInterface $ logger = null ?? new NullLogger (),
5453 private readonly RuntimeReflectionService $ reflectionService = new RuntimeReflectionService (),
5554 ) {
55+ $ this ->entitiesWithTranslatables = new \WeakMap ();
56+ }
57+
58+ public function onClear (OnClearEventArgs $ args ): void
59+ {
60+ $ this ->entitiesWithTranslatables = new \WeakMap ();
5661 }
5762
5863 public function postLoad (LifecycleEventArgs $ event ): void
@@ -69,6 +74,10 @@ public function prePersist(LifecycleEventArgs $event): void
6974
7075 private function injectPersistentTranslatables (EntityManager $ entityManager , object $ object ): void
7176 {
77+ if (isset ($ this ->entitiesWithTranslatables [$ object ])) {
78+ return ;
79+ }
80+
7281 $ hasTranslatables = false ;
7382
7483 foreach ($ this ->getTranslationMetadatas ($ object , $ entityManager ) as $ tm ) {
@@ -77,21 +86,15 @@ private function injectPersistentTranslatables(EntityManager $entityManager, obj
7786 }
7887
7988 if ($ hasTranslatables ) {
80- $ this ->entitiesWithTranslatables [] = WeakReference:: create ( $ object ) ;
89+ $ this ->entitiesWithTranslatables [$ object ] = true ;
8190 }
8291 }
8392
8493 public function preFlush (PreFlushEventArgs $ event ): void
8594 {
8695 $ em = $ event ->getObjectManager ();
8796
88- foreach ($ this ->entitiesWithTranslatables as $ key => $ weakRef ) {
89- $ object = $ weakRef ->get ();
90- if (null === $ object ) {
91- unset($ this ->entitiesWithTranslatables [$ key ]);
92- continue ;
93- }
94-
97+ foreach ($ this ->entitiesWithTranslatables as $ object => $ ignored ) {
9598 foreach ($ this ->getTranslationMetadatas ($ object , $ em ) as $ tm ) {
9699 $ this ->ejectedTranslatables = array_merge ($ this ->ejectedTranslatables , $ tm ->ejectPersistentTranslatables ($ object ));
97100 }
0 commit comments