Bläddra i källkod

[DependencyInjection/Compiler] fixes a bug which silently changed the scope of services

Johannes M. Schmitt 14 år sedan
förälder
incheckning
67c886f3df

+ 9 - 5
src/Symfony/Component/DependencyInjection/Compiler/InlineServiceDefinitionsPass.php

@@ -37,7 +37,7 @@ class InlineServiceDefinitionsPass implements RepeatablePassInterface
     /**
      * Processes the ContainerBuilder for inline service definitions.
      *
-     * @param ContainerBuilder $container 
+     * @param ContainerBuilder $container
      */
     public function process(ContainerBuilder $container)
     {
@@ -89,9 +89,9 @@ class InlineServiceDefinitionsPass implements RepeatablePassInterface
     /**
      * Checks if the definition is inlineable.
      *
-     * @param ContainerBuilder $container 
-     * @param string $id 
-     * @param Definition $definition 
+     * @param ContainerBuilder $container
+     * @param string $id
+     * @param Definition $definition
      * @return boolean If the definition is inlineable
      */
     protected function isInlinableDefinition(ContainerBuilder $container, $id, Definition $definition)
@@ -113,6 +113,10 @@ class InlineServiceDefinitionsPass implements RepeatablePassInterface
             $ids[] = $edge->getSourceNode()->getId();
         }
 
-        return count(array_unique($ids)) <= 1;
+        if (count(array_unique($ids)) > 1) {
+            return false;
+        }
+
+        return $container->getDefinition(reset($ids))->getScope() === $definition->getScope();
     }
 }

+ 16 - 0
tests/Symfony/Tests/Component/DependencyInjection/Compiler/InlineServiceDefinitionsPassTest.php

@@ -11,6 +11,8 @@
 
 namespace Symfony\Tests\Component\DependencyInjection\Compiler;
 
+use Symfony\Component\DependencyInjection\Scope;
+
 use Symfony\Component\DependencyInjection\Definition;
 use Symfony\Component\DependencyInjection\Compiler\AnalyzeServiceReferencesPass;
 use Symfony\Component\DependencyInjection\Compiler\Compiler;
@@ -110,6 +112,20 @@ class InlineServiceDefinitionsPassTest extends \PHPUnit_Framework_TestCase
         $this->assertSame($a, $inlinedArguments[0]);
     }
 
+    public function testProcessInlinesOnlyIfSameScope()
+    {
+        $container = new ContainerBuilder();
+
+        $container->addScope(new Scope('foo'));
+        $a = $container->register('a')->setPublic(false)->setScope('foo');
+        $b = $container->register('b')->addArgument(new Reference('a'));
+
+        $this->process($container);
+        $arguments = $b->getArguments();
+        $this->assertEquals(new Reference('a'), $arguments[0]);
+        $this->assertTrue($container->hasDefinition('a'));
+    }
+
     protected function process(ContainerBuilder $container)
     {
         $repeatedPass = new RepeatedPass(array(new AnalyzeServiceReferencesPass(), new InlineServiceDefinitionsPass()));