From 17269e137d9f878d43946c99b8d8328fb20710d0 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 21 Mar 2013 17:34:16 +0100 Subject: [PATCH] [DependencyInjection] fixed management of scoped services with an invalid behavior set to null The optimization for references has been removed as it does not take scopes into account. --- .../Compiler/ResolveInvalidReferencesPass.php | 4 +--- .../Component/DependencyInjection/Container.php | 5 +++++ .../DependencyInjection/Tests/ContainerTest.php | 17 ++++++++++++++++- .../Tests/Fixtures/php/services9_compiled.php | 4 ++-- 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/ResolveInvalidReferencesPass.php b/src/Symfony/Component/DependencyInjection/Compiler/ResolveInvalidReferencesPass.php index 6fad9a2841..93d5806036 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/ResolveInvalidReferencesPass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/ResolveInvalidReferencesPass.php @@ -88,9 +88,7 @@ class ResolveInvalidReferencesPass implements CompilerPassInterface $exists = $this->container->has($id); // resolve invalid behavior - if ($exists && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) { - $arguments[$k] = new Reference($id, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $argument->isStrict()); - } elseif (!$exists && ContainerInterface::NULL_ON_INVALID_REFERENCE === $invalidBehavior) { + if (!$exists && ContainerInterface::NULL_ON_INVALID_REFERENCE === $invalidBehavior) { $arguments[$k] = null; } elseif (!$exists && ContainerInterface::IGNORE_ON_INVALID_REFERENCE === $invalidBehavior) { if ($inMethodCall) { diff --git a/src/Symfony/Component/DependencyInjection/Container.php b/src/Symfony/Component/DependencyInjection/Container.php index 34e9a32876..be72e8fc68 100644 --- a/src/Symfony/Component/DependencyInjection/Container.php +++ b/src/Symfony/Component/DependencyInjection/Container.php @@ -11,6 +11,7 @@ namespace Symfony\Component\DependencyInjection; +use Symfony\Component\DependencyInjection\Exception\InactiveScopeException; use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; use Symfony\Component\DependencyInjection\Exception\RuntimeException; use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; @@ -271,6 +272,10 @@ class Container implements IntrospectableContainerInterface unset($this->services[$id]); } + if ($e instanceof InactiveScopeException && self::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) { + return null; + } + throw $e; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php index 23c13c5094..726af13904 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php @@ -15,6 +15,7 @@ use Symfony\Component\DependencyInjection\Scope; use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; +use Symfony\Component\DependencyInjection\Exception\InactiveScopeException; class ContainerTest extends \PHPUnit_Framework_TestCase { @@ -98,7 +99,7 @@ class ContainerTest extends \PHPUnit_Framework_TestCase $this->assertEquals(array('service_container', 'foo', 'bar'), $sc->getServiceIds(), '->getServiceIds() returns all defined service ids'); $sc = new ProjectServiceContainer(); - $this->assertEquals(array('scoped', 'scoped_foo', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'service_container'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods'); + $this->assertEquals(array('scoped', 'scoped_foo', 'inactive', 'bar', 'foo_bar', 'foo.baz', 'circular', 'throw_exception', 'throws_exception_on_service_configuration', 'service_container'), $sc->getServiceIds(), '->getServiceIds() returns defined service ids by getXXXService() methods'); } /** @@ -179,6 +180,15 @@ class ContainerTest extends \PHPUnit_Framework_TestCase } } + /** + * @covers Symfony\Component\DependencyInjection\Container::get + */ + public function testGetReturnsNullOnInactiveScope() + { + $sc = new ProjectServiceContainer(); + $this->assertNull($sc->get('inactive', ContainerInterface::NULL_ON_INVALID_REFERENCE)); + } + /** * @covers Symfony\Component\DependencyInjection\Container::has */ @@ -476,6 +486,11 @@ class ProjectServiceContainer extends Container return $this->services['scoped_foo'] = $this->scopedServices['foo']['scoped_foo'] = new \stdClass(); } + protected function getInactiveService() + { + throw new InactiveScopeException('request', 'request'); + } + protected function getBarService() { return $this->__bar; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php index 0002845a46..2deea119f4 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php @@ -81,7 +81,7 @@ class ProjectServiceContainer extends Container { $this->services['depends_on_request'] = $instance = new \stdClass(); - $instance->setRequest($this->get('request')); + $instance->setRequest($this->get('request', ContainerInterface::NULL_ON_INVALID_REFERENCE)); return $instance; } @@ -220,7 +220,7 @@ class ProjectServiceContainer extends Container protected function synchronizeRequestService() { if ($this->initialized('depends_on_request')) { - $this->get('depends_on_request')->setRequest($this->get('request')); + $this->get('depends_on_request')->setRequest($this->get('request', ContainerInterface::NULL_ON_INVALID_REFERENCE)); } }