bug #33546 [DependencyInjection] Accept existing interfaces as valid named args (fancyweb)
This PR was merged into the 4.3 branch.
Discussion
----------
[DependencyInjection] Accept existing interfaces as valid named args
| Q | A
| ------------- | ---
| Branch? | 4.3
| Bug fix? | yes
| New feature? | no
| Deprecations? | no
| Tickets | https://github.com/symfony/symfony/issues/33531
| License | MIT
| Doc PR | -
Commits
-------
6612e9a939
[DependencyInjection] Accept existing interfaces as valid named args
This commit is contained in:
commit
6ea7f30b89
@ -53,7 +53,7 @@ class ResolveNamedArgumentsPass extends AbstractRecursivePass
|
|||||||
$parameters = $r->getParameters();
|
$parameters = $r->getParameters();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($key[0]) && '$' !== $key[0] && !class_exists($key)) {
|
if (isset($key[0]) && '$' !== $key[0] && !class_exists($key) && !interface_exists($key, false)) {
|
||||||
throw new InvalidArgumentException(sprintf('Invalid service "%s": did you forget to add the "$" prefix to argument "%s"?', $this->currentId, $key));
|
throw new InvalidArgumentException(sprintf('Invalid service "%s": did you forget to add the "$" prefix to argument "%s"?', $this->currentId, $key));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,6 +12,7 @@
|
|||||||
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
|
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
use Symfony\Component\DependencyInjection\Compiler\ResolveNamedArgumentsPass;
|
use Symfony\Component\DependencyInjection\Compiler\ResolveNamedArgumentsPass;
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
use Symfony\Component\DependencyInjection\Reference;
|
use Symfony\Component\DependencyInjection\Reference;
|
||||||
@ -152,6 +153,19 @@ class ResolveNamedArgumentsPassTest extends TestCase
|
|||||||
$pass->process($container);
|
$pass->process($container);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testInterfaceTypedArgument()
|
||||||
|
{
|
||||||
|
$container = new ContainerBuilder();
|
||||||
|
|
||||||
|
$definition = $container->register(NamedArgumentsDummy::class, NamedArgumentsDummy::class);
|
||||||
|
$definition->setArgument(ContainerInterface::class, $expected = new Reference('foo'));
|
||||||
|
|
||||||
|
$pass = new ResolveNamedArgumentsPass();
|
||||||
|
$pass->process($container);
|
||||||
|
|
||||||
|
$this->assertSame($expected, $definition->getArgument(3));
|
||||||
|
}
|
||||||
|
|
||||||
public function testResolvesMultipleArgumentsOfTheSameType()
|
public function testResolvesMultipleArgumentsOfTheSameType()
|
||||||
{
|
{
|
||||||
$container = new ContainerBuilder();
|
$container = new ContainerBuilder();
|
||||||
|
@ -2,12 +2,14 @@
|
|||||||
|
|
||||||
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
|
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
|
||||||
|
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Kévin Dunglas <dunglas@gmail.com>
|
* @author Kévin Dunglas <dunglas@gmail.com>
|
||||||
*/
|
*/
|
||||||
class NamedArgumentsDummy
|
class NamedArgumentsDummy
|
||||||
{
|
{
|
||||||
public function __construct(CaseSensitiveClass $c, $apiKey, $hostName)
|
public function __construct(CaseSensitiveClass $c, $apiKey, $hostName, ContainerInterface $interface)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user