mirror of
https://github.com/symfony/symfony.git
synced 2026-03-24 00:32:15 +01:00
[Console] Fix allowing invalid #[Autowire] references in command arguments
This commit is contained in:
@@ -142,6 +142,7 @@ final class RegisterCommandArgumentLocatorsPass implements CompilerPassInterface
|
||||
}
|
||||
|
||||
if ($autowireAttributes) {
|
||||
$invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE;
|
||||
$attribute = $autowireAttributes[0]->newInstance();
|
||||
$value = $parameterBag->resolveValue($attribute->value);
|
||||
|
||||
|
||||
@@ -11,11 +11,14 @@
|
||||
|
||||
namespace Symfony\Component\Console\Tests\DependencyInjection;
|
||||
|
||||
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Console\DependencyInjection\RegisterCommandArgumentLocatorsPass;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Definition;
|
||||
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\DependencyInjection\ServiceLocator;
|
||||
|
||||
@@ -58,6 +61,42 @@ class RegisterCommandArgumentLocatorsPassTest extends TestCase
|
||||
$this->assertArrayHasKey('test:command', $commands);
|
||||
}
|
||||
|
||||
#[DoesNotPerformAssertions]
|
||||
public function testProcessServiceArgumentWithAutowireAttribute()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
$container->register('console.argument_resolver.service')->setSynthetic(true)->addArgument(null);
|
||||
$container->register('logger')->setSynthetic(true);
|
||||
|
||||
$command = new Definition(CommandWithAutowireAttribute::class);
|
||||
$command->addTag('console.command', ['command' => 'test:command']);
|
||||
$command->addTag('console.command.service_arguments');
|
||||
$container->setDefinition('test.command', $command);
|
||||
|
||||
$pass = new RegisterCommandArgumentLocatorsPass();
|
||||
|
||||
$pass->process($container);
|
||||
$container->compile();
|
||||
}
|
||||
|
||||
public function testProcessThrowsOnInvalidAutowiredService()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
$container->register('console.argument_resolver.service')->setSynthetic(true)->addArgument(null);
|
||||
|
||||
$command = new Definition(CommandWithAutowireAttributeInvalidReference::class);
|
||||
$command->setAutowired(true);
|
||||
$command->addTag('console.command', ['command' => 'test:command']);
|
||||
$command->addTag('console.command.service_arguments');
|
||||
$container->setDefinition('test.command', $command);
|
||||
|
||||
$pass = new RegisterCommandArgumentLocatorsPass();
|
||||
$pass->process($container);
|
||||
|
||||
$this->expectException(ServiceNotFoundException::class);
|
||||
$container->compile();
|
||||
}
|
||||
|
||||
public function testProcessWithManualArgumentMapping()
|
||||
{
|
||||
$container = new ContainerBuilder();
|
||||
@@ -177,6 +216,24 @@ class CommandWithServiceArguments
|
||||
}
|
||||
}
|
||||
|
||||
class CommandWithAutowireAttribute
|
||||
{
|
||||
public function __invoke(
|
||||
#[Autowire(service: 'logger')]
|
||||
LoggerInterface $logger,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
class CommandWithAutowireAttributeInvalidReference
|
||||
{
|
||||
public function __invoke(
|
||||
#[Autowire(service: 'invalid.id')]
|
||||
?\stdClass $service2 = null,
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
class CommandWithInputOutput
|
||||
{
|
||||
public function __invoke(\Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output): void
|
||||
|
||||
Reference in New Issue
Block a user