Skip to content

Commit d7b1097

Browse files
feature #62068 [Console] Add support for Cursor helper in invokable commands (yceruto)
This PR was merged into the 7.4 branch. Discussion ---------- [Console] Add support for `Cursor` helper in invokable commands | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | - | License | MIT I missed including this helper in #59340 and it's particularly useful for creating dynamic console commands. **Before:** ```php #[AsCommand('make:pizza')] class MakePizza { public function __invoke(OutputInterface $output): int { $cursor = new Cursor($output); // ... } } ``` **After:** ```php #[AsCommand('make:pizza')] class MakePizza { public function __invoke(Cursor $cursor): int { // ... } } ``` Cheers! Commits ------- f8f0683 [Console] Add support for `Cursor` helper in invokable commands
2 parents 8a3f8cf + f8f0683 commit d7b1097

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

src/Symfony/Component/Console/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ CHANGELOG
1414
* Add `#[MapInput]` attribute to support DTOs in commands
1515
* Add optional timeout for interaction in `QuestionHelper`
1616
* Add support for interactive invokable commands with `#[Interact]` and `#[Ask]` attributes
17+
* Add support for `Cursor` helper in invokable commands
1718

1819
7.3
1920
---

src/Symfony/Component/Console/Command/InvokableCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Console\Attribute\Interact;
1717
use Symfony\Component\Console\Attribute\MapInput;
1818
use Symfony\Component\Console\Attribute\Option;
19+
use Symfony\Component\Console\Cursor;
1920
use Symfony\Component\Console\Exception\LogicException;
2021
use Symfony\Component\Console\Exception\RuntimeException;
2122
use Symfony\Component\Console\Input\InputArgument;
@@ -174,6 +175,7 @@ private function getParameters(\ReflectionFunction $function, InputInterface $in
174175
$parameters[] = match ($type->getName()) {
175176
InputInterface::class => $input,
176177
OutputInterface::class => $output,
178+
Cursor::class => new Cursor($output),
177179
SymfonyStyle::class => new SymfonyStyle($input, $output),
178180
Application::class => $this->command->getApplication(),
179181
default => throw new RuntimeException(\sprintf('Unsupported type "%s" for parameter "$%s".', $type->getName(), $parameter->getName())),

src/Symfony/Component/Console/Tests/Command/InvokableCommandTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@
1414
use PHPUnit\Framework\Assert;
1515
use PHPUnit\Framework\Attributes\DataProvider;
1616
use PHPUnit\Framework\TestCase;
17+
use Symfony\Component\Console\Application;
1718
use Symfony\Component\Console\Attribute\Argument;
1819
use Symfony\Component\Console\Attribute\Option;
1920
use Symfony\Component\Console\Command\Command;
2021
use Symfony\Component\Console\Completion\CompletionInput;
2122
use Symfony\Component\Console\Completion\CompletionSuggestions;
2223
use Symfony\Component\Console\Completion\Suggestion;
24+
use Symfony\Component\Console\Cursor;
2325
use Symfony\Component\Console\Exception\InvalidArgumentException;
2426
use Symfony\Component\Console\Exception\InvalidOptionException;
2527
use Symfony\Component\Console\Exception\LogicException;
2628
use Symfony\Component\Console\Input\ArrayInput;
2729
use Symfony\Component\Console\Input\InputInterface;
2830
use Symfony\Component\Console\Output\NullOutput;
2931
use Symfony\Component\Console\Output\OutputInterface;
32+
use Symfony\Component\Console\Style\SymfonyStyle;
3033
use Symfony\Component\Console\Tests\Fixtures\InvokableTestCommand;
3134

3235
class InvokableCommandTest extends TestCase
@@ -462,6 +465,25 @@ public function testInvalidRequiredValueOptionEvenWithDefault()
462465
$command->run(new ArrayInput(['--a' => null]), new NullOutput());
463466
}
464467

468+
public function testHelpersInjection()
469+
{
470+
$command = new Command('foo');
471+
$command->setApplication(new Application());
472+
$command->setCode(function (
473+
InputInterface $input,
474+
OutputInterface $output,
475+
Cursor $cursor,
476+
SymfonyStyle $io,
477+
Application $application,
478+
): int {
479+
$this->addToAssertionCount(1);
480+
481+
return 0;
482+
});
483+
484+
$command->run(new ArrayInput([]), new NullOutput());
485+
}
486+
465487
public function getSuggestedRoles(CompletionInput $input): array
466488
{
467489
return ['ROLE_ADMIN', 'ROLE_USER'];

0 commit comments

Comments
 (0)