Skip to content

Commit

Permalink
Merge pull request #332 from jolicode/simplify-raw-param
Browse files Browse the repository at this point in the history
chore: simplify Input::getRawTokens
  • Loading branch information
pyrech committed Mar 20, 2024
2 parents 46410a3 + fd6a568 commit 531fc31
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 54 deletions.
3 changes: 1 addition & 2 deletions bin/castor
Expand Up @@ -2,7 +2,6 @@
<?php

use Castor\Console\ApplicationFactory;
use Castor\Console\Input\Input;

if (file_exists($file = __DIR__ . '/../vendor/autoload.php')) {
require $file;
Expand All @@ -12,4 +11,4 @@ if (file_exists($file = __DIR__ . '/../vendor/autoload.php')) {
throw new \RuntimeException('Unable to find autoloader.');
}

ApplicationFactory::create()->run(new Input());
ApplicationFactory::create()->run();
9 changes: 3 additions & 6 deletions src/Console/Application.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.');
}
Expand Down Expand Up @@ -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));
Expand Down
11 changes: 4 additions & 7 deletions src/Console/Command/SymfonyTaskCommand.php
Expand Up @@ -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;
Expand All @@ -14,6 +14,8 @@
/** @internal */
class SymfonyTaskCommand extends Command
{
use GetRawTokenTrait;

private const OPTIONS_FILTERS = [
'--help',
'--quiet',
Expand Down Expand Up @@ -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;
Expand Down
22 changes: 4 additions & 18 deletions src/Console/Command/TaskCommand.php
Expand Up @@ -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;
Expand All @@ -26,6 +26,8 @@
/** @internal */
class TaskCommand extends Command implements SignalableCommandInterface
{
use GetRawTokenTrait;

/**
* @var array<string, string>
*/
Expand Down Expand Up @@ -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;
}
Expand Down
38 changes: 38 additions & 0 deletions src/Console/Input/GetRawTokenTrait.php
@@ -0,0 +1,38 @@
<?php

namespace Castor\Console\Input;

use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Input\InputInterface;

/** @internal */
trait GetRawTokenTrait
{
/**
* @return list<string>
*/
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;
}
}
17 changes: 0 additions & 17 deletions src/Console/Input/Input.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/GlobalHelper.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
3 changes: 1 addition & 2 deletions src/functions.php
Expand Up @@ -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;
Expand Down Expand Up @@ -632,7 +631,7 @@ function get_application(): Application
return app();
}

function input(): Input
function input(): InputInterface
{
return GlobalHelper::getInput();
}
Expand Down

0 comments on commit 531fc31

Please sign in to comment.