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