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 13dc341

Browse filesBrowse files
feature #27806 [DI] Allow autoconfiguring bindings (nicolas-grekas)
This PR was merged into the 4.2-dev branch. Discussion ---------- [DI] Allow autoconfiguring bindings | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - I've come up with a case where we will need to inject a different service based on which interfaces a consumer service implements: injecting a different token storage for monolog processor than for everything else. Required in #27801. Commits ------- 7c29977 [DI] Allow autoconfiguring bindings
2 parents 568e3a4 + 7c29977 commit 13dc341
Copy full SHA for 13dc341

File tree

Expand file treeCollapse file tree

6 files changed

+38
-4
lines changed
Filter options
Expand file treeCollapse file tree

6 files changed

+38
-4
lines changed

‎src/Symfony/Component/DependencyInjection/Argument/BoundArgument.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Argument/BoundArgument.php
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,14 @@ final class BoundArgument implements ArgumentInterface
2222
private $identifier;
2323
private $used;
2424

25-
public function __construct($value)
25+
public function __construct($value, bool $trackUsage = true)
2626
{
2727
$this->value = $value;
28-
$this->identifier = ++self::$sequence;
28+
if ($trackUsage) {
29+
$this->identifier = ++self::$sequence;
30+
} else {
31+
$this->used = true;
32+
}
2933
}
3034

3135
/**

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/ResolveInstanceofConditionalsPass.php
+4-1Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
6262
$parent = $shared = null;
6363
$instanceofTags = array();
6464
$instanceofCalls = array();
65+
$instanceofBindings = array();
6566

6667
foreach ($conditionals as $interface => $instanceofDefs) {
6768
if ($interface !== $class && (!$container->getReflectionClass($class, false))) {
@@ -79,13 +80,15 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
7980
$parent = '.instanceof.'.$interface.'.'.$key.'.'.$id;
8081
$container->setDefinition($parent, $instanceofDef);
8182
$instanceofTags[] = $instanceofDef->getTags();
83+
$instanceofBindings = $instanceofDef->getBindings() + $instanceofBindings;
8284

8385
foreach ($instanceofDef->getMethodCalls() as $methodCall) {
8486
$instanceofCalls[] = $methodCall;
8587
}
8688

8789
$instanceofDef->setTags(array());
8890
$instanceofDef->setMethodCalls(array());
91+
$instanceofDef->setBindings(array());
8992

9093
if (isset($instanceofDef->getChanges()['shared'])) {
9194
$shared = $instanceofDef->isShared();
@@ -123,7 +126,7 @@ private function processDefinition(ContainerBuilder $container, $id, Definition
123126
}
124127

125128
$definition->setMethodCalls(array_merge($instanceofCalls, $definition->getMethodCalls()));
126-
$definition->setBindings($bindings);
129+
$definition->setBindings($bindings + $instanceofBindings);
127130

128131
// reset fields with "merge" behavior
129132
$abstract

‎src/Symfony/Component/DependencyInjection/Loader/Configurator/InstanceofConfigurator.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Loader/Configurator/InstanceofConfigurator.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class InstanceofConfigurator extends AbstractServiceConfigurator
2626
use Traits\PublicTrait;
2727
use Traits\ShareTrait;
2828
use Traits\TagTrait;
29+
use Traits\BindTrait;
2930

3031
/**
3132
* Defines an instanceof-conditional to be applied to following service definitions.

‎src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class YamlFileLoader extends FileLoader
9393
'calls' => 'calls',
9494
'tags' => 'tags',
9595
'autowire' => 'autowire',
96+
'bind' => 'bind',
9697
);
9798

9899
private static $defaultsKeywords = array(

‎src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
<xsd:element name="call" type="call" minOccurs="0" maxOccurs="unbounded" />
142142
<xsd:element name="tag" type="tag" minOccurs="0" maxOccurs="unbounded" />
143143
<xsd:element name="property" type="property" minOccurs="0" maxOccurs="unbounded" />
144+
<xsd:element name="bind" type="bind" minOccurs="0" maxOccurs="unbounded" />
144145
</xsd:choice>
145146
<xsd:attribute name="id" type="xsd:string" use="required" />
146147
<xsd:attribute name="shared" type="boolean" />

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Compiler/ResolveInstanceofConditionalsPassTest.php
+25-1Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\DependencyInjection\Compiler\ResolveChildDefinitionsPass;
1818
use Symfony\Component\DependencyInjection\Compiler\ResolveInstanceofConditionalsPass;
1919
use Symfony\Component\DependencyInjection\ContainerBuilder;
20+
use Symfony\Component\DependencyInjection\Reference;
2021

2122
class ResolveInstanceofConditionalsPassTest extends TestCase
2223
{
@@ -270,7 +271,30 @@ public function testMergeReset()
270271
$this->assertTrue($abstract->isAbstract());
271272
}
272273

273-
public function testBindings()
274+
public function testProcessForAutoconfiguredBindings()
275+
{
276+
$container = new ContainerBuilder();
277+
278+
$container->registerForAutoconfiguration(self::class)
279+
->setBindings(array(
280+
'$foo' => new BoundArgument(234, false),
281+
parent::class => new BoundArgument(new Reference('foo'), false),
282+
));
283+
284+
$container->register('foo', self::class)
285+
->setAutoconfigured(true)
286+
->setBindings(array('$foo' => new BoundArgument(123, false)));
287+
288+
(new ResolveInstanceofConditionalsPass())->process($container);
289+
290+
$expected = array(
291+
'$foo' => new BoundArgument(123, false),
292+
parent::class => new BoundArgument(new Reference('foo'), false),
293+
);
294+
$this->assertEquals($expected, $container->findDefinition('foo')->getBindings());
295+
}
296+
297+
public function testBindingsOnInstanceofConditionals()
274298
{
275299
$container = new ContainerBuilder();
276300
$def = $container->register('foo', self::class)->setBindings(array('$toto' => 123));

0 commit comments

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