@@ -46,6 +46,7 @@ class Container implements ResettableContainerInterface
4646 protected $ parameterBag ;
4747
4848 protected $ services = array ();
49+ protected $ fileMap = array ();
4950 protected $ methodMap = array ();
5051 protected $ aliases = array ();
5152 protected $ loading = array ();
@@ -150,7 +151,7 @@ public function set($id, $service)
150151 throw new InvalidArgumentException ('You cannot set service "service_container". ' );
151152 }
152153
153- if (isset ($ this ->methodMap [$ id ])) {
154+ if (isset ($ this ->fileMap [ $ id ]) || isset ( $ this -> methodMap [$ id ])) {
154155 throw new InvalidArgumentException (sprintf ('You cannot set the pre-defined service "%s". ' , $ id ));
155156 }
156157
@@ -186,19 +187,12 @@ public function has($id)
186187 return true ;
187188 }
188189
189- if (isset ($ this ->methodMap [$ id ])) {
190- return true ;
191- }
192-
193- return false ;
190+ return isset ($ this ->fileMap [$ id ]) || isset ($ this ->methodMap [$ id ]);
194191 }
195192
196193 /**
197194 * Gets a service.
198195 *
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- *
202196 * @param string $id The service identifier
203197 * @param int $invalidBehavior The behavior when the service does not exist
204198 *
@@ -228,32 +222,14 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
228222 throw new ServiceCircularReferenceException ($ id , array_keys ($ this ->loading ));
229223 }
230224
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-
253225 $ this ->loading [$ id ] = true ;
254226
255227 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+ }
257233 } catch (\Exception $ e ) {
258234 unset($ this ->services [$ id ]);
259235
@@ -262,7 +238,21 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
262238 unset($ this ->loading [$ id ]);
263239 }
264240
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+ }
266256 }
267257
268258 /**
@@ -300,7 +290,7 @@ public function reset()
300290 */
301291 public function getServiceIds ()
302292 {
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 )));
304294 }
305295
306296 /**
@@ -327,6 +317,16 @@ public static function underscore($id)
327317 return strtolower (preg_replace (array ('/([A-Z]+)([A-Z][a-z])/ ' , '/([a-z\d])([A-Z])/ ' ), array ('\\1_ \\2 ' , '\\1_ \\2 ' ), str_replace ('_ ' , '. ' , $ id )));
328318 }
329319
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+
330330 /**
331331 * Fetches a variable from the environment.
332332 *
0 commit comments