Skip to content

Commit

Permalink
[DependencyInjection] minor interface change
Browse files Browse the repository at this point in the history
  • Loading branch information
schmittjoh authored and fabpot committed Feb 13, 2011
1 parent 205621d commit d4d2d60
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -67,7 +68,7 @@ public function registerExtensions(ContainerBuilder $container)
{
parent::registerExtensions($container);

$container->addScope('request');
$container->addScope(new Scope('request'));

$container->addCompilerPass(new RoutingResolverPass());
$container->addCompilerPass(new ProfilerPass());
Expand Down
8 changes: 5 additions & 3 deletions src/Symfony/Component/DependencyInjection/Container.php
Expand Up @@ -334,12 +334,14 @@ public function leaveScope($name)
/**
* 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));
}
Expand Down
Expand Up @@ -104,11 +104,10 @@ function leaveScope($name);
/**
* 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
Expand Down
30 changes: 30 additions & 0 deletions 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 changes: 14 additions & 0 deletions 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();
}
Expand Up @@ -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;
Expand Down Expand Up @@ -33,8 +35,8 @@ public function testProcessDetectsScopeWidening()
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');
Expand All @@ -48,8 +50,8 @@ public function testProcessIgnoresCrossScopeHierarchyReferenceIfNotStrict()
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');
Expand Down
27 changes: 14 additions & 13 deletions tests/Symfony/Tests/Component/DependencyInjection/ContainerTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -125,14 +126,14 @@ public function testSetDoesNotAllowPrototypeScope()
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');

Expand Down Expand Up @@ -183,7 +184,7 @@ public function testHas()
public function testEnterLeaveCurrentScope()
{
$container = new ProjectServiceContainer();
$container->addScope('foo');
$container->addScope(new Scope('foo'));

$container->enterScope('foo');
$scoped1 = $container->get('scoped');
Expand All @@ -208,8 +209,8 @@ public function testEnterLeaveCurrentScope()
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'));

Expand Down Expand Up @@ -243,7 +244,7 @@ public function testEnterLeaveScopeWithChildScopes()
public function testAddScopeDoesNotAllowBuiltInScopes($scope)
{
$container = new Container();
$container->addScope($scope);
$container->addScope(new Scope($scope));
}

/**
Expand All @@ -252,8 +253,8 @@ public function testAddScopeDoesNotAllowBuiltInScopes($scope)
public function testAddScopeDoesNotAllowExistingScope()
{
$container = new Container();
$container->addScope('foo');
$container->addScope('foo');
$container->addScope(new Scope('foo'));
$container->addScope(new Scope('foo'));
}

/**
Expand All @@ -263,14 +264,14 @@ public function testAddScopeDoesNotAllowExistingScope()
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'));
Expand All @@ -281,7 +282,7 @@ public function testHasScope()
$c = new Container();

$this->assertFalse($c->hasScope('foo'));
$c->addScope('foo');
$c->addScope(new Scope('foo'));
$this->assertTrue($c->hasScope('foo'));
}

Expand All @@ -290,7 +291,7 @@ public function testIsScopeActive()
$c = new Container();

$this->assertFalse($c->isScopeActive('foo'));
$c->addScope('foo');
$c->addScope(new Scope('foo'));

$this->assertFalse($c->isScopeActive('foo'));
$c->enterScope('foo');
Expand Down

0 comments on commit d4d2d60

Please sign in to comment.