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()) {
|
if ($definition->getArguments()) {
|
||||||
throw new InvalidArgumentException(sprintf('Autoconfigured instanceof for type "%s" defines arguments but these are not supported and should be removed.', $interface));
|
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) {
|
foreach ($container->getDefinitions() as $id => $definition) {
|
||||||
@ -64,6 +61,7 @@ class ResolveInstanceofConditionalsPass implements CompilerPassInterface
|
|||||||
$definition->setInstanceofConditionals(array());
|
$definition->setInstanceofConditionals(array());
|
||||||
$parent = $shared = null;
|
$parent = $shared = null;
|
||||||
$instanceofTags = array();
|
$instanceofTags = array();
|
||||||
|
$instanceofCalls = array();
|
||||||
|
|
||||||
foreach ($conditionals as $interface => $instanceofDefs) {
|
foreach ($conditionals as $interface => $instanceofDefs) {
|
||||||
if ($interface !== $class && (!$container->getReflectionClass($class, false))) {
|
if ($interface !== $class && (!$container->getReflectionClass($class, false))) {
|
||||||
@ -81,7 +79,13 @@ class ResolveInstanceofConditionalsPass implements CompilerPassInterface
|
|||||||
$parent = 'instanceof.'.$interface.'.'.$key.'.'.$id;
|
$parent = 'instanceof.'.$interface.'.'.$key.'.'.$id;
|
||||||
$container->setDefinition($parent, $instanceofDef);
|
$container->setDefinition($parent, $instanceofDef);
|
||||||
$instanceofTags[] = $instanceofDef->getTags();
|
$instanceofTags[] = $instanceofDef->getTags();
|
||||||
|
|
||||||
|
foreach ($instanceofDef->getMethodCalls() as $methodCall) {
|
||||||
|
$instanceofCalls[] = $methodCall;
|
||||||
|
}
|
||||||
|
|
||||||
$instanceofDef->setTags(array());
|
$instanceofDef->setTags(array());
|
||||||
|
$instanceofDef->setMethodCalls(array());
|
||||||
|
|
||||||
if (isset($instanceofDef->getChanges()['shared'])) {
|
if (isset($instanceofDef->getChanges()['shared'])) {
|
||||||
$shared = $instanceofDef->isShared();
|
$shared = $instanceofDef->isShared();
|
||||||
@ -98,6 +102,7 @@ class ResolveInstanceofConditionalsPass implements CompilerPassInterface
|
|||||||
$definition = serialize($definition);
|
$definition = serialize($definition);
|
||||||
$definition = substr_replace($definition, '53', 2, 2);
|
$definition = substr_replace($definition, '53', 2, 2);
|
||||||
$definition = substr_replace($definition, 'Child', 44, 0);
|
$definition = substr_replace($definition, 'Child', 44, 0);
|
||||||
|
/** @var ChildDefinition $definition */
|
||||||
$definition = unserialize($definition);
|
$definition = unserialize($definition);
|
||||||
$definition->setParent($parent);
|
$definition->setParent($parent);
|
||||||
|
|
||||||
@ -117,6 +122,8 @@ class ResolveInstanceofConditionalsPass implements CompilerPassInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$definition->setMethodCalls(array_merge($instanceofCalls, $definition->getMethodCalls()));
|
||||||
|
|
||||||
// reset fields with "merge" behavior
|
// reset fields with "merge" behavior
|
||||||
$abstract
|
$abstract
|
||||||
->setBindings($bindings)
|
->setBindings($bindings)
|
||||||
|
@ -200,16 +200,22 @@ class ResolveInstanceofConditionalsPassTest extends TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
|
* Test that autoconfigured calls are handled gracefully.
|
||||||
* @expectedExceptionMessage Autoconfigured instanceof for type "PHPUnit\Framework\TestCase" defines method calls but these are not supported and should be removed.
|
|
||||||
*/
|
*/
|
||||||
public function testProcessThrowsExceptionForAutoconfiguredCalls()
|
public function testProcessForAutoconfiguredCalls()
|
||||||
{
|
{
|
||||||
$container = new ContainerBuilder();
|
$container = new ContainerBuilder();
|
||||||
$container->registerForAutoconfiguration(parent::class)
|
$container->registerForAutoconfiguration(parent::class)->addMethodCall('setLogger');
|
||||||
->addMethodCall('setFoo');
|
|
||||||
|
$def = $container->register('foo', self::class)->setAutoconfigured(true);
|
||||||
|
$this->assertFalse($def->hasMethodCall('setLogger'), 'Definition shouldn\'t have method call yet.');
|
||||||
|
|
||||||
(new ResolveInstanceofConditionalsPass())->process($container);
|
(new ResolveInstanceofConditionalsPass())->process($container);
|
||||||
|
|
||||||
|
$this->assertTrue(
|
||||||
|
$container->findDefinition('foo')->hasMethodCall('setLogger'),
|
||||||
|
'Definition should have "setLogger" method call.'
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user