Skip to content

Commit

Permalink
validate arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
recursivetree committed Nov 15, 2022
1 parent 79e3367 commit 2a5b60d
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions src/Http/Validation/Custom/ArtisanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down

0 comments on commit 2a5b60d

Please sign in to comment.