@@ -1162,7 +1162,8 @@ private function addNewInstance(Definition $definition, string $return = '', str
1162
1162
if ('current ' === $ callable && [0 ] === array_keys ($ definition ->getArguments ()) && \is_array ($ value ) && [0 ] === array_keys ($ value )) {
1163
1163
return $ return .$ this ->dumpValue ($ value [0 ]).$ tail ;
1164
1164
}
1165
- if (['Closure ' , 'fromCallable ' ] === $ callable && [0 ] === array_keys ($ definition ->getArguments ())) {
1165
+
1166
+ if (['Closure ' , 'fromCallable ' ] === $ callable ) {
1166
1167
$ callable = $ definition ->getArgument (0 );
1167
1168
if ($ callable instanceof ServiceClosureArgument) {
1168
1169
return $ return .$ this ->dumpValue ($ callable ).$ tail ;
@@ -1175,58 +1176,56 @@ private function addNewInstance(Definition $definition, string $return = '', str
1175
1176
}
1176
1177
}
1177
1178
1178
- if (\is_array ($ callable )) {
1179
- if (!preg_match ('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/ ' , $ callable [1 ])) {
1180
- throw new RuntimeException (sprintf ('Cannot dump definition because of invalid factory method (%s). ' , $ callable [1 ] ?: 'n/a ' ));
1181
- }
1182
-
1183
- if (['... ' ] === $ arguments && $ definition ->isLazy () && 'Closure ' === ($ definition ->getClass () ?? 'Closure ' ) && (
1184
- $ callable [0 ] instanceof Reference
1185
- || ($ callable [0 ] instanceof Definition && !$ this ->definitionVariables ->contains ($ callable [0 ]))
1186
- )) {
1187
- $ class = ($ callable [0 ] instanceof Reference ? $ this ->container ->findDefinition ($ callable [0 ]) : $ callable [0 ])->getClass ();
1179
+ if (\is_string ($ callable ) && str_starts_with ($ callable , '@= ' )) {
1180
+ return $ return .sprintf ('(($args = %s) ? (%s) : null) ' ,
1181
+ $ this ->dumpValue (new ServiceLocatorArgument ($ definition ->getArguments ())),
1182
+ $ this ->getExpressionLanguage ()->compile (substr ($ callable , 2 ), ['container ' => 'container ' , 'args ' => 'args ' ])
1183
+ ).$ tail ;
1184
+ }
1188
1185
1189
- if (str_contains ($ initializer = $ this ->dumpValue ($ callable [0 ]), '$container ' )) {
1190
- $ this ->addContainerRef = true ;
1191
- $ initializer = sprintf ('function () use ($containerRef) { $container = $containerRef; return %s; } ' , $ initializer );
1192
- } else {
1193
- $ initializer = 'fn () => ' .$ initializer ;
1194
- }
1186
+ if (!\is_array ($ callable )) {
1187
+ return $ return .sprintf ('%s(%s) ' , $ this ->dumpLiteralClass ($ this ->dumpValue ($ callable )), $ arguments ? implode (', ' , $ arguments ) : '' ).$ tail ;
1188
+ }
1195
1189
1196
- return $ return .LazyClosure::getCode ($ initializer , $ this ->container ->getReflectionClass ($ class ), $ callable [1 ], $ id ).$ tail ;
1197
- }
1190
+ if (!preg_match ('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/ ' , $ callable [1 ])) {
1191
+ throw new RuntimeException (sprintf ('Cannot dump definition because of invalid factory method (%s). ' , $ callable [1 ] ?: 'n/a ' ));
1192
+ }
1198
1193
1199
- if ($ callable [0 ] instanceof Reference
1200
- || ($ callable [0 ] instanceof Definition && $ this ->definitionVariables ->contains ($ callable [0 ]))
1201
- ) {
1202
- return $ return .sprintf ('%s->%s(%s) ' , $ this ->dumpValue ($ callable [0 ]), $ callable [1 ], $ arguments ? implode (', ' , $ arguments ) : '' ).$ tail ;
1194
+ if (['... ' ] === $ arguments && ($ definition ->isLazy () || 'Closure ' !== ($ definition ->getClass () ?? 'Closure ' )) && (
1195
+ $ callable [0 ] instanceof Reference
1196
+ || ($ callable [0 ] instanceof Definition && !$ this ->definitionVariables ->contains ($ callable [0 ]))
1197
+ )) {
1198
+ if (str_contains ($ initializer = $ this ->dumpValue ($ callable [0 ]), '$container ' )) {
1199
+ $ this ->addContainerRef = true ;
1200
+ $ initializer = sprintf ('function () use ($containerRef) { $container = $containerRef->get(); return %s; } ' , $ initializer );
1201
+ } else {
1202
+ $ initializer = 'fn () => ' .$ initializer ;
1203
1203
}
1204
1204
1205
- $ class = $ this ->dumpValue ($ callable [0 ]);
1206
- // If the class is a string we can optimize away
1207
- if (str_starts_with ($ class , "' " ) && !str_contains ($ class , '$ ' )) {
1208
- if ("'' " === $ class ) {
1209
- throw new RuntimeException (sprintf ('Cannot dump definition: "%s" service is defined to be created by a factory but is missing the service reference, did you forget to define the factory service id or class? ' , $ id ? 'The " ' .$ id .'" ' : 'inline ' ));
1210
- }
1205
+ return $ return .LazyClosure::getCode ($ initializer , $ callable , $ definition , $ this ->container , $ id ).$ tail ;
1206
+ }
1211
1207
1212
- return $ return .sprintf ('%s::%s(%s) ' , $ this ->dumpLiteralClass ($ class ), $ callable [1 ], $ arguments ? implode (', ' , $ arguments ) : '' ).$ tail ;
1213
- }
1208
+ if ($ callable [0 ] instanceof Reference
1209
+ || ($ callable [0 ] instanceof Definition && $ this ->definitionVariables ->contains ($ callable [0 ]))
1210
+ ) {
1211
+ return $ return .sprintf ('%s->%s(%s) ' , $ this ->dumpValue ($ callable [0 ]), $ callable [1 ], $ arguments ? implode (', ' , $ arguments ) : '' ).$ tail ;
1212
+ }
1214
1213
1215
- if (str_starts_with ($ class , 'new ' )) {
1216
- return $ return .sprintf ('(%s)->%s(%s) ' , $ class , $ callable [1 ], $ arguments ? implode (', ' , $ arguments ) : '' ).$ tail ;
1214
+ $ class = $ this ->dumpValue ($ callable [0 ]);
1215
+ // If the class is a string we can optimize away
1216
+ if (str_starts_with ($ class , "' " ) && !str_contains ($ class , '$ ' )) {
1217
+ if ("'' " === $ class ) {
1218
+ throw new RuntimeException (sprintf ('Cannot dump definition: "%s" service is defined to be created by a factory but is missing the service reference, did you forget to define the factory service id or class? ' , $ id ? 'The " ' .$ id .'" ' : 'inline ' ));
1217
1219
}
1218
1220
1219
- return $ return .sprintf (" [%s, '%s'] (%s)" , $ class , $ callable [1 ], $ arguments ? implode (', ' , $ arguments ) : '' ).$ tail ;
1221
+ return $ return .sprintf (' %s::%s (%s)' , $ this -> dumpLiteralClass ( $ class) , $ callable [1 ], $ arguments ? implode (', ' , $ arguments ) : '' ).$ tail ;
1220
1222
}
1221
1223
1222
- if (\is_string ($ callable ) && str_starts_with ($ callable , '@= ' )) {
1223
- return $ return .sprintf ('(($args = %s) ? (%s) : null) ' ,
1224
- $ this ->dumpValue (new ServiceLocatorArgument ($ definition ->getArguments ())),
1225
- $ this ->getExpressionLanguage ()->compile (substr ($ callable , 2 ), ['container ' => 'container ' , 'args ' => 'args ' ])
1226
- ).$ tail ;
1224
+ if (str_starts_with ($ class , 'new ' )) {
1225
+ return $ return .sprintf ('(%s)->%s(%s) ' , $ class , $ callable [1 ], $ arguments ? implode (', ' , $ arguments ) : '' ).$ tail ;
1227
1226
}
1228
1227
1229
- return $ return .sprintf ('%s(%s) ' , $ this -> dumpLiteralClass ( $ this -> dumpValue ( $ callable)) , $ arguments ? implode (', ' , $ arguments ) : '' ).$ tail ;
1228
+ return $ return .sprintf (" [%s, '%s'] (%s)" , $ class , $ callable[ 1 ] , $ arguments ? implode (', ' , $ arguments ) : '' ).$ tail ;
1230
1229
}
1231
1230
1232
1231
if (null === $ class = $ definition ->getClass ()) {
@@ -2344,7 +2343,7 @@ private function isProxyCandidate(Definition $definition, ?bool &$asGhostObject,
2344
2343
{
2345
2344
$ asGhostObject = false ;
2346
2345
2347
- if (' Closure ' === ( $ definition -> getClass () ?: ( ['Closure ' , 'fromCallable ' ] === $ definition ->getFactory () ? ' Closure ' : null ) )) {
2346
+ if (['Closure ' , 'fromCallable ' ] === $ definition ->getFactory ()) {
2348
2347
return null ;
2349
2348
}
2350
2349
0 commit comments