Thanks to visit codestin.com
Credit goes to github.com

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

Conversation

GaryPEGEOT
Copy link
Contributor

Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

Allow to auto-configure method calls like:

$container->registerForAutoconfiguration(LoggerAwareInterface::class)->addMethodCall('setLogger', array(new Reference(LoggerInterface::class)));

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for starting this work

@@ -63,7 +60,10 @@ private function processDefinition(ContainerBuilder $container, $id, Definition

$definition->setInstanceofConditionals(array());
$parent = $shared = null;
$instanceofTags = array();
$instanceof = array(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another variable instead of an array would be better I think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in b61d981

@@ -77,11 +77,13 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
foreach ($instanceofDefs as $key => $instanceofDef) {
/** @var ChildDefinition $instanceofDef */
$instanceofDef = clone $instanceofDef;
$instanceofDef->setAbstract(true)->setParent($parent ?: 'abstract.instanceof.'.$id);
$parent = 'instanceof.'.$interface.'.'.$key.'.'.$id;
$instanceofDef->setAbstract(true)->setParent($parent ?: "abstract.instanceof.$id");
Copy link
Member

@nicolas-grekas nicolas-grekas Nov 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revert these changes, they are unrelated (did fabbot suggest them? if yes, please ignore it :) )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not fabbot, just did it because string interpolation run faster in PHP7, but I can revert it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes please do, that's unrelated

@@ -91,23 +93,24 @@ private function processDefinition(ContainerBuilder $container, $id, Definition

if ($parent) {
$bindings = $definition->getBindings();
$abstract = $container->setDefinition('abstract.instanceof.'.$id, $definition);
$abstract = $container->setDefinition("abstract.instanceof.$id", $definition);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

@@ -60,7 +60,6 @@ public function testProcessInheritance()
(new ResolveChildDefinitionsPass())->process($container);

$expected = array(
array('foo', array('bar')),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changing an existing test case is always suspicious,
is this really legitimate? won't it introduce BC breaks?
we need to answer these questions carefully

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change came from the fact that if a definition has already a method call (line 125: if (!$definition->hasMethodCall($call[0])) {), I do not add it again to avoid call a setter multiple times. Hard to say if we should deduplicate or not method calls...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard to say if we should deduplicate or not method calls
we should not:

$twig->addExtension($ext1);
$twig->addExtension($ext1);
...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed method filtering. Method order has changed, but seems normal since we now allow calls inheritance and they are added at the end of the process.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test order fixed in da6d7e4

@@ -20,7 +20,6 @@ services:
# calls from instanceof are kept, but this comes later
calls:
# first call is from instanceof
- [setSunshine, [bright]]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above: changing an existing test case needs to be doubly confirmed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas fixed in da6d7e4

- [enableSummer, [true]]
- [setSunshine, [warm]]
# Call from instanceof
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the order of calls changes, that means a BC break

@GaryPEGEOT GaryPEGEOT force-pushed the feature/autoconfigured-calls branch from 44b706c to 9d0bd6e Compare March 30, 2018 15:19
@GaryPEGEOT
Copy link
Contributor Author

Methods calls are now in the right order, error on Travis is unrelated (error with RedisAdapter).

@GaryPEGEOT GaryPEGEOT closed this Apr 3, 2018
@GaryPEGEOT GaryPEGEOT deleted the feature/autoconfigured-calls branch April 3, 2018 12:41
@xabbuh
Copy link
Member

xabbuh commented Apr 3, 2018

@GaryPEGEOT Why did you close here?

@nicolas-grekas
Copy link
Member

Continued in #26768

@GaryPEGEOT
Copy link
Contributor Author

Lost some of my commits, so openned a new one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants