-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Handle command defined with AsCommand php attribute
- Loading branch information
Jérémy
committed
Dec 24, 2024
1 parent
a3a7e4a
commit c648f8f
Showing
4 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the zenstruck/console-test package. | ||
* | ||
* (c) Kevin Bond <kevinbond@gmail.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Console\Test\Tests\Fixture; | ||
|
||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\Table; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\ConsoleOutputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Question\Question; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
/** | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
*/ | ||
#[AsCommand( | ||
name: 'fixture:command', | ||
description: 'zenstruck/console-test command for tests' | ||
)] | ||
final class FixtureAttributeCommand extends Command | ||
{ | ||
protected function configure(): void | ||
{ | ||
$this | ||
->addArgument('arg1', InputArgument::OPTIONAL) | ||
->addOption('opt1', null, InputOption::VALUE_NONE) | ||
->addOption('opt2', null, InputOption::VALUE_REQUIRED) | ||
->addOption('opt3', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY) | ||
->addOption('throw', null, InputOption::VALUE_NONE) | ||
->addOption('code', null, InputOption::VALUE_REQUIRED, '', 0) | ||
; | ||
} | ||
|
||
/** | ||
* @param ConsoleOutputInterface $output | ||
*/ | ||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$errOutput = $output->getErrorOutput(); | ||
|
||
$output->writeln('Executing <info>command</info>...'); | ||
$output->writeln("verbosity: {$output->getVerbosity()}"); | ||
$output->writeln('decorated: '.($output->isDecorated() ? 'yes' : 'no')); | ||
$errOutput->writeln('Error output.'); | ||
|
||
if ($input->getOption('throw')) { | ||
throw new \RuntimeException('Exception thrown!'); | ||
} | ||
|
||
if ($arg1 = $input->getArgument('arg1')) { | ||
$output->writeln("arg1 value: {$arg1}"); | ||
} | ||
|
||
if ($input->getOption('opt1')) { | ||
$output->writeln('opt1 option set'); | ||
} | ||
|
||
if ($opt2 = $input->getOption('opt2')) { | ||
$output->writeln("opt2 value: {$opt2}"); | ||
} | ||
|
||
foreach ($input->getOption('opt3') as $value) { | ||
$output->writeln("opt3 value: {$value}"); | ||
} | ||
|
||
(new SymfonyStyle($input, $output))->success('Long link: https://github.com/zenstruck/console-test/blob/997ee1f66743342ffd9cd00a77613ebfa2efd2b8/src/CommandResult.php'); | ||
|
||
$table = new Table($output->section()); | ||
$table->addRow(['table row 1']); | ||
$table->render(); | ||
$table->appendRow(['table row 2']); | ||
|
||
return (int) $input->getOption('code'); | ||
} | ||
|
||
protected function interact(InputInterface $input, OutputInterface $output): void | ||
{ | ||
$value = $this->getHelper('question')->ask($input, $output, new Question('Arg1 value?')); | ||
|
||
$input->setArgument('arg1', $value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters