Skip to content

Commit a23444a

Browse files
committed
Merge branch '2.8'
* 2.8: [Yaml] look for colon in parsed inline string [DI][autowiring] throw exception when many services use the same class.
2 parents bcfacac + 729b98c commit a23444a

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ private function set($type, $id)
227227
*/
228228
private function createAutowiredDefinition(\ReflectionClass $typeHint, $id)
229229
{
230-
if (!$typeHint->isInstantiable()) {
230+
if (isset($this->notGuessableTypes[$typeHint->name]) || !$typeHint->isInstantiable()) {
231231
throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s".', $typeHint->name, $id));
232232
}
233233

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

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,56 @@ public function testTypeCollision()
117117
$pass->process($container);
118118
}
119119

120+
/**
121+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
122+
* @expectedExceptionMessage Unable to autowire argument of type "Symfony\Component\DependencyInjection\Tests\Compiler\Foo" for the service "a".
123+
*/
124+
public function testTypeNotGuessable()
125+
{
126+
$container = new ContainerBuilder();
127+
128+
$container->register('a1', __NAMESPACE__.'\Foo');
129+
$container->register('a2', __NAMESPACE__.'\Foo');
130+
$aDefinition = $container->register('a', __NAMESPACE__.'\NotGuessableArgument');
131+
$aDefinition->setAutowired(true);
132+
133+
$pass = new AutowirePass();
134+
$pass->process($container);
135+
}
136+
137+
/**
138+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
139+
* @expectedExceptionMessage Unable to autowire argument of type "Symfony\Component\DependencyInjection\Tests\Compiler\A" for the service "a".
140+
*/
141+
public function testTypeNotGuessableWithSubclass()
142+
{
143+
$container = new ContainerBuilder();
144+
145+
$container->register('a1', __NAMESPACE__.'\B');
146+
$container->register('a2', __NAMESPACE__.'\B');
147+
$aDefinition = $container->register('a', __NAMESPACE__.'\NotGuessableArgumentForSubclass');
148+
$aDefinition->setAutowired(true);
149+
150+
$pass = new AutowirePass();
151+
$pass->process($container);
152+
}
153+
154+
public function testTypeNotGuessableWithTypeSet()
155+
{
156+
$container = new ContainerBuilder();
157+
158+
$container->register('a1', __NAMESPACE__.'\Foo');
159+
$container->register('a2', __NAMESPACE__.'\Foo')->addAutowiringType(__NAMESPACE__.'\Foo');
160+
$aDefinition = $container->register('a', __NAMESPACE__.'\NotGuessableArgument');
161+
$aDefinition->setAutowired(true);
162+
163+
$pass = new AutowirePass();
164+
$pass->process($container);
165+
166+
$this->assertCount(1, $container->getDefinition('a')->getArguments());
167+
$this->assertEquals('a2', (string) $container->getDefinition('a')->getArgument(0));
168+
}
169+
120170
public function testWithTypeSet()
121171
{
122172
$container = new ContainerBuilder();
@@ -335,3 +385,15 @@ public function __construct(Dunglas $k, NotARealClass $r)
335385
{
336386
}
337387
}
388+
class NotGuessableArgument
389+
{
390+
public function __construct(Foo $k)
391+
{
392+
}
393+
}
394+
class NotGuessableArgumentForSubclass
395+
{
396+
public function __construct(A $k)
397+
{
398+
}
399+
}

0 commit comments

Comments
 (0)