@@ -46,6 +46,7 @@ class Container implements ResettableContainerInterface
46
46
protected $ parameterBag ;
47
47
48
48
protected $ services = array ();
49
+ protected $ fileMap = array ();
49
50
protected $ methodMap = array ();
50
51
protected $ aliases = array ();
51
52
protected $ loading = array ();
@@ -150,7 +151,7 @@ public function set($id, $service)
150
151
throw new InvalidArgumentException ('You cannot set service "service_container". ' );
151
152
}
152
153
153
- if (isset ($ this ->methodMap [$ id ])) {
154
+ if (isset ($ this ->fileMap [ $ id ]) || isset ( $ this -> methodMap [$ id ])) {
154
155
throw new InvalidArgumentException (sprintf ('You cannot set the pre-defined service "%s". ' , $ id ));
155
156
}
156
157
@@ -186,19 +187,12 @@ public function has($id)
186
187
return true ;
187
188
}
188
189
189
- if (isset ($ this ->methodMap [$ id ])) {
190
- return true ;
191
- }
192
-
193
- return false ;
190
+ return isset ($ this ->fileMap [$ id ]) || isset ($ this ->methodMap [$ id ]);
194
191
}
195
192
196
193
/**
197
194
* Gets a service.
198
195
*
199
- * If a service is defined both through a set() method and
200
- * with a get{$id}Service() method, the former has always precedence.
201
- *
202
196
* @param string $id The service identifier
203
197
* @param int $invalidBehavior The behavior when the service does not exist
204
198
*
@@ -228,32 +222,14 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
228
222
throw new ServiceCircularReferenceException ($ id , array_keys ($ this ->loading ));
229
223
}
230
224
231
- if (isset ($ this ->methodMap [$ id ])) {
232
- $ method = $ this ->methodMap [$ id ];
233
- } else {
234
- if (self ::EXCEPTION_ON_INVALID_REFERENCE === $ invalidBehavior ) {
235
- if (!$ id ) {
236
- throw new ServiceNotFoundException ($ id );
237
- }
238
-
239
- $ alternatives = array ();
240
- foreach ($ this ->getServiceIds () as $ knownId ) {
241
- $ lev = levenshtein ($ id , $ knownId );
242
- if ($ lev <= strlen ($ id ) / 3 || false !== strpos ($ knownId , $ id )) {
243
- $ alternatives [] = $ knownId ;
244
- }
245
- }
246
-
247
- throw new ServiceNotFoundException ($ id , null , null , $ alternatives );
248
- }
249
-
250
- return ;
251
- }
252
-
253
225
$ this ->loading [$ id ] = true ;
254
226
255
227
try {
256
- $ service = $ this ->$ method ();
228
+ if (isset ($ this ->fileMap [$ id ])) {
229
+ return $ this ->requireInScope ($ this ->fileMap [$ id ]);
230
+ } elseif (isset ($ this ->methodMap [$ id ])) {
231
+ return $ this ->{$ this ->methodMap [$ id ]}();
232
+ }
257
233
} catch (\Exception $ e ) {
258
234
unset($ this ->services [$ id ]);
259
235
@@ -262,7 +238,21 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
262
238
unset($ this ->loading [$ id ]);
263
239
}
264
240
265
- return $ service ;
241
+ if (self ::EXCEPTION_ON_INVALID_REFERENCE === $ invalidBehavior ) {
242
+ if (!$ id ) {
243
+ throw new ServiceNotFoundException ($ id );
244
+ }
245
+
246
+ $ alternatives = array ();
247
+ foreach ($ this ->getServiceIds () as $ knownId ) {
248
+ $ lev = levenshtein ($ id , $ knownId );
249
+ if ($ lev <= strlen ($ id ) / 3 || false !== strpos ($ knownId , $ id )) {
250
+ $ alternatives [] = $ knownId ;
251
+ }
252
+ }
253
+
254
+ throw new ServiceNotFoundException ($ id , null , null , $ alternatives );
255
+ }
266
256
}
267
257
268
258
/**
@@ -300,7 +290,7 @@ public function reset()
300
290
*/
301
291
public function getServiceIds ()
302
292
{
303
- return array_unique (array_merge (array ('service_container ' ), array_keys ($ this ->methodMap ), array_keys ($ this ->services )));
293
+ return array_unique (array_merge (array ('service_container ' ), array_keys ($ this ->fileMap ), array_keys ( $ this -> methodMap ), array_keys ($ this ->services )));
304
294
}
305
295
306
296
/**
@@ -327,6 +317,16 @@ public static function underscore($id)
327
317
return strtolower (preg_replace (array ('/([A-Z]+)([A-Z][a-z])/ ' , '/([a-z\d])([A-Z])/ ' ), array ('\\1_ \\2 ' , '\\1_ \\2 ' ), str_replace ('_ ' , '. ' , $ id )));
328
318
}
329
319
320
+ /**
321
+ * Creates a service by requiring its factory file.
322
+ *
323
+ * @return object The service created by the file
324
+ */
325
+ protected function requireInScope ($ file )
326
+ {
327
+ return require $ file ;
328
+ }
329
+
330
330
/**
331
331
* Fetches a variable from the environment.
332
332
*
0 commit comments