Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 17269e1

Browse files
committed
[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.
1 parent bb83b3e commit 17269e1

File tree

4 files changed

+24
-6
lines changed

4 files changed

+24
-6
lines changed

src/Symfony/Component/DependencyInjection/Compiler/ResolveInvalidReferencesPass.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ private function processArguments(array $arguments, $inMethodCall = false)
8888
$exists = $this->container->has($id);
8989

9090
// resolve invalid behavior
91-
if ($exists && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
92-
$arguments[$k] = new Reference($id, ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, $argument->isStrict());
93-
} elseif (!$exists && ContainerInterface::NULL_ON_INVALID_REFERENCE === $invalidBehavior) {
91+
if (!$exists && ContainerInterface::NULL_ON_INVALID_REFERENCE === $invalidBehavior) {
9492
$arguments[$k] = null;
9593
} elseif (!$exists && ContainerInterface::IGNORE_ON_INVALID_REFERENCE === $invalidBehavior) {
9694
if ($inMethodCall) {

src/Symfony/Component/DependencyInjection/Container.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\DependencyInjection;
1313

14+
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
1415
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1516
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1617
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
@@ -271,6 +272,10 @@ public function get($id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE
271272
unset($this->services[$id]);
272273
}
273274

275+
if ($e instanceof InactiveScopeException && self::EXCEPTION_ON_INVALID_REFERENCE !== $invalidBehavior) {
276+
return null;
277+
}
278+
274279
throw $e;
275280
}
276281

src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\DependencyInjection\Container;
1616
use Symfony\Component\DependencyInjection\ContainerInterface;
1717
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
18+
use Symfony\Component\DependencyInjection\Exception\InactiveScopeException;
1819

1920
class ContainerTest extends \PHPUnit_Framework_TestCase
2021
{
@@ -98,7 +99,7 @@ public function testGetServiceIds()
9899
$this->assertEquals(array('service_container', 'foo', 'bar'), $sc->getServiceIds(), '->getServiceIds() returns all defined service ids');
99100

100101
$sc = new ProjectServiceContainer();
101-
$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');
102+
$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');
102103
}
103104

104105
/**
@@ -179,6 +180,15 @@ public function testGetCircularReference()
179180
}
180181
}
181182

183+
/**
184+
* @covers Symfony\Component\DependencyInjection\Container::get
185+
*/
186+
public function testGetReturnsNullOnInactiveScope()
187+
{
188+
$sc = new ProjectServiceContainer();
189+
$this->assertNull($sc->get('inactive', ContainerInterface::NULL_ON_INVALID_REFERENCE));
190+
}
191+
182192
/**
183193
* @covers Symfony\Component\DependencyInjection\Container::has
184194
*/
@@ -476,6 +486,11 @@ protected function getScopedFooService()
476486
return $this->services['scoped_foo'] = $this->scopedServices['foo']['scoped_foo'] = new \stdClass();
477487
}
478488

489+
protected function getInactiveService()
490+
{
491+
throw new InactiveScopeException('request', 'request');
492+
}
493+
479494
protected function getBarService()
480495
{
481496
return $this->__bar;

src/Symfony/Component/DependencyInjection/Tests/Fixtures/php/services9_compiled.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected function getDependsOnRequestService()
8181
{
8282
$this->services['depends_on_request'] = $instance = new \stdClass();
8383

84-
$instance->setRequest($this->get('request'));
84+
$instance->setRequest($this->get('request', ContainerInterface::NULL_ON_INVALID_REFERENCE));
8585

8686
return $instance;
8787
}
@@ -220,7 +220,7 @@ protected function getAliasForFooService()
220220
protected function synchronizeRequestService()
221221
{
222222
if ($this->initialized('depends_on_request')) {
223-
$this->get('depends_on_request')->setRequest($this->get('request'));
223+
$this->get('depends_on_request')->setRequest($this->get('request', ContainerInterface::NULL_ON_INVALID_REFERENCE));
224224
}
225225
}
226226

0 commit comments

Comments
 (0)