Skip to content

Commit

Permalink
TASK: Refactor eel command input validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebobo committed Sep 24, 2021
1 parent 26a1d9c commit f73a147
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions Classes/Command/EvaluateEelExpressionCommand.php
Expand Up @@ -13,12 +13,16 @@
* source code.
*/

use Neos\Eel\Exception as EelException;
use Neos\Eel\ParserException;
use Neos\Flow\Annotations as Flow;
use Neos\Eel\Exception as EelException;
use Shel\Neos\Terminal\Domain\CommandContext;
use Shel\Neos\Terminal\Domain\CommandInvocationResult;
use Shel\Neos\Terminal\Service\EelEvaluationService;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\StringInput;

class EvaluateEelExpressionCommand implements TerminalCommandInterface
{
Expand All @@ -40,11 +44,28 @@ public static function getCommandDescription(): string

public static function getCommandUsage(): string
{
return 'eel <string>';
return 'eel ' . self::getInputDefinition()->getSynopsis();
}

public static function getInputDefinition(): InputDefinition
{
return new InputDefinition([
new InputArgument('expression', InputArgument::REQUIRED),
]);
}

public function invokeCommand(string $argument, CommandContext $commandContext): CommandInvocationResult
{
$input = new StringInput($argument);
$input->bind(self::getInputDefinition());

try {
$input->validate();
} catch (RuntimeException $e) {
return new CommandInvocationResult(false, $e->getMessage());
}

$expression = $input->getArgument('expression');
$success = true;

$evaluationContext = [
Expand All @@ -54,7 +75,7 @@ public function invokeCommand(string $argument, CommandContext $commandContext):
];

try {
$result = $this->eelEvaluationService->evaluateEelExpression('${' . $argument . '}', $evaluationContext);
$result = $this->eelEvaluationService->evaluateEelExpression('${' . $expression . '}', $evaluationContext);
} catch (EelException | ParserException | \Exception $e) {
$success = false;
$result = $e->getMessage();
Expand Down

0 comments on commit f73a147

Please sign in to comment.