You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
2.2 KiB
PHP
82 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace jrosset\Tests\Commands;
|
|
|
|
use DateTimeImmutable;
|
|
use DateTimeInterface;
|
|
use jrosset\CliProgram\Output\OutputWithLogger;
|
|
use jrosset\CliProgram\Requirements\IRequirements;
|
|
use jrosset\CliProgram\Validation\CommandWithValidation;
|
|
use jrosset\CliProgram\Validation\Validators\DateValidator;
|
|
use jrosset\CliProgram\Validation\Validators\IntegerValidator;
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputArgument;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Input\InputOption;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
class Hello extends CommandWithValidation implements IRequirements {
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
protected static $defaultDescription = 'Say hello';
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function __construct (?string $name = null) {
|
|
parent::__construct($name ?? 'hello', 'bonjour');
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function checkRequirements (InputInterface $input, OutputInterface $output): void {
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
protected function configure (): void {
|
|
parent::configure();
|
|
|
|
$this->addArgument(
|
|
'day',
|
|
InputArgument::OPTIONAL,
|
|
'The day',
|
|
new DateTimeImmutable(),
|
|
new DateValidator()
|
|
);
|
|
|
|
$this->addOption(
|
|
'repeat',
|
|
'r',
|
|
InputOption::VALUE_OPTIONAL,
|
|
'The number of repeat',
|
|
1,
|
|
new IntegerValidator(1, null)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
protected function execute (InputInterface $input, OutputInterface $output): int {
|
|
$output->writeln('<info>Command : ' . __CLASS__ . '</info>', OutputInterface::VERBOSITY_DEBUG);
|
|
|
|
$text = 'Hello';
|
|
$repeat = $input->getOption('repeat');
|
|
|
|
/** @var DateTimeInterface $day */
|
|
$day = $input->getArgument('day');
|
|
if ((new DateTimeImmutable())->diff($day)->days === 0) {
|
|
$text .= ' today';
|
|
}
|
|
|
|
for ($curr = 0; $curr < $repeat; $curr++) {
|
|
$output->writeln($text);
|
|
}
|
|
$output->writeln('FIN', OutputWithLogger::OPTION_SKIP_LOGGER);
|
|
return Command::SUCCESS;
|
|
}
|
|
} |