99 * file that was distributed with this source code.
1010 */
1111
12- namespace Zenstruck \Foundry \Persistence \ Proxy ;
12+ namespace Zenstruck \Foundry \Persistence ;
1313
1414use Zenstruck \Foundry \Configuration ;
15+ use Zenstruck \Foundry \Persistence \Event \AfterPersist ;
1516
1617/**
1718 * @internal
@@ -23,48 +24,49 @@ final class PersistedObjectsTracker
2324 *
2425 * @var \WeakMap<object, mixed> keys: objects, values: value ids
2526 */
26- private static \WeakMap $ buffer ;
27+ private static \WeakMap $ trackedObjects ;
2728
2829 public function __construct ()
2930 {
30- self ::$ buffer ??= new \WeakMap ();
31+ self ::$ trackedObjects ??= new \WeakMap ();
3132 }
3233
3334 public function refresh (): void
3435 {
3536 self ::proxifyObjects ();
3637 }
3738
38- public function add (object ...$ objects ): void
39+ /**
40+ * @param AfterPersist<object> $event
41+ */
42+ public function afterPersistHook (AfterPersist $ event ): void
3943 {
40- foreach ($ objects as $ object ) {
41- if (self ::$ buffer ->offsetExists ($ object ) && self ::$ buffer [$ object ]) {
42- continue ;
43- }
44-
45- self ::$ buffer [$ object ] = Configuration::instance ()->persistence ()->getIdentifierValues ($ object );
44+ if ($ event ->factory instanceof PersistentProxyObjectFactory || !$ event ->factory ->autorefreshEnabled ()) {
45+ return ;
4646 }
47+
48+ $ this ->add ($ event ->object );
4749 }
4850
49- public static function updateIds ( ): void
51+ public function add ( object ... $ objects ): void
5052 {
51- foreach (self :: $ buffer as $ object => $ id ) {
52- if ($ id ) {
53+ foreach ($ objects as $ object ) {
54+ if (self :: $ trackedObjects -> offsetExists ( $ object ) && self :: $ trackedObjects [ $ object ] ) {
5355 continue ;
5456 }
5557
56- self ::$ buffer [$ object ] = Configuration::instance ()->persistence ()->getIdentifierValues ($ object );
58+ self ::$ trackedObjects [$ object ] = Configuration::instance ()->persistence ()->getIdentifierValues ($ object );
5759 }
5860 }
5961
6062 public static function reset (): void
6163 {
62- self ::$ buffer = new \WeakMap ();
64+ self ::$ trackedObjects = new \WeakMap ();
6365 }
6466
6567 public static function countObjects (): int
6668 {
67- return \count (self ::$ buffer );
69+ return \count (self ::$ trackedObjects );
6870 }
6971
7072 private static function proxifyObjects (): void
@@ -73,7 +75,7 @@ private static function proxifyObjects(): void
7375 return ;
7476 }
7577
76- foreach (self ::$ buffer as $ object => $ id ) {
78+ foreach (self ::$ trackedObjects as $ object => $ id ) {
7779 if (!$ id ) {
7880 continue ;
7981 }
0 commit comments