Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DependencyInjection] Accept existing interfaces as valid named args #33546

Merged
merged 1 commit into from Sep 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -53,7 +53,7 @@ protected function processValue($value, $isRoot = false)
$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));
}

Expand Down
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\DependencyInjection\Tests\Compiler;

use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Compiler\ResolveNamedArgumentsPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
Expand Down Expand Up @@ -152,6 +153,19 @@ public function testTypedArgumentWithMissingDollar()
$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()
{
$container = new ContainerBuilder();
Expand Down
Expand Up @@ -2,12 +2,14 @@

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

use Psr\Container\ContainerInterface;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
*/
class NamedArgumentsDummy
{
public function __construct(CaseSensitiveClass $c, $apiKey, $hostName)
public function __construct(CaseSensitiveClass $c, $apiKey, $hostName, ContainerInterface $interface)
{
}

Expand Down