From b31a099c1a1c75672e9420e29c544650c3ee9e87 Mon Sep 17 00:00:00 2001 From: Denis Smetannikov Date: Mon, 11 Apr 2022 22:12:49 +0200 Subject: [PATCH] Fixes --- demo/Commands/ExamplesAgruments.php | 18 ++++++------- demo/my-app | 3 +-- src/CliCommand.php | 31 ++++------------------ src/CliCommandMultiProc.php | 6 ++--- tests/fake-app/Commands/TestCliOptions.php | 2 +- tests/fake-app/Commands/TestProgress.php | 4 +-- tests/fake-app/Commands/TestSleepMulti.php | 6 ++--- 7 files changed, 24 insertions(+), 46 deletions(-) diff --git a/demo/Commands/ExamplesAgruments.php b/demo/Commands/ExamplesAgruments.php index cde62fb..11fab39 100644 --- a/demo/Commands/ExamplesAgruments.php +++ b/demo/Commands/ExamplesAgruments.php @@ -164,31 +164,31 @@ protected function executeAction(): int // ./my-app examples:agruments -aQwerty -aAsd $this->getOpt('opt-array-req-default'); // 'Asd' - + $input = $this->helper->getInput(); ////////////////////////////////////////// Arguments // ./my-app examples:agruments - $this->input->getArgument('arg-req'); // null + $input->getArgument('arg-req'); // null // ./my-app examples:agruments Qwerty - $this->input->getArgument('arg-req'); // "Qwerty" + $input->getArgument('arg-req'); // "Qwerty" // ./my-app examples:agruments Qwerty - $this->input->getArgument('arg-default'); // 42 + $input->getArgument('arg-default'); // 42 // ./my-app examples:agruments Qwerty Some text - $this->input->getArgument('arg-default'); // "Some" + $input->getArgument('arg-default'); // "Some" // ./my-app examples:agruments Qwerty "Some text" - $this->input->getArgument('arg-default'); // "Some text" + $input->getArgument('arg-default'); // "Some text" // ./my-app examples:agruments Qwerty "Some text" - $this->input->getArgument('arg-optional'); // [] + $input->getArgument('arg-optional'); // [] // ./my-app examples:agruments Qwerty "Some text" 123 - $this->input->getArgument('arg-optional'); // ["123"] + $input->getArgument('arg-optional'); // ["123"] // ./my-app examples:agruments Qwerty "Some text" 123 456 "789 098" - $this->input->getArgument('arg-optional'); // ["123", "456", "789 098"] + $input->getArgument('arg-optional'); // ["123", "456", "789 098"] ////////////////////////////////////////// Standard input diff --git a/demo/my-app b/demo/my-app index 4556c71..7b900cd 100755 --- a/demo/my-app +++ b/demo/my-app @@ -24,6 +24,5 @@ require_once dirname(__DIR__) . '/vendor/autoload.php'; $application = new CliApplication('Example CLI Application', 'v1.0.0'); $application->registerCommandsByPath(__DIR__ . '/Commands', __NAMESPACE__); -// $application->setDefaultCommand('simple'); - +$application->setDefaultCommand('list'); $application->run(); diff --git a/src/CliCommand.php b/src/CliCommand.php index 2c10bdf..27b0781 100644 --- a/src/CliCommand.php +++ b/src/CliCommand.php @@ -22,7 +22,6 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\ConsoleOutput; use Symfony\Component\Console\Output\OutputInterface; use function JBZoo\Utils\bool; @@ -39,24 +38,7 @@ abstract class CliCommand extends Command * @var Helper * @psalm-suppress PropertyNotSetInConstructor */ - private $helper; - - /** - * @var InputInterface - * @psalm-suppress PropertyNotSetInConstructor - */ - protected $input; - - /** - * @var OutputInterface|ConsoleOutput - * @psalm-suppress PropertyNotSetInConstructor - */ - protected $output; - - /** - * @var OutputInterface - */ - protected $errOutput; + protected $helper; /** * @inheritDoc @@ -91,9 +73,6 @@ protected function configure(): void protected function execute(InputInterface $input, OutputInterface $output): int { $this->helper = new Helper($input, $output); - $this->input = $this->helper->getInput(); - $this->output = $this->helper->getOutput(); - $this->errOutput = $this->helper->getErrOutput(); $exitCode = 0; try { @@ -148,7 +127,7 @@ abstract protected function executeAction(): int; */ protected function getOpt(string $optionName, bool $canBeArray = true) { - $value = $this->input->getOption($optionName); + $value = $this->helper->getInput()->getOption($optionName); if ($canBeArray && \is_array($value)) { return Arr::last($value); @@ -259,7 +238,7 @@ protected function _($messages = '', string $verboseLevel = ''): void */ protected function isInfoLevel(): bool { - return $this->output->isVerbose(); + return $this->helper->getOutput()->isVerbose(); } /** @@ -267,7 +246,7 @@ protected function isInfoLevel(): bool */ protected function isWarningLevel(): bool { - return $this->output->isVeryVerbose(); + return $this->helper->getOutput()->isVeryVerbose(); } /** @@ -275,7 +254,7 @@ protected function isWarningLevel(): bool */ protected function isDebugLevel(): bool { - return $this->output->isDebug(); + return $this->helper->getOutput()->isDebug(); } /** diff --git a/src/CliCommandMultiProc.php b/src/CliCommandMultiProc.php index e98df1a..29b16c8 100644 --- a/src/CliCommandMultiProc.php +++ b/src/CliCommandMultiProc.php @@ -146,7 +146,7 @@ protected function executeMultiProcessAction(): int $procListIds = $this->getListOfChildIds(); if (!$this->getOptBool('no-progress')) { - $this->progressBar = new ProgressBarProcessManager($this->output, \count($procListIds)); + $this->progressBar = new ProgressBarProcessManager($this->helper->getOutput(), \count($procListIds)); $this->progressBar->start(); } @@ -234,7 +234,7 @@ private function initProcManager( private function createSubProcess(string $procId): Process { // Prepare option list from the parent process - $options = \array_filter($this->input->getOptions(), static function ($optionValue): bool { + $options = \array_filter($this->helper->getInput()->getOptions(), static function ($optionValue): bool { return $optionValue !== false && $optionValue !== ''; }); @@ -250,7 +250,7 @@ private function createSubProcess(string $procId): Process $options['pm-proc-id'] = $procId; // Prepare $argument list from the parent process - $arguments = $this->input->getArguments(); + $arguments = $this->helper->getInput()->getArguments(); $argumentsList = []; foreach ($arguments as $argKey => $argValue) { diff --git a/tests/fake-app/Commands/TestCliOptions.php b/tests/fake-app/Commands/TestCliOptions.php index 8f70d4a..7445e65 100644 --- a/tests/fake-app/Commands/TestCliOptions.php +++ b/tests/fake-app/Commands/TestCliOptions.php @@ -71,7 +71,7 @@ protected function executeAction(): int foreach ($options as $option) { $result[$option] = [ - 'Default' => $this->input->getOption($option), + 'Default' => $this->helper->getInput()->getOption($option), 'Bool' => $this->getOptBool($option), 'Int' => $this->getOptInt($option), 'Float' => $this->getOptFloat($option), diff --git a/tests/fake-app/Commands/TestProgress.php b/tests/fake-app/Commands/TestProgress.php index 0bd20aa..f2ed0ca 100644 --- a/tests/fake-app/Commands/TestProgress.php +++ b/tests/fake-app/Commands/TestProgress.php @@ -167,8 +167,8 @@ protected function executeAction(): int if ($testCase === 'nested') { $array = []; - $parentSection = $this->output->section(); - $childSection = $this->output->section(); + $parentSection = $this->helper->getOutput()->section(); + $childSection = $this->helper->getOutput()->section(); ProgressBar::run(3, function ($parentId) use ($testCase, $childSection) { sleep($this->getOptInt('sleep')); diff --git a/tests/fake-app/Commands/TestSleepMulti.php b/tests/fake-app/Commands/TestSleepMulti.php index 768932f..e0553f5 100644 --- a/tests/fake-app/Commands/TestSleepMulti.php +++ b/tests/fake-app/Commands/TestSleepMulti.php @@ -65,9 +65,9 @@ protected function executeOneProcess(string $pmThreadId): int $this->_([ "Started: {$pmThreadId}", 'Sleep : ' . $sleep, - 'Arg #1: ' . $this->input->getArgument('arg-1'), - 'Arg #2: ' . $this->input->getArgument('arg-2'), - 'Arg #3: ' . $this->input->getArgument('arg-3'), + 'Arg #1: ' . $this->helper->getInput()->getArgument('arg-1'), + 'Arg #2: ' . $this->helper->getInput()->getArgument('arg-2'), + 'Arg #3: ' . $this->helper->getInput()->getArgument('arg-3'), 'Env Var: ' . Env::string('JBZOO_TEST_VAR'), ]);