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