From 2a5b60deff022dfa3edd9bb4db7d36670f29e33b Mon Sep 17 00:00:00 2001 From: recursive_tree Date: Tue, 15 Nov 2022 10:27:42 +0100 Subject: [PATCH] validate arguments --- src/Http/Validation/Custom/ArtisanCommand.php | 28 ++++++++++++++++--- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Http/Validation/Custom/ArtisanCommand.php b/src/Http/Validation/Custom/ArtisanCommand.php index 19b4dde5d..8d5d7815d 100644 --- a/src/Http/Validation/Custom/ArtisanCommand.php +++ b/src/Http/Validation/Custom/ArtisanCommand.php @@ -23,6 +23,8 @@ namespace Seat\Web\Http\Validation\Custom; use Artisan; +use Symfony\Component\Console\Input\ArgvInput; +use Symfony\Component\Console\Exception\RuntimeException; /** * Class ArtisanCommand. @@ -48,12 +50,30 @@ public static function validate($attribute, $value, $parameters, $validator) //split arguments $argv = explode(' ', $value); - //get command name and check if it exists - $commandName = $argv[0] ?? null; - if($commandName === null) return false; + //check if a command name is included and get it + if(count($argv) === 0) return false; + $commandName = array_shift($argv); + + //check if the command exists and get it if(! array_key_exists($commandName, $allCommands)) return false; + $command = $allCommands[$commandName]; + + //extract the argument definition off the command + $argumentDefinition = $command->getDefinition(); - //I haven't found a way to validate the arguments, so we assume they are alright + //validate our arguments + try { + //create an argument parser and supply it with the provided arguments. + // Also supply the argument definition to check if it complies with the command + // ArgvInput ignores the first element of the array(usually the executable name when calling it from the console), so just supply an empty string + $input = new ArgvInput(["", ...$argv], $argumentDefinition); + //I guess this validates the input? + $input->validate(); + } + // instead of returning false in ->validate(), it throws an exception + catch (\RuntimeException $e){ + return false; + } //all checks have passed, we can allow it return true;