Skip to content

[DI] Allow auto-configured method calls. #24996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ public function process(ContainerBuilder $container)
if ($definition->getArguments()) {
throw new InvalidArgumentException(sprintf('Autoconfigured instanceof for type "%s" defines arguments but these are not supported and should be removed.', $interface));
}
if ($definition->getMethodCalls()) {
throw new InvalidArgumentException(sprintf('Autoconfigured instanceof for type "%s" defines method calls but these are not supported and should be removed.', $interface));
}
}

foreach ($container->getDefinitions() as $id => $definition) {
Expand Down Expand Up @@ -64,6 +61,7 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
$definition->setInstanceofConditionals(array());
$parent = $shared = null;
$instanceofTags = array();
$instanceofCalls = array();

foreach ($conditionals as $interface => $instanceofDefs) {
if ($interface !== $class && (!$container->getReflectionClass($class, false))) {
Expand All @@ -81,7 +79,9 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
$parent = 'instanceof.'.$interface.'.'.$key.'.'.$id;
$container->setDefinition($parent, $instanceofDef);
$instanceofTags[] = $instanceofDef->getTags();
$instanceofCalls[] = $instanceofDef->getMethodCalls();
$instanceofDef->setTags(array());
$instanceofDef->setMethodCalls(array());

if (isset($instanceofDef->getChanges()['shared'])) {
$shared = $instanceofDef->isShared();
Expand All @@ -98,6 +98,7 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
$definition = serialize($definition);
$definition = substr_replace($definition, '53', 2, 2);
$definition = substr_replace($definition, 'Child', 44, 0);
/** @var ChildDefinition $definition */
$definition = unserialize($definition);
$definition->setParent($parent);

Expand All @@ -117,6 +118,10 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
}
}

$instanceofCalls = array_filter($instanceofCalls);
$instanceofCalls = array_map('current', $instanceofCalls);
$definition->setMethodCalls(array_merge($instanceofCalls, $definition->getMethodCalls()));

// reset fields with "merge" behavior
$abstract
->setBindings($bindings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,22 @@ public function testBadInterfaceForAutomaticInstanceofIsOk()
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
* @expectedExceptionMessage Autoconfigured instanceof for type "PHPUnit\Framework\TestCase" defines method calls but these are not supported and should be removed.
* Test that autoconfigured calls are handled gracefully.
*/
public function testProcessThrowsExceptionForAutoconfiguredCalls()
public function testProcessForAutoconfiguredCalls()
{
$container = new ContainerBuilder();
$container->registerForAutoconfiguration(parent::class)
->addMethodCall('setFoo');
$container->registerForAutoconfiguration(parent::class)->addMethodCall('setLogger');

$def = $container->register('foo', self::class)->setAutoconfigured(true);
$this->assertFalse($def->hasMethodCall('setLogger'), 'Definition shouldn\'t have method call yet.');

(new ResolveInstanceofConditionalsPass())->process($container);

$this->assertTrue(
$container->findDefinition('foo')->hasMethodCall('setLogger'),
'Definition should have "setLogger" method call.'
);
}

/**
Expand Down