Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[DependencyInjection] fixed management of scoped services with an inv…

…alid behavior set to null

The optimization for references has been removed as it does not take
scopes into account.
  • Loading branch information...
commit 17269e137d9f878d43946c99b8d8328fb20710d0 1 parent bb83b3e
Fabien Potencier fabpot authored
4 src/Symfony/Component/DependencyInjection/Compiler/ResolveInvalidReferencesPass.php
View
@@ -88,9 +88,7 @@ private function processArguments(array $arguments, $inMethodCall = false)
$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) {
5 src/Symfony/Component/DependencyInjection/Container.php
View
@@ -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 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
unset($this->services[$id]);
}
+ if ($e instanceof InactiveScopeException && self::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
+ return null;
+ }
+
throw $e;
}
17 src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
View
@@ -15,6 +15,7 @@
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 @@ public function testGetServiceIds()
$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');
}
/**
@@ -180,6 +181,15 @@ public function testGetCircularReference()
}
/**
+ * @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
*/
public function testHas()
@@ -476,6 +486,11 @@ protected function getScopedFooService()
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;
4 src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php
View
@@ -81,7 +81,7 @@ protected function getDependsOnRequestService()
{
$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 @@ protected function getAliasForFooService()
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));
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.