diff --git a/src/Symfony/Component/Console/CHANGELOG.md b/src/Symfony/Component/Console/CHANGELOG.md index 9f3ae3d7d2326..4afafdd583cf7 100644 --- a/src/Symfony/Component/Console/CHANGELOG.md +++ b/src/Symfony/Component/Console/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +7.4 +--- + + * Add helper resolution support for invokable commands + 7.3 --- diff --git a/src/Symfony/Component/Console/Command/InvokableCommand.php b/src/Symfony/Component/Console/Command/InvokableCommand.php index 72ff407c81fdf..ce5c3ece8d737 100644 --- a/src/Symfony/Component/Console/Command/InvokableCommand.php +++ b/src/Symfony/Component/Console/Command/InvokableCommand.php @@ -133,11 +133,21 @@ private function getParameters(InputInterface $input, OutputInterface $output): throw new LogicException(\sprintf('The parameter "$%s" must have a named type. Untyped, Union or Intersection types are not supported.', $parameter->getName())); } + if ($application = $this->command->getApplication()) { + foreach ($application->getHelperSet() as $helper) { + if ($type->getName() === $helper::class) { + $parameters[] = $helper; + + continue 2; + } + } + } + $parameters[] = match ($type->getName()) { InputInterface::class => $input, OutputInterface::class => $output, SymfonyStyle::class => new SymfonyStyle($input, $output), - Application::class => $this->command->getApplication(), + Application::class => $application, default => throw new RuntimeException(\sprintf('Unsupported type "%s" for parameter "$%s".', $type->getName(), $parameter->getName())), }; } diff --git a/src/Symfony/Component/Console/Tests/Command/InvokableCommandTest.php b/src/Symfony/Component/Console/Tests/Command/InvokableCommandTest.php index 5ab7951e7f575..aef107c6cbc23 100644 --- a/src/Symfony/Component/Console/Tests/Command/InvokableCommandTest.php +++ b/src/Symfony/Component/Console/Tests/Command/InvokableCommandTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Console\Tests\Command; use PHPUnit\Framework\TestCase; +use Symfony\Component\Console\Application; use Symfony\Component\Console\Attribute\Argument; use Symfony\Component\Console\Attribute\Option; use Symfony\Component\Console\Command\Command; @@ -20,6 +21,7 @@ use Symfony\Component\Console\Completion\Suggestion; use Symfony\Component\Console\Exception\InvalidOptionException; use Symfony\Component\Console\Exception\LogicException; +use Symfony\Component\Console\Helper\FormatterHelper; use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\NullOutput; @@ -372,6 +374,19 @@ public function testInvalidRequiredValueOptionEvenWithDefault() $command->run(new ArrayInput(['--a' => null]), new NullOutput()); } + public function testResolveHelper() + { + $command = new Command('foo'); + $command->setApplication(new Application()); + $command->setCode(function (FormatterHelper $formatter): int { + $this->assertSame('foo...', $formatter->truncate('foobar', 3)); + + return 0; + }); + + $command->run(new ArrayInput([]), new NullOutput()); + } + public function getSuggestedRoles(CompletionInput $input): array { return ['ROLE_ADMIN', 'ROLE_USER'];
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: