@@ -21,18 +21,13 @@ final class PersistedObjectsTracker
2121 /**
2222 * This buffer of objects needs to be static to be kept between two kernel.reset events.
2323 *
24- * @var list<\WeakReference <object>>
24+ * @var \WeakMap <object, mixed> keys: objects, values: value ids
2525 */
26- private static array $ buffer = [];
27-
28- /**
29- * @var \WeakMap<object, mixed>
30- */
31- private static \WeakMap $ ids ;
26+ private static \WeakMap $ buffer ;
3227
3328 public function __construct ()
3429 {
35- self ::$ ids ??= new \WeakMap ();
30+ self ::$ buffer ??= new \WeakMap ();
3631 }
3732
3833 public function refresh (): void
@@ -43,41 +38,33 @@ public function refresh(): void
4338 public function add (object ...$ objects ): void
4439 {
4540 foreach ($ objects as $ object ) {
46- self ::$ buffer [] = \WeakReference::create ($ object );
47-
48- $ id = Configuration::instance ()->persistence ()->getIdentifierValues ($ object );
49- if ($ id ) {
50- self ::$ ids [$ object ] = $ id ;
41+ if (self ::$ buffer ->offsetExists ($ object ) && self ::$ buffer [$ object ]) {
42+ continue ;
5143 }
44+
45+ self ::$ buffer [$ object ] = Configuration::instance ()->persistence ()->getIdentifierValues ($ object );
5246 }
5347 }
5448
5549 public static function updateIds (): void
5650 {
57- foreach (self ::$ buffer as $ reference ) {
58- if (null === $ object = $ reference ->get ()) {
59- continue ;
60- }
61-
62- if (self ::$ ids ->offsetExists ($ object )) {
51+ foreach (self ::$ buffer as $ object => $ id ) {
52+ if ($ id ) {
6353 continue ;
6454 }
6555
66- self ::$ ids [$ object ] = Configuration::instance ()->persistence ()->getIdentifierValues ($ object );
56+ self ::$ buffer [$ object ] = Configuration::instance ()->persistence ()->getIdentifierValues ($ object );
6757 }
6858 }
6959
7060 public static function reset (): void
7161 {
72- self ::$ buffer = [];
73- self ::$ ids = new \WeakMap ();
62+ self ::$ buffer = new \WeakMap ();
7463 }
7564
7665 public static function countObjects (): int
7766 {
78- return \count (
79- \array_filter (self ::$ buffer , static fn (\WeakReference $ weakRef ) => null !== $ weakRef ->get ())
80- );
67+ return \count (self ::$ buffer );
8168 }
8269
8370 private static function proxifyObjects (): void
@@ -86,30 +73,21 @@ private static function proxifyObjects(): void
8673 return ;
8774 }
8875
89- self ::$ buffer = \array_values (
90- \array_map (
91- static function (\WeakReference $ weakRef ) {
92- $ object = $ weakRef ->get () ?? throw new \LogicException ('Object cannot be null. ' );
93-
94- $ reflector = new \ReflectionClass ($ object );
95-
96- if ($ reflector ->isUninitializedLazyObject ($ object )) {
97- return \WeakReference::create ($ object );
98- }
99-
100- $ clone = clone $ object ;
101- $ reflector ->resetAsLazyGhost ($ object , function ($ object ) use ($ clone ) {
102- $ id = self ::$ ids [$ object ] ?? throw new \LogicException ('Canot find the id for object ' );
76+ foreach (self ::$ buffer as $ object => $ id ) {
77+ if (!$ id ) {
78+ continue ;
79+ }
10380
104- Configuration::instance ()->persistence ()->autorefresh ($ object , $ id , $ clone );
105- });
81+ $ reflector = new \ReflectionClass ($ object );
10682
107- return \WeakReference::create ($ object );
108- },
83+ if ($ reflector ->isUninitializedLazyObject ($ object )) {
84+ continue ;
85+ }
10986
110- // remove all empty references
111- \array_filter (self ::$ buffer , static fn (\WeakReference $ weakRef ) => null !== $ weakRef ->get ()),
112- )
113- );
87+ $ clone = clone $ object ;
88+ $ reflector ->resetAsLazyGhost ($ object , function ($ object ) use ($ clone , $ id ) {
89+ Configuration::instance ()->persistence ()->autorefresh ($ object , $ id , $ clone );
90+ });
91+ }
11492 }
11593}
0 commit comments