Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2612f81

Browse filesBrowse files
Gary PEGEOTGaryPEGEOT
Gary PEGEOT
authored andcommitted
Allow autoconfigured calls in PHP.
1 parent d5b88eb commit 2612f81
Copy full SHA for 2612f81

File tree

2 files changed

+21
-8
lines changed
Filter options

2 files changed

+21
-8
lines changed

‎src/Symfony/Component/DependencyInjection/Compiler/ResolveInstanceofConditionalsPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/ResolveInstanceofConditionalsPass.php
+10-3Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,6 @@ public function process(ContainerBuilder $container)
3333
if ($definition->getArguments()) {
3434
throw new InvalidArgumentException(sprintf('Autoconfigured instanceof for type "%s" defines arguments but these are not supported and should be removed.', $interface));
3535
}
36-
if ($definition->getMethodCalls()) {
37-
throw new InvalidArgumentException(sprintf('Autoconfigured instanceof for type "%s" defines method calls but these are not supported and should be removed.', $interface));
38-
}
3936
}
4037

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

6866
foreach ($conditionals as $interface => $instanceofDefs) {
6967
if ($interface !== $class && (!$container->getReflectionClass($class, false))) {
@@ -81,7 +79,13 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
8179
$parent = 'instanceof.'.$interface.'.'.$key.'.'.$id;
8280
$container->setDefinition($parent, $instanceofDef);
8381
$instanceofTags[] = $instanceofDef->getTags();
82+
83+
foreach ($instanceofDef->getMethodCalls() as $methodCall) {
84+
$instanceofCalls[] = $methodCall;
85+
}
86+
8487
$instanceofDef->setTags(array());
88+
$instanceofDef->setMethodCalls(array());
8589

8690
if (isset($instanceofDef->getChanges()['shared'])) {
8791
$shared = $instanceofDef->isShared();
@@ -98,6 +102,7 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
98102
$definition = serialize($definition);
99103
$definition = substr_replace($definition, '53', 2, 2);
100104
$definition = substr_replace($definition, 'Child', 44, 0);
105+
/** @var ChildDefinition $definition */
101106
$definition = unserialize($definition);
102107
$definition->setParent($parent);
103108

@@ -117,6 +122,8 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
117122
}
118123
}
119124

125+
$definition->setMethodCalls(array_merge($instanceofCalls, $definition->getMethodCalls()));
126+
120127
// reset fields with "merge" behavior
121128
$abstract
122129
->setBindings($bindings)

‎src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveInstanceofConditionalsPassTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveInstanceofConditionalsPassTest.php
+11-5Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,16 +200,22 @@ public function testBadInterfaceForAutomaticInstanceofIsOk()
200200
}
201201

202202
/**
203-
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
204-
* @expectedExceptionMessage Autoconfigured instanceof for type "PHPUnit\Framework\TestCase" defines method calls but these are not supported and should be removed.
203+
* Test that autoconfigured calls are handled gracefully.
205204
*/
206-
public function testProcessThrowsExceptionForAutoconfiguredCalls()
205+
public function testProcessForAutoconfiguredCalls()
207206
{
208207
$container = new ContainerBuilder();
209-
$container->registerForAutoconfiguration(parent::class)
210-
->addMethodCall('setFoo');
208+
$container->registerForAutoconfiguration(parent::class)->addMethodCall('setLogger');
209+
210+
$def = $container->register('foo', self::class)->setAutoconfigured(true);
211+
$this->assertFalse($def->hasMethodCall('setLogger'), 'Definition shouldn\'t have method call yet.');
211212

212213
(new ResolveInstanceofConditionalsPass())->process($container);
214+
215+
$this->assertTrue(
216+
$container->findDefinition('foo')->hasMethodCall('setLogger'),
217+
'Definition should have "setLogger" method call.'
218+
);
213219
}
214220

215221
/**

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.