소스 검색

[DependencyInjection] added automatic detection for service circular references

Fabien Potencier 14 년 전
부모
커밋
af8ebeaabb
1개의 변경된 파일13개의 추가작업 그리고 1개의 파일을 삭제
  1. 13 1
      src/Symfony/Component/DependencyInjection/Container.php

+ 13 - 1
src/Symfony/Component/DependencyInjection/Container.php

@@ -183,14 +183,26 @@ class Container implements ContainerInterface
      */
     public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE)
     {
+        static $loading = array();
+
         $id = strtolower($id);
 
         if (isset($this->services[$id])) {
             return $this->services[$id];
         }
 
+        if (isset($loading[$id])) {
+            throw new \LogicException(sprintf('Circular reference detected for service "%s" (services currently loading: %s).', $id, implode(', ', array_keys($loading))));
+        }
+
         if (method_exists($this, $method = 'get'.strtr($id, array('_' => '', '.' => '_')).'Service')) {
-            return $this->$method();
+            $loading[$id] = true;
+
+            $service = $this->$method();
+
+            unset($loading[$id]);
+
+            return $service;
         }
 
         if (self::EXCEPTION_ON_INVALID_REFERENCE === $invalidBehavior) {