Sfoglia il codice sorgente

[DependencyInjection] minor interface change

Johannes M. Schmitt 14 anni fa
parent
commit
d4d2d60f7b

+ 2 - 1
src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

@@ -23,6 +23,7 @@ use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\TranslatorPass;
 use Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\AddCacheWarmerPass;
 use Symfony\Component\DependencyInjection\ContainerBuilder;
 use Symfony\Component\DependencyInjection\Compiler\PassConfig;
+use Symfony\Component\DependencyInjection\Scope;
 use Symfony\Component\HttpFoundation\File\File;
 use Symfony\Component\HttpKernel\Bundle\Bundle;
 use Symfony\Component\ClassLoader\ClassCollectionLoader;
@@ -67,7 +68,7 @@ class FrameworkBundle extends Bundle
     {
         parent::registerExtensions($container);
 
-        $container->addScope('request');
+        $container->addScope(new Scope('request'));
 
         $container->addCompilerPass(new RoutingResolverPass());
         $container->addCompilerPass(new ProfilerPass());

+ 5 - 3
src/Symfony/Component/DependencyInjection/Container.php

@@ -334,12 +334,14 @@ class Container implements ContainerInterface
     /**
      * Adds a scope to the container
      *
-     * @param string $name
-     * @param string $parentScope
+     * @param ScopeInterface $scope
      * @return void
      */
-    public function addScope($name, $parentScope = self::SCOPE_CONTAINER)
+    public function addScope(ScopeInterface $scope)
     {
+        $name = $scope->getName();
+        $parentScope = $scope->getParentName();
+
         if (self::SCOPE_CONTAINER === $name || self::SCOPE_PROTOTYPE === $name) {
             throw new \InvalidArgumentException(sprintf('The scope "%s" is reserved.', $name));
         }

+ 2 - 3
src/Symfony/Component/DependencyInjection/ContainerInterface.php

@@ -104,11 +104,10 @@ interface ContainerInterface
     /**
      * Adds a scope to the container
      *
-     * @param string $name
-     * @param string $parentScope
+     * @param ScopeInterface $scope
      * @return void
      */
-    function addScope($name, $parentScope = self::SCOPE_CONTAINER);
+    function addScope(ScopeInterface $scope);
 
     /**
      * Whether this container has the given scope

+ 30 - 0
src/Symfony/Component/DependencyInjection/Scope.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace Symfony\Component\DependencyInjection;
+
+/**
+ * Scope class.
+ *
+ * @author Johannes M. Schmitt <schmittjoh@gmail.com>
+ */
+class Scope implements ScopeInterface
+{
+    protected $name;
+    protected $parentName;
+
+    public function __construct($name, $parentName = ContainerInterface::SCOPE_CONTAINER)
+    {
+        $this->name = $name;
+        $this->parentName = $parentName;
+    }
+
+    public function getName()
+    {
+        return $this->name;
+    }
+
+    public function getParentName()
+    {
+        return $this->parentName;
+    }
+}

+ 14 - 0
src/Symfony/Component/DependencyInjection/ScopeInterface.php

@@ -0,0 +1,14 @@
+<?php
+
+namespace Symfony\Component\DependencyInjection;
+
+/**
+ * Scope Interface.
+ *
+ * @author Johannes M. Schmitt <schmittjoh@gmail.com>
+ */
+interface ScopeInterface
+{
+    function getName();
+    function getParentName();
+}

+ 6 - 4
tests/Symfony/Tests/Component/DependencyInjection/Compiler/CheckReferenceValidityPassTest.php

@@ -2,6 +2,8 @@
 
 namespace Symfony\Tests\Component\DependencyInjection\Compiler;
 
+use Symfony\Component\DependencyInjection\Scope;
+
 use Symfony\Component\DependencyInjection\Compiler\CheckReferenceValidityPass;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\DependencyInjection\Reference;
@@ -33,8 +35,8 @@ class CheckReferenceValidityPassTest extends \PHPUnit_Framework_TestCase
     public function testProcessIgnoresCrossScopeHierarchyReferenceIfNotStrict()
     {
         $container = new ContainerBuilder();
-        $container->addScope('a');
-        $container->addScope('b');
+        $container->addScope(new Scope('a'));
+        $container->addScope(new Scope('b'));
 
         $container->register('a')->setScope('a')->addArgument(new Reference('b', ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, false));
         $container->register('b')->setScope('b');
@@ -48,8 +50,8 @@ class CheckReferenceValidityPassTest extends \PHPUnit_Framework_TestCase
     public function testProcessDetectsCrossScopeHierarchyReference()
     {
         $container = new ContainerBuilder();
-        $container->addScope('a');
-        $container->addScope('b');
+        $container->addScope(new Scope('a'));
+        $container->addScope(new Scope('b'));
 
         $container->register('a')->setScope('a')->addArgument(new Reference('b'));
         $container->register('b')->setScope('b');

+ 14 - 13
tests/Symfony/Tests/Component/DependencyInjection/ContainerTest.php

@@ -11,6 +11,7 @@
 
 namespace Symfony\Tests\Component\DependencyInjection;
 
+use Symfony\Component\DependencyInjection\Scope;
 use Symfony\Component\DependencyInjection\Container;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
@@ -125,14 +126,14 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
     public function testSetDoesNotAllowInactiveScope()
     {
         $c = new Container();
-        $c->addScope('foo');
+        $c->addScope(new Scope('foo'));
         $c->set('foo', new \stdClass(), 'foo');
     }
 
     public function testSetAlsoSetsScopedService()
     {
         $c = new Container();
-        $c->addScope('foo');
+        $c->addScope(new Scope('foo'));
         $c->enterScope('foo');
         $c->set('foo', $foo = new \stdClass(), 'foo');
 
@@ -183,7 +184,7 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
     public function testEnterLeaveCurrentScope()
     {
         $container = new ProjectServiceContainer();
-        $container->addScope('foo');
+        $container->addScope(new Scope('foo'));
 
         $container->enterScope('foo');
         $scoped1 = $container->get('scoped');
@@ -208,8 +209,8 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
     public function testEnterLeaveScopeWithChildScopes()
     {
         $container = new Container();
-        $container->addScope('foo');
-        $container->addScope('bar', 'foo');
+        $container->addScope(new Scope('foo'));
+        $container->addScope(new Scope('bar', 'foo'));
 
         $this->assertFalse($container->isScopeActive('foo'));
 
@@ -243,7 +244,7 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
     public function testAddScopeDoesNotAllowBuiltInScopes($scope)
     {
         $container = new Container();
-        $container->addScope($scope);
+        $container->addScope(new Scope($scope));
     }
 
     /**
@@ -252,8 +253,8 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
     public function testAddScopeDoesNotAllowExistingScope()
     {
         $container = new Container();
-        $container->addScope('foo');
-        $container->addScope('foo');
+        $container->addScope(new Scope('foo'));
+        $container->addScope(new Scope('foo'));
     }
 
     /**
@@ -263,14 +264,14 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
     public function testAddScopeDoesNotAllowInvalidParentScope($scope)
     {
         $c = new Container();
-        $c->addScope('foo', $scope);
+        $c->addScope(new Scope('foo', $scope));
     }
 
     public function testAddScope()
     {
         $c = new Container();
-        $c->addScope('foo');
-        $c->addScope('bar', 'foo');
+        $c->addScope(new Scope('foo'));
+        $c->addScope(new Scope('bar', 'foo'));
 
         $this->assertSame(array('foo' => 'container', 'bar' => 'foo'), $this->getField($c, 'scopes'));
         $this->assertSame(array('foo' => array('bar'), 'bar' => array()), $this->getField($c, 'scopeChildren'));
@@ -281,7 +282,7 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
         $c = new Container();
 
         $this->assertFalse($c->hasScope('foo'));
-        $c->addScope('foo');
+        $c->addScope(new Scope('foo'));
         $this->assertTrue($c->hasScope('foo'));
     }
 
@@ -290,7 +291,7 @@ class ContainerTest extends \PHPUnit_Framework_TestCase
         $c = new Container();
 
         $this->assertFalse($c->isScopeActive('foo'));
-        $c->addScope('foo');
+        $c->addScope(new Scope('foo'));
 
         $this->assertFalse($c->isScopeActive('foo'));
         $c->enterScope('foo');