@@ -119,8 +119,26 @@ public function registerClasses(Definition $prototype, string $namespace, string
119
119
$ autoconfigureAttributes = new RegisterAutoconfigureAttributesPass ();
120
120
$ autoconfigureAttributes = $ autoconfigureAttributes ->accept ($ prototype ) ? $ autoconfigureAttributes : null ;
121
121
$ classes = $ this ->findClasses ($ namespace , $ resource , (array ) $ exclude , $ autoconfigureAttributes , $ source );
122
- // prepare for deep cloning
123
- $ serializedPrototype = serialize ($ prototype );
122
+
123
+ $ getPrototype = static fn () => clone $ prototype ;
124
+ $ serialized = serialize ($ prototype );
125
+
126
+ // avoid deep cloning if no definitions are nested
127
+ if (strpos ($ serialized , 'O:48:"Symfony\Component\DependencyInjection\Definition" ' , 55 )
128
+ || strpos ($ serialized , 'O:53:"Symfony\Component\DependencyInjection\ChildDefinition" ' , 55 )
129
+ ) {
130
+ // prepare for deep cloning
131
+ foreach (['Arguments ' , 'Properties ' , 'MethodCalls ' , 'Configurator ' , 'Factory ' , 'Bindings ' ] as $ key ) {
132
+ $ serialized = serialize ($ prototype ->{'get ' .$ key }());
133
+
134
+ if (strpos ($ serialized , 'O:48:"Symfony\Component\DependencyInjection\Definition" ' )
135
+ || strpos ($ serialized , 'O:53:"Symfony\Component\DependencyInjection\ChildDefinition" ' )
136
+ ) {
137
+ $ getPrototype = static fn () => $ getPrototype ()->{'set ' .$ key }(unserialize ($ serialized ));
138
+ }
139
+ }
140
+ }
141
+ unset($ serialized );
124
142
125
143
foreach ($ classes as $ class => $ errorMessage ) {
126
144
if (null === $ errorMessage && $ autoconfigureAttributes ) {
@@ -147,7 +165,7 @@ public function registerClasses(Definition $prototype, string $namespace, string
147
165
if (interface_exists ($ class , false )) {
148
166
$ this ->interfaces [] = $ class ;
149
167
} else {
150
- $ this ->setDefinition ($ class , $ definition = unserialize ( $ serializedPrototype ));
168
+ $ this ->setDefinition ($ class , $ definition = $ getPrototype ( ));
151
169
if (null !== $ errorMessage ) {
152
170
$ definition ->addError ($ errorMessage );
153
171
0 commit comments