[DI] Deal with inlined non-shared services

This commit is contained in:
Nicolas Grekas 2017-06-01 08:27:51 +02:00
parent 7a9875c3cd
commit 996698d996
4 changed files with 12 additions and 8 deletions

View File

@ -58,7 +58,11 @@ class AutowireExceptionPass implements CompilerPassInterface
// was the service inlined? Of so, does its parent service exist? // was the service inlined? Of so, does its parent service exist?
if (isset($inlinedIds[$serviceId])) { if (isset($inlinedIds[$serviceId])) {
return $this->doesServiceExistInTheContainer($inlinedIds[$serviceId], $container, $inlinedIds); foreach ($inlinedIds[$serviceId] as $parentId) {
if ($this->doesServiceExistInTheContainer($parentId, $container, $inlinedIds)) {
return true;
}
}
} }
return false; return false;

View File

@ -36,7 +36,7 @@ class InlineServiceDefinitionsPass extends AbstractRecursivePass implements Repe
/** /**
* Returns an array of all services inlined by this pass. * Returns an array of all services inlined by this pass.
* *
* The key is the inlined service id and its value is the service it was inlined into. * The key is the inlined service id and its value is the list of services it was inlined into.
* *
* @return array * @return array
*/ */
@ -59,7 +59,7 @@ class InlineServiceDefinitionsPass extends AbstractRecursivePass implements Repe
if ($this->isInlineableDefinition($id, $definition, $this->container->getCompiler()->getServiceReferenceGraph())) { if ($this->isInlineableDefinition($id, $definition, $this->container->getCompiler()->getServiceReferenceGraph())) {
$this->container->log($this, sprintf('Inlined service "%s" to "%s".', $id, $this->currentId)); $this->container->log($this, sprintf('Inlined service "%s" to "%s".', $id, $this->currentId));
$this->inlinedServiceIds[$id] = $this->currentId; $this->inlinedServiceIds[$id][] = $this->currentId;
if ($definition->isShared()) { if ($definition->isShared()) {
return $definition; return $definition;

View File

@ -65,9 +65,9 @@ class AutowireExceptionPassTest extends TestCase
->method('getInlinedServiceIds') ->method('getInlinedServiceIds')
->will($this->returnValue(array( ->will($this->returnValue(array(
// a_service inlined into b_service // a_service inlined into b_service
'a_service' => 'b_service', 'a_service' => array('b_service'),
// b_service inlined into c_service // b_service inlined into c_service
'b_service' => 'c_service', 'b_service' => array('c_service'),
))); )));
$container = new ContainerBuilder(); $container = new ContainerBuilder();
@ -100,9 +100,9 @@ class AutowireExceptionPassTest extends TestCase
->method('getInlinedServiceIds') ->method('getInlinedServiceIds')
->will($this->returnValue(array( ->will($this->returnValue(array(
// a_service inlined into b_service // a_service inlined into b_service
'a_service' => 'b_service', 'a_service' => array('b_service'),
// b_service inlined into c_service // b_service inlined into c_service
'b_service' => 'c_service', 'b_service' => array('c_service'),
))); )));
// do NOT register c_service in the container // do NOT register c_service in the container

View File

@ -273,7 +273,7 @@ class InlineServiceDefinitionsPassTest extends TestCase
$repeatedPass = new RepeatedPass(array(new AnalyzeServiceReferencesPass(), $inlinePass)); $repeatedPass = new RepeatedPass(array(new AnalyzeServiceReferencesPass(), $inlinePass));
$repeatedPass->process($container); $repeatedPass->process($container);
$this->assertEquals(array('inlinable.service' => 'other_service'), $inlinePass->getInlinedServiceIds()); $this->assertEquals(array('inlinable.service' => array('other_service')), $inlinePass->getInlinedServiceIds());
} }
protected function process(ContainerBuilder $container) protected function process(ContainerBuilder $container)