8000 [DI] Allow autoconfigured calls in PHP by GaryPEGEOT · Pull Request #26768 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI] Allow autoconfigured calls in PHP #26768

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

Merged
merged 4 commits into from
Apr 20, 2018
Merged
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,13 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
$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();
Expand All @@ -98,6 +102,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 +122,8 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
}
}

$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 @@ -207,6 +207,16 @@ public function getYamlCompileTests()
'child_service',
'child_service_expected',
);

$container = new ContainerBuilder();
$container->registerForAutoconfiguration(IntegrationTestStub::class)
->addMethodCall('setSunshine', array('supernova'));
yield array(
'instanceof_and_calls',
'main_service',
'main_service_expected',
$container,
);
}
}

Expand Down
8000
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,34 @@ 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');

$expected = array(
array('setFoo', array(
'plain_value',
'%some_parameter%',
)),
array('callBar', array()),
array('isBaz', array()),
);

$container->registerForAutoconfiguration(parent::class)->addMethodCall('setFoo', $expected[0][1]);
$container->registerForAutoconfiguration(self::class)->addMethodCall('callBar');

$def = $container->register('foo', self::class)->setAutoconfigured(true)->addMethodCall('isBaz');
$this->assertEquals(
array(array('isBaz', array())),
$def->getMethodCalls(),
'Definition shouldn\'t have only one method call.'
);

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

$this->assertEquals($expected, $container->findDefinition('foo')->getMethodCalls());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
services:
# main_service should look like this in the end
main_service_expected:
class: Symfony\Component\DependencyInjection\Tests\Compiler\IntegrationTestStub
public: true
autoconfigure: true
calls:
- [setSunshine, [supernova]]
- [setSunshine, [warm]]

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
_instanceof:
Symfony\Component\DependencyInjection\Tests\Compiler\IntegrationTestStubParent:
calls:
- [setSunshine, [warm]]
main_service:
class: Symfony\Component\DependencyInjection\Tests\Compiler\IntegrationTestStub
autoconfigure: true
public: true
0