From fd6a56877c6f87202ab9628fd384787ab6dac558 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Pineau?= Date: Wed, 20 Mar 2024 11:41:02 +0100 Subject: [PATCH] chore: simplify Input::getRawTokens --- bin/castor | 3 +- src/Console/Application.php | 9 ++--- src/Console/Command/SymfonyTaskCommand.php | 11 +++---- src/Console/Command/TaskCommand.php | 22 +++---------- src/Console/Input/GetRawTokenTrait.php | 38 ++++++++++++++++++++++ src/Console/Input/Input.php | 17 ---------- src/GlobalHelper.php | 4 +-- src/functions.php | 3 +- 8 files changed, 53 insertions(+), 54 deletions(-) create mode 100644 src/Console/Input/GetRawTokenTrait.php delete mode 100644 src/Console/Input/Input.php diff --git a/bin/castor b/bin/castor index 9512426e..873b8d6f 100755 --- a/bin/castor +++ b/bin/castor @@ -2,7 +2,6 @@ run(new Input()); +ApplicationFactory::create()->run(); diff --git a/src/Console/Application.php b/src/Console/Application.php index 618ec635..4aad94d5 100644 --- a/src/Console/Application.php +++ b/src/Console/Application.php @@ -4,7 +4,6 @@ use Castor\Console\Command\SymfonyTaskCommand; use Castor\Console\Command\TaskCommand; -use Castor\Console\Input\Input; use Castor\Context; use Castor\ContextDescriptor; use Castor\ContextGeneratorDescriptor; @@ -47,7 +46,7 @@ class Application extends SymfonyApplication public const VERSION = 'v0.14.0'; // "Current" objects availables at some point of the lifecycle - private Input $input; + private InputInterface $input; private SectionOutput $sectionOutput; private SymfonyStyle $symfonyStyle; private Command $command; @@ -95,7 +94,7 @@ public function __construct( GlobalHelper::setApplication($this); } - public function getInput(): Input + public function getInput(): InputInterface { return $this->input ?? throw new \LogicException('Input not available yet.'); } @@ -127,9 +126,7 @@ public function getCommand(bool $allowNull = false): ?Command // is registered public function doRun(InputInterface $input, OutputInterface $output): int { - if ($input instanceof Input) { - $this->input = $input; - } + $this->input = $input; $this->sectionOutput = new SectionOutput($output); $this->symfonyStyle = new SymfonyStyle($input, $output); $this->logger->pushHandler(new ConsoleHandler($output)); diff --git a/src/Console/Command/SymfonyTaskCommand.php b/src/Console/Command/SymfonyTaskCommand.php index bf064d9f..93e296aa 100644 --- a/src/Console/Command/SymfonyTaskCommand.php +++ b/src/Console/Command/SymfonyTaskCommand.php @@ -3,7 +3,7 @@ namespace Castor\Console\Command; use Castor\Attribute\AsSymfonyTask; -use Castor\Console\Input\Input; +use Castor\Console\Input\GetRawTokenTrait; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; @@ -14,6 +14,8 @@ /** @internal */ class SymfonyTaskCommand extends Command { + use GetRawTokenTrait; + private const OPTIONS_FILTERS = [ '--help', '--quiet', @@ -68,14 +70,9 @@ protected function configure(): void } } - /** - * @param Input $input - */ protected function execute(InputInterface $input, OutputInterface $output): int { - $extra = array_filter($input->getRawTokens(), fn ($item) => $item !== $this->taskAttribute->name); - - $p = new Process([...$this->taskAttribute->console, $this->taskAttribute->originalName, ...$extra]); + $p = new Process([...$this->taskAttribute->console, $this->taskAttribute->originalName, ...$this->getRawTokens($input)]); $p->run(fn ($type, $bytes) => print ($bytes)); return $p->getExitCode() ?? 0; diff --git a/src/Console/Command/TaskCommand.php b/src/Console/Command/TaskCommand.php index a5ec276f..8c9e1a30 100644 --- a/src/Console/Command/TaskCommand.php +++ b/src/Console/Command/TaskCommand.php @@ -8,7 +8,7 @@ use Castor\Attribute\AsRawTokens; use Castor\Attribute\AsTask; use Castor\Console\Application; -use Castor\Console\Input\Input; +use Castor\Console\Input\GetRawTokenTrait; use Castor\Event\AfterExecuteTaskEvent; use Castor\Event\BeforeExecuteTaskEvent; use Castor\EventDispatcher; @@ -26,6 +26,8 @@ /** @internal */ class TaskCommand extends Command implements SignalableCommandInterface { + use GetRawTokenTrait; + /** * @var array */ @@ -150,29 +152,13 @@ protected function configure(): void } } - /** - * @param Input $input - */ protected function execute(InputInterface $input, OutputInterface $output): int { $args = []; foreach ($this->function->getParameters() as $parameter) { if ($parameter->getAttributes(AsRawTokens::class, \ReflectionAttribute::IS_INSTANCEOF)[0] ?? null) { - $parameters = []; - $keep = false; - foreach ($input->getRawTokens() as $value) { - if ($value === $input->getFirstArgument()) { - $keep = true; - - continue; - } - if ($keep) { - $parameters[] = $value; - } - } - - $args[] = $parameters; + $args[] = $this->getRawTokens($input); continue; } diff --git a/src/Console/Input/GetRawTokenTrait.php b/src/Console/Input/GetRawTokenTrait.php new file mode 100644 index 00000000..a839989f --- /dev/null +++ b/src/Console/Input/GetRawTokenTrait.php @@ -0,0 +1,38 @@ + + */ + private function getRawTokens(InputInterface $input): array + { + if (!$input instanceof ArgvInput) { + throw new \RuntimeException('The input must be an instance of ArgvInput.'); + } + + // @phpstan-ignore-next-line + $tokens = (fn () => $this->tokens)->bindTo($input, ArgvInput::class)(); + + $parameters = []; + $keep = false; + foreach ($tokens as $value) { + if ($value === $input->getFirstArgument()) { + $keep = true; + + continue; + } + if ($keep) { + $parameters[] = $value; + } + } + + return $parameters; + } +} diff --git a/src/Console/Input/Input.php b/src/Console/Input/Input.php deleted file mode 100644 index 85efc40c..00000000 --- a/src/Console/Input/Input.php +++ /dev/null @@ -1,17 +0,0 @@ - - */ - public function getRawTokens(): array - { - // @phpstan-ignore-next-line - return (fn () => $this->tokens)->bindTo($this, ArgvInput::class)(); - } -} diff --git a/src/GlobalHelper.php b/src/GlobalHelper.php index 11aed709..c6a210ab 100644 --- a/src/GlobalHelper.php +++ b/src/GlobalHelper.php @@ -3,10 +3,10 @@ namespace Castor; use Castor\Console\Application; -use Castor\Console\Input\Input; use Monolog\Logger; use Psr\Cache\CacheItemPoolInterface; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Filesystem\Filesystem; @@ -57,7 +57,7 @@ public static function getLogger(): Logger return self::getApplication()->logger; } - public static function getInput(): Input + public static function getInput(): InputInterface { return self::getApplication()->getInput(); } diff --git a/src/functions.php b/src/functions.php index abed0c6f..96b631ea 100644 --- a/src/functions.php +++ b/src/functions.php @@ -4,7 +4,6 @@ use Castor\Attribute\AsContextGenerator; use Castor\Console\Application; -use Castor\Console\Input\Input; use Castor\Exception\ExecutableNotFoundException; use Castor\Exception\MinimumVersionRequirementNotMetException; use Castor\Exception\WaitFor\ExitedBeforeTimeoutException; @@ -632,7 +631,7 @@ function get_application(): Application return app(); } -function input(): Input +function input(): InputInterface { return GlobalHelper::getInput(); }