Skip to content

Commit f6fdbf2

Browse files
[DependencyInjection] Fix "proxy" tag: resolve its parameters and pass it to child definitions
1 parent 0e49bfe commit f6fdbf2

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

Compiler/ResolveChildDefinitionsPass.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,12 @@ private function doResolveDefinition(ChildDefinition $definition): Definition
187187
// and it's not legal on an instanceof
188188
$def->setAutoconfigured($definition->isAutoconfigured());
189189

190+
if (!$def->hasTag('proxy')) {
191+
foreach ($parentDef->getTag('proxy') as $v) {
192+
$def->addTag('proxy', $v);
193+
}
194+
}
195+
190196
return $def;
191197
}
192198
}

Compiler/ResolveParameterPlaceHoldersPass.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@ protected function processValue($value, $isRoot = false)
8585
if (isset($changes['file'])) {
8686
$value->setFile($this->bag->resolveValue($value->getFile()));
8787
}
88+
$tags = $value->getTags();
89+
if (isset($tags['proxy'])) {
90+
$tags['proxy'] = $this->bag->resolveValue($tags['proxy']);
91+
$value->setTags($tags);
92+
}
8893
}
8994

9095
$value = parent::processValue($value, $isRoot);

Tests/Compiler/ResolveChildDefinitionsPassTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,31 @@ public function testProcessDoesNotCopyTags()
118118
$this->assertEquals([], $def->getTags());
119119
}
120120

121+
public function testProcessCopiesTagsProxy()
122+
{
123+
$container = new ContainerBuilder();
124+
125+
$container
126+
->register('parent')
127+
->addTag('proxy', ['a' => 'b'])
128+
;
129+
130+
$container
131+
->setDefinition('child1', new ChildDefinition('parent'))
132+
;
133+
$container
134+
->setDefinition('child2', (new ChildDefinition('parent'))->addTag('proxy', ['c' => 'd']))
135+
;
136+
137+
$this->process($container);
138+
139+
$def = $container->getDefinition('child1');
140+
$this->assertSame(['proxy' => [['a' => 'b']]], $def->getTags());
141+
142+
$def = $container->getDefinition('child2');
143+
$this->assertSame(['proxy' => [['c' => 'd']]], $def->getTags());
144+
}
145+
121146
public function testProcessDoesNotCopyDecoratedService()
122147
{
123148
$container = new ContainerBuilder();

Tests/Compiler/ResolveParameterPlaceHoldersPassTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ public function testParameterNotFoundExceptionsIsNotThrown()
9797
$this->assertCount(1, $definition->getErrors());
9898
}
9999

100+
public function testOnlyProxyTagIsResolved()
101+
{
102+
$containerBuilder = new ContainerBuilder();
103+
$containerBuilder->setParameter('a_param', 'here_you_go');
104+
$definition = $containerBuilder->register('def');
105+
$definition->addTag('foo', ['bar' => '%a_param%']);
106+
$definition->addTag('proxy', ['interface' => '%a_param%']);
107+
108+
$pass = new ResolveParameterPlaceHoldersPass(true, false);
109+
$pass->process($containerBuilder);
110+
111+
$this->assertSame(['foo' => [['bar' => '%a_param%']], 'proxy' => [['interface' => 'here_you_go']]], $definition->getTags());
112+
}
113+
100114
private function createContainerBuilder(): ContainerBuilder
101115
{
102116
$containerBuilder = new ContainerBuilder();

0 commit comments

Comments
 (0)