Skip to content

[Console] Add helper resolution support for invokable commands #60555

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.4
---

* Add helper resolution support for invokable commands

7.3
---

Expand Down
12 changes: 11 additions & 1 deletion src/Symfony/Component/Console/Command/InvokableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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'];
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy