diff --git a/composer.json b/composer.json index e6c98a3..c113566 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,6 @@ "require": { "php": "^7.4 || ^8.0.0", "jrosset/betterphptoken": "^1.0", - "jrosset/collections": "^2.3", "jrosset/extendedmonolog": "^1.1", "psr/log": "^1.1", "symfony/console": "^5.4", diff --git a/src/CliProgram/BaseCommand.php b/src/CliProgram/BaseCommand.php index 5174d31..43bcb4c 100644 --- a/src/CliProgram/BaseCommand.php +++ b/src/CliProgram/BaseCommand.php @@ -2,8 +2,12 @@ namespace jrosset\CliProgram; +use jrosset\CliProgram\CommandCall\CommandCall; +use jrosset\CliProgram\CommandCall\CommandCallsList; use ReflectionClass; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Output\OutputInterface; +use Throwable; /** * A basic command @@ -24,4 +28,31 @@ class BaseCommand extends Command { parent::__construct($name); $this->setAliases($aliases); } + + /** + * Run a list of subcommands + * + * Stop à the first subcommand returning other than {@see Command::SUCCESS} + * + * @param CommandCallsList|CommandCall $subcommandList The subcommands to run + * @param OutputInterface $output The output + * + * @return int The last executed subcommand return code + * + * @throws Throwable If an error occurs + */ + public function runSubCommands ($subcommandList, OutputInterface $output): int { + if (!$subcommandList instanceof CommandCallsList) { + $subcommandList = new CommandCallsList([$subcommandList]); + } + + /** @var CommandCall $subcommand */ + foreach ($subcommandList as $subcommand) { + if (($returnCode = $subcommand->run($this->getApplication(), $output)) !== Command::SUCCESS) { + return $returnCode; + } + } + + return Command::SUCCESS; + } } \ No newline at end of file diff --git a/src/CliProgram/CommandCall/CommandCall.php b/src/CliProgram/CommandCall/CommandCall.php new file mode 100644 index 0000000..cbe9c57 --- /dev/null +++ b/src/CliProgram/CommandCall/CommandCall.php @@ -0,0 +1,100 @@ + The command arguments + */ + private Arrayy $commandArguments; + + /** + * @param string|Command $commandName The new command name + * @param null|Arrayy $commandArguments The command new arguments + */ + public function __construct ($commandName, ?Arrayy $commandArguments) { + $this->setCommandName($commandName); + $this->setCommandArguments($commandArguments ?? new Arrayy()); + } + + /** + * Generate the input for the command call + * + * @return InputInterface Generate the input for the command call + */ + public function generateInput (): InputInterface { + return new ArrayInput( + (clone $this->getCommandArguments()) + ->prepend($this->getCommandName(), 'command') + ->toArray() + ); + } + /** + * Run the command + * + * @param Application $app The application + * @param OutputInterface $output The output + * + * @return int The command return code + * + * @throws Throwable If an error occurs + */ + public function run (Application $app, OutputInterface $output): int { + return $app->doRun($this->generateInput(), $output); + } + + /** + * The command name + * + * @return string The command name + */ + public function getCommandName (): string { + return $this->commandName; + } + /** + * Set the command name + * + * @param string|Command $commandName The new command name + * + * @return $this + */ + public function setCommandName ($commandName): self { + $this->commandName = $commandName instanceof Command ? $commandName->getName() : $commandName; + return $this; + } + + /** + * The command arguments + * + * @return Arrayy The command arguments + */ + public function getCommandArguments (): Arrayy { + return $this->commandArguments; + } + /** + * Set the command arguments + * + * @param Arrayy $commandArguments The command new arguments + * + * @return $this + */ + public function setCommandArguments (Arrayy $commandArguments): self { + $this->commandArguments = $commandArguments; + return $this; + } +} \ No newline at end of file diff --git a/src/CliProgram/CommandCall/CommandCallsList.php b/src/CliProgram/CommandCall/CommandCallsList.php new file mode 100644 index 0000000..2a5d566 --- /dev/null +++ b/src/CliProgram/CommandCall/CommandCallsList.php @@ -0,0 +1,19 @@ + + */ +class CommandCallsList extends AbstractCollection { + /** + * @inheritDoc + */ + public function getType (): string { + return CommandCall::class; + } +} \ No newline at end of file