Permalink
Browse files

bug #27364 [DI] Fix bad exception on uninitialized references to non-…

…shared services (nicolas-grekas)

This PR was merged into the 3.4 branch.

Discussion
----------

[DI] Fix bad exception on uninitialized references to non-shared services

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #27360
| License       | MIT
| Doc PR        | -

This restriction is unneeded complexity that prevents legit use cases (see linked issue #27360).

Commits
-------

8bba68f [DI] Fix bad exception on uninitialized references to non-shared services
  • Loading branch information...
nicolas-grekas committed May 25, 2018
2 parents 531fcac + 8bba68f commit 46c2d4b3b01eb1d23f54adb3cb5c5f2704a917dd
@@ -11,7 +11,6 @@
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Reference;
@@ -31,9 +30,6 @@ protected function processValue($value, $isRoot = false)
if (ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE === $value->getInvalidBehavior() && !$this->container->has($id = (string) $value)) {
throw new ServiceNotFoundException($id, $this->currentId);
}
if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $value->getInvalidBehavior() && $this->container->has($id = (string) $value) && !$this->container->findDefinition($id)->isShared()) {
throw new InvalidArgumentException(sprintf('Invalid ignore-on-uninitialized reference found in service "%s": target service "%s" is not shared.', $this->currentId, $id));
}
return $value;
}
@@ -1896,6 +1896,9 @@ private function getServiceCall($id, Reference $reference = null)
if ($this->container->hasDefinition($id) && ($definition = $this->container->getDefinition($id)) && !$definition->isSynthetic()) {
if (null !== $reference && ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $reference->getInvalidBehavior()) {
$code = 'null';
if (!$definition->isShared()) {
return $code;
}
} elseif ($this->isTrivialInstance($definition)) {
$code = substr($this->addNewInstance($definition, '', '', $id), 8, -2);
if ($definition->isShared()) {
@@ -68,27 +68,6 @@ public function testProcessThrowsExceptionOnInvalidReferenceFromInlinedDefinitio
$this->process($container);
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Invalid ignore-on-uninitialized reference found in service
*/
public function testProcessThrowsExceptionOnNonSharedUninitializedReference()
{
$container = new ContainerBuilder();
$container
->register('a', 'stdClass')
->addArgument(new Reference('b', $container::IGNORE_ON_UNINITIALIZED_REFERENCE))
;
$container
->register('b', 'stdClass')
->setShared(false)
;
$this->process($container);
}
public function testProcessDefinitionWithBindings()
{
$container = new ContainerBuilder();

0 comments on commit 46c2d4b

Please sign in to comment.