|
|
|
@ -7,12 +7,12 @@ use jrosset\CliProgram\Validation\Validators\InvalidValueException;
|
|
|
|
|
use jrosset\CliProgram\Validation\Validators\IValidator;
|
|
|
|
|
use ReflectionFunction;
|
|
|
|
|
use Stringable;
|
|
|
|
|
use Symfony\Component\Console\Command\Command;
|
|
|
|
|
use Symfony\Component\Console\Exception\InvalidArgumentException;
|
|
|
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provide a value validation process ({@see IValidator}) to {@see self::addArgument()} and {@see self::addOption()}
|
|
|
|
@ -34,6 +34,8 @@ trait TCommandWithValidation {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*
|
|
|
|
|
* @throws Throwable If an argument or option is not valid
|
|
|
|
|
*/
|
|
|
|
|
protected function initialize (InputInterface $input, OutputInterface $output): void {
|
|
|
|
|
parent::setCode(
|
|
|
|
@ -49,8 +51,10 @@ trait TCommandWithValidation {
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* @inheritDoc
|
|
|
|
|
*
|
|
|
|
|
* @noinspection PhpMissingReturnTypeInspection
|
|
|
|
|
*/
|
|
|
|
|
public function setCode (callable $code): static {
|
|
|
|
|
public function setCode (callable $code) {
|
|
|
|
|
if ($code instanceof Closure) {
|
|
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
|
|
|
|
$codeReflection = new ReflectionFunction($code);
|
|
|
|
@ -69,9 +73,6 @@ trait TCommandWithValidation {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
$code = $code(...);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// {@see Command::$code} is used to perform input validation before execution, so set {@see self::$realCode} instead
|
|
|
|
|
$this->realCode = $code;
|
|
|
|
@ -193,7 +194,6 @@ trait TCommandWithValidation {
|
|
|
|
|
* @param mixed $default The default value (for InputArgument::OPTIONAL mode only)
|
|
|
|
|
* @param string $description The argument description
|
|
|
|
|
* @param IValidator|null $validator The validator or Null if none
|
|
|
|
|
* @param Closure|array|null $suggestedValues The function or list of suggested values when completing ; Null if none
|
|
|
|
|
*
|
|
|
|
|
* @return $this
|
|
|
|
|
*
|
|
|
|
@ -203,21 +203,15 @@ trait TCommandWithValidation {
|
|
|
|
|
string $name,
|
|
|
|
|
int $mode = null,
|
|
|
|
|
string $description = '',
|
|
|
|
|
mixed $default = null,
|
|
|
|
|
?IValidator $validator = null,
|
|
|
|
|
Closure|array|null $suggestedValues = null
|
|
|
|
|
): static {
|
|
|
|
|
$default = null,
|
|
|
|
|
?IValidator $validator = null
|
|
|
|
|
): self {
|
|
|
|
|
$default = static::treatStringableDefaultValue($default);
|
|
|
|
|
if ($validator !== null) {
|
|
|
|
|
$default = $validator->getValidDefault($default);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($suggestedValues !== null) {
|
|
|
|
|
parent::addArgument($name, $mode, $description, $default, $suggestedValues);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
parent::addArgument($name, $mode, $description, $default);
|
|
|
|
|
}
|
|
|
|
|
return $this->setArgumentValidator($name, $validator);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
@ -230,7 +224,7 @@ trait TCommandWithValidation {
|
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException If the {@see InputArgument} doesn't exist
|
|
|
|
|
*/
|
|
|
|
|
public function setArgumentValidator (string $name, ?IValidator $validator): static {
|
|
|
|
|
public function setArgumentValidator (string $name, ?IValidator $validator): self {
|
|
|
|
|
if (!$this->getDefinition()->hasArgument($name)) {
|
|
|
|
|
throw new InvalidArgumentException(sprintf('The "%s" argument does not exist', $name));
|
|
|
|
|
}
|
|
|
|
@ -266,7 +260,6 @@ trait TCommandWithValidation {
|
|
|
|
|
* @param string $description The argument description
|
|
|
|
|
* @param mixed $default The default value (must be null for InputOption::VALUE_NONE)
|
|
|
|
|
* @param IValidator|null $validator The validator or Null if none ; ignored if **$mode** = {@see InputOption::VALUE_NONE}
|
|
|
|
|
* @param Closure|array|null $suggestedValues The function or list of suggested values when completing ; Null if none
|
|
|
|
|
*
|
|
|
|
|
* @return $this
|
|
|
|
|
*
|
|
|
|
@ -274,24 +267,18 @@ trait TCommandWithValidation {
|
|
|
|
|
*/
|
|
|
|
|
public function addOption (
|
|
|
|
|
string $name,
|
|
|
|
|
string|array|null $shortcut = null,
|
|
|
|
|
$shortcut = null,
|
|
|
|
|
int $mode = null,
|
|
|
|
|
string $description = '',
|
|
|
|
|
mixed $default = null,
|
|
|
|
|
?IValidator $validator = null,
|
|
|
|
|
Closure|array|null $suggestedValues = null
|
|
|
|
|
): static {
|
|
|
|
|
$default = null,
|
|
|
|
|
?IValidator $validator = null
|
|
|
|
|
): self {
|
|
|
|
|
$default = static::treatStringableDefaultValue($default);
|
|
|
|
|
if ($validator !== null) {
|
|
|
|
|
$default = $validator->getValidDefault($default);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($suggestedValues !== null) {
|
|
|
|
|
parent::addOption($name, $shortcut, $mode, $description, $default, $suggestedValues);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
parent::addOption($name, $shortcut, $mode, $description, $default);
|
|
|
|
|
}
|
|
|
|
|
return $this->setOptionValidator($name, $validator);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
@ -304,7 +291,7 @@ trait TCommandWithValidation {
|
|
|
|
|
*
|
|
|
|
|
* @throws InvalidArgumentException If the {@see InputOption} doesn't exist
|
|
|
|
|
*/
|
|
|
|
|
public function setOptionValidator (string $name, ?IValidator $validator): static {
|
|
|
|
|
public function setOptionValidator (string $name, ?IValidator $validator): self {
|
|
|
|
|
if (!$this->getDefinition()->hasOption($name)) {
|
|
|
|
|
throw new InvalidArgumentException(sprintf('The "--%s" option does not exist', $name));
|
|
|
|
|
}
|
|
|
|
@ -338,7 +325,7 @@ trait TCommandWithValidation {
|
|
|
|
|
*
|
|
|
|
|
* @return mixed The default value
|
|
|
|
|
*/
|
|
|
|
|
protected static function treatStringableDefaultValue (mixed $default): mixed {
|
|
|
|
|
protected static function treatStringableDefaultValue ($default) {
|
|
|
|
|
if ($default instanceof Stringable) {
|
|
|
|
|
return $default->__toString();
|
|
|
|
|
}
|
|
|
|
|