Allow autoconfigured calls in PHP.
This commit is contained in:
parent
d5b88eb7eb
commit
2612f81052
@ -33,9 +33,6 @@ class ResolveInstanceofConditionalsPass implements CompilerPassInterface
|
||||
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) {
|
||||
@ -64,6 +61,7 @@ class ResolveInstanceofConditionalsPass implements CompilerPassInterface
|
||||
$definition->setInstanceofConditionals(array());
|
||||
$parent = $shared = null;
|
||||
$instanceofTags = array();
|
||||
$instanceofCalls = array();
|
||||
|
||||
foreach ($conditionals as $interface => $instanceofDefs) {
|
||||
if ($interface !== $class && (!$container->getReflectionClass($class, false))) {
|
||||
@ -81,7 +79,13 @@ class ResolveInstanceofConditionalsPass implements CompilerPassInterface
|
||||
$parent = 'instanceof.'.$interface.'.'.$key.'.'.$id;
|
||||
$container->setDefinition($parent, $instanceofDef);
|
||||
$instanceofTags[] = $instanceofDef->getTags();
|
||||
|
||||
foreach ($instanceofDef->getMethodCalls() as $methodCall) {
|
||||
$instanceofCalls[] = $methodCall;
|
||||
}
|
||||
|
||||
$instanceofDef->setTags(array());
|
||||
$instanceofDef->setMethodCalls(array());
|
||||
|
||||
if (isset($instanceofDef->getChanges()['shared'])) {
|
||||
$shared = $instanceofDef->isShared();
|
||||
@ -98,6 +102,7 @@ class ResolveInstanceofConditionalsPass implements CompilerPassInterface
|
||||
$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);
|
||||
|
||||
@ -117,6 +122,8 @@ class ResolveInstanceofConditionalsPass implements CompilerPassInterface
|
||||
}
|
||||
}
|
||||
|
||||
$definition->setMethodCalls(array_merge($instanceofCalls, $definition->getMethodCalls()));
|
||||
|
||||
// reset fields with "merge" behavior
|
||||
$abstract
|
||||
->setBindings($bindings)
|
||||
|
@ -200,16 +200,22 @@ class ResolveInstanceofConditionalsPassTest extends TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @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.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user