parent
38c1a583dc
commit
9440e3edb6
@ -0,0 +1,100 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace jrosset\CliProgram\CommandCall;
|
||||||
|
|
||||||
|
use Arrayy\Arrayy;
|
||||||
|
use Symfony\Component\Console\Application;
|
||||||
|
use Symfony\Component\Console\Command\Command;
|
||||||
|
use Symfony\Component\Console\Input\ArrayInput;
|
||||||
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A call to a command
|
||||||
|
*/
|
||||||
|
class CommandCall {
|
||||||
|
/**
|
||||||
|
* @var string The command name
|
||||||
|
*/
|
||||||
|
private string $commandName;
|
||||||
|
/**
|
||||||
|
* @var Arrayy<string, mixed> The command arguments
|
||||||
|
*/
|
||||||
|
private Arrayy $commandArguments;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string|Command $commandName The new command name
|
||||||
|
* @param null|Arrayy<string, mixed> $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<string, mixed> The command arguments
|
||||||
|
*/
|
||||||
|
public function getCommandArguments (): Arrayy {
|
||||||
|
return $this->commandArguments;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Set the command arguments
|
||||||
|
*
|
||||||
|
* @param Arrayy<string, mixed> $commandArguments The command new arguments
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function setCommandArguments (Arrayy $commandArguments): self {
|
||||||
|
$this->commandArguments = $commandArguments;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace jrosset\CliProgram\CommandCall;
|
||||||
|
|
||||||
|
use Arrayy\Collection\AbstractCollection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A list of calls to a command
|
||||||
|
*
|
||||||
|
* @extends AbstractCollection<CommandCall>
|
||||||
|
*/
|
||||||
|
class CommandCallsList extends AbstractCollection {
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
public function getType (): string {
|
||||||
|
return CommandCall::class;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue