1414final class AutowireDiscovery implements Discovery
1515{
1616 use HandlesDiscoveryCache;
17+ /** @var array<string, string> */
18+ private array $ trackedAutowireDefinitions = [];
1719
1820 public function __construct (
1921 private readonly Container $ container ,
@@ -39,25 +41,39 @@ private function discoverAsSingleton(ClassReflector $class): void
3941 {
4042 $ interfaces = $ class ->getReflection ()->getInterfaceNames ();
4143 foreach ($ interfaces as $ interface ) {
42- $ this ->container ->singleton ($ interface , fn (Container $ container ) => $ container ->get ($ class ->getName ()));
44+ // No need to track this autowire definition as it will be cached as a singleton
45+
46+ $ this ->container ->singleton (
47+ $ interface ,
48+ static fn (Container $ container ) => $ container ->get ($ class ->getName ())
49+ );
4350 }
4451 }
4552
4653 private function discoverAsDefinition (ClassReflector $ class ): void
4754 {
4855 $ interfaces = $ class ->getReflection ()->getInterfaceNames ();
4956 foreach ($ interfaces as $ interface ) {
50- $ this ->container ->register ($ interface , fn (Container $ container ) => $ container ->get ($ class ->getName ()));
57+ $ this ->trackedAutowireDefinitions [$ interface ] = $ class ->getName ();
58+
59+ $ this ->container ->register (
60+ $ interface ,
61+ static fn (Container $ container ) => $ container ->get ($ class ->getName ())
62+ );
5163 }
5264 }
5365
5466 public function createCachePayload (): string
5567 {
56- return serialize ($ this ->container -> getDefinitions () );
68+ return serialize ($ this ->trackedAutowireDefinitions );
5769 }
5870
5971 public function restoreCachePayload (Container $ container , string $ payload ): void
6072 {
61- $ this ->container ->setDefinitions (unserialize ($ payload ));
73+ $ this ->trackedAutowireDefinitions = unserialize ($ payload , ['allowed_classes ' => false ]);
74+
75+ foreach ($ this ->trackedAutowireDefinitions as $ interface => $ className ) {
76+ $ this ->container ->register ($ interface , static fn (Container $ container ) => $ container ->get ($ className ));
77+ }
6278 }
6379}
0 commit comments