Skip to content

Commit faefc60

Browse files
committed
[DependencyInjection] Autowing: exclude abstract definitons
1 parent 71d502a commit faefc60

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,17 @@ private function populateAvailableTypes()
138138
*/
139139
private function populateAvailableType($id, Definition $definition)
140140
{
141+
// Never use abstract services
142+
if ($definition->isAbstract()) {
143+
return;
144+
}
145+
141146
foreach ($definition->getAutowiringTypes() as $type) {
142147
$this->definedTypes[$type] = true;
143148
$this->types[$type] = $id;
144149
}
145150

151+
// Cannot use reflection if the class isn't set
146152
if (!$definition->getClass()) {
147153
return;
148154
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,21 @@ public function testClassNotFoundThrowsException()
216216
$pass = new AutowirePass();
217217
$pass->process($container);
218218
}
219+
220+
public function testDontUseAbstractServices()
221+
{
222+
$container = new ContainerBuilder();
223+
224+
$container->register('abstract_foo', __NAMESPACE__.'\Foo')->setAbstract(true);
225+
$container->register('foo', __NAMESPACE__.'\Foo');
226+
$container->register('bar', __NAMESPACE__.'\Bar')->setAutowired(true);
227+
228+
$pass = new AutowirePass();
229+
$pass->process($container);
230+
231+
$arguments = $container->getDefinition('bar')->getArguments();
232+
$this->assertSame('foo', (string) $arguments[0]);
233+
}
219234
}
220235

221236
class Foo

0 commit comments

Comments
 (0)