@@ -76,6 +76,8 @@ class Container implements IntrospectableContainerInterface
76
76
protected $ scopeStacks = array ();
77
77
protected $ loading = array ();
78
78
79
+ private $ underscoreMap = array ('_ ' => '' , '. ' => '_ ' , '\\' => '_ ' );
80
+
79
81
/**
80
82
* Constructor.
81
83
*
@@ -211,7 +213,7 @@ public function set($id, $service, $scope = self::SCOPE_CONTAINER)
211
213
212
214
$ this ->services [$ id ] = $ service ;
213
215
214
- if (method_exists ($ this , $ method = 'synchronize ' .strtr ($ id , array ( ' _ ' => '' , ' . ' => ' _ ' , '\\' => ' _ ' ) ).'Service ' )) {
216
+ if (method_exists ($ this , $ method = 'synchronize ' .strtr ($ id , $ this -> underscoreMap ).'Service ' )) {
215
217
$ this ->$ method ();
216
218
}
217
219
@@ -235,17 +237,20 @@ public function set($id, $service, $scope = self::SCOPE_CONTAINER)
235
237
*/
236
238
public function has ($ id )
237
239
{
238
- $ id = strtolower ($ id );
239
-
240
- if ('service_container ' === $ id ) {
241
- return true ;
240
+ for ($ i = 2 ;;) {
241
+ if ('service_container ' === $ id
242
+ || isset ($ this ->aliases [$ id ])
243
+ || isset ($ this ->services [$ id ])
244
+ || array_key_exists ($ id , $ this ->services )
245
+ ) {
246
+ return true ;
247
+ }
248
+ if (--$ i && $ id !== $ lcId = strtolower ($ id )) {
249
+ $ id = $ lcId ;
250
+ } else {
251
+ return method_exists ($ this , 'get ' .strtr ($ id , $ this ->underscoreMap ).'Service ' );
252
+ }
242
253
}
243
-
244
- return isset ($ this ->services [$ id ])
245
- || array_key_exists ($ id , $ this ->services )
246
- || isset ($ this ->aliases [$ id ])
247
- || method_exists ($ this , 'get ' .strtr ($ id , array ('_ ' => '' , '. ' => '_ ' , '\\' => '_ ' )).'Service ' )
248
- ;
249
254
}
250
255
251
256
/**
@@ -273,10 +278,7 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
273
278
// available services. Service IDs are case insensitive, however since
274
279
// this method can be called thousands of times during a request, avoid
275
280
// calling strtolower() unless necessary.
276
- foreach (array (false , true ) as $ strtolower ) {
277
- if ($ strtolower ) {
278
- $ id = strtolower ($ id );
279
- }
281
+ for ($ i = 2 ;;) {
280
282
if ('service_container ' === $ id ) {
281
283
return $ this ;
282
284
}
@@ -287,57 +289,60 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
287
289
if (isset ($ this ->services [$ id ]) || array_key_exists ($ id , $ this ->services )) {
288
290
return $ this ->services [$ id ];
289
291
}
290
- }
291
292
292
- if (isset ($ this ->loading [$ id ])) {
293
- throw new ServiceCircularReferenceException ($ id , array_keys ($ this ->loading ));
294
- }
293
+ if (isset ($ this ->loading [$ id ])) {
294
+ throw new ServiceCircularReferenceException ($ id , array_keys ($ this ->loading ));
295
+ }
295
296
296
- if (isset ($ this ->methodMap [$ id ])) {
297
- $ method = $ this ->methodMap [$ id ];
298
- } elseif (method_exists ($ this , $ method = 'get ' .strtr ($ id , array ('_ ' => '' , '. ' => '_ ' , '\\' => '_ ' )).'Service ' )) {
299
- // $method is set to the right value, proceed
300
- } else {
301
- if (self ::EXCEPTION_ON_INVALID_REFERENCE === $ invalidBehavior ) {
302
- if (!$ id ) {
303
- throw new ServiceNotFoundException ($ id );
304
- }
297
+ if (isset ($ this ->methodMap [$ id ])) {
298
+ $ method = $ this ->methodMap [$ id ];
299
+ } elseif (--$ i && $ id !== $ lcId = strtolower ($ id )) {
300
+ $ id = $ lcId ;
301
+ continue ;
302
+ } elseif (method_exists ($ this , $ method = 'get ' .strtr ($ id , $ this ->underscoreMap ).'Service ' )) {
303
+ // $method is set to the right value, proceed
304
+ } else {
305
+ if (self ::EXCEPTION_ON_INVALID_REFERENCE === $ invalidBehavior ) {
306
+ if (!$ id ) {
307
+ throw new ServiceNotFoundException ($ id );
308
+ }
305
309
306
- $ alternatives = array ();
307
- foreach ($ this ->services as $ key => $ associatedService ) {
308
- $ lev = levenshtein ($ id , $ key );
309
- if ($ lev <= strlen ($ id ) / 3 || false !== strpos ($ key , $ id )) {
310
- $ alternatives [] = $ key ;
310
+ $ alternatives = array ();
311
+ foreach ($ this ->services as $ key => $ associatedService ) {
312
+ $ lev = levenshtein ($ id , $ key );
313
+ if ($ lev <= strlen ($ id ) / 3 || false !== strpos ($ key , $ id )) {
314
+ $ alternatives [] = $ key ;
315
+ }
311
316
}
317
+
318
+ throw new ServiceNotFoundException ($ id , null , null , $ alternatives );
312
319
}
313
320
314
- throw new ServiceNotFoundException ( $ id , null , null , $ alternatives ) ;
321
+ return ;
315
322
}
316
323
317
- return ;
318
- }
324
+ $ this ->loading [$ id ] = true ;
319
325
320
- $ this ->loading [$ id ] = true ;
326
+ try {
327
+ $ service = $ this ->$ method ();
328
+ } catch (\Exception $ e ) {
329
+ unset($ this ->loading [$ id ]);
321
330
322
- try {
323
- $ service = $ this ->$ method ();
324
- } catch (\Exception $ e ) {
325
- unset($ this ->loading [$ id ]);
331
+ if (array_key_exists ($ id , $ this ->services )) {
332
+ unset($ this ->services [$ id ]);
333
+ }
326
334
327
- if (array_key_exists ( $ id , $ this -> services ) ) {
328
- unset( $ this -> services [ $ id ]) ;
329
- }
335
+ if ($ e instanceof InactiveScopeException && self :: EXCEPTION_ON_INVALID_REFERENCE !== $ invalidBehavior ) {
336
+ return ;
337
+ }
330
338
331
- if ($ e instanceof InactiveScopeException && self ::EXCEPTION_ON_INVALID_REFERENCE !== $ invalidBehavior ) {
332
- return ;
339
+ throw $ e ;
333
340
}
334
341
335
- throw $ e ;
336
- }
337
-
338
- unset($ this ->loading [$ id ]);
342
+ unset($ this ->loading [$ id ]);
339
343
340
- return $ service ;
344
+ return $ service ;
345
+ }
341
346
}
342
347
343
348
/**
0 commit comments