-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
171 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
namespace App\Support; | ||
|
||
use Illuminate\Support\Arr; | ||
use InvalidArgumentException; | ||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
class CommandOptionsParser | ||
{ | ||
/** | ||
* The options being parsed. | ||
*/ | ||
protected array $options = []; | ||
|
||
/** | ||
* Construct new CommandOptionsParser instance. | ||
*/ | ||
public function __construct(array $options) | ||
{ | ||
$this->setOptions($options); | ||
} | ||
|
||
/** | ||
* Set the options to parse. | ||
*/ | ||
public function setOptions(array $options): static | ||
{ | ||
$this->options = array_filter($options); | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Tests use ArrayInput objects, so we need to handle the tokens differently. | ||
* | ||
* @return void | ||
*/ | ||
protected function parseTokensForTests() | ||
{ | ||
$options = []; | ||
foreach ($this->options as $token => $v) { | ||
if (str_starts_with($token, '--')) { | ||
if ($v == false) { | ||
continue; | ||
} | ||
if (is_array($v)) { | ||
foreach ($v as $item) { | ||
$options[] = "$token=$item"; | ||
|
||
} | ||
} else { | ||
$token = $token.($v ? "=$v" : ''); | ||
$options[] = $token; | ||
} | ||
} | ||
} | ||
|
||
return $options; | ||
} | ||
|
||
/** | ||
* Parse the set options. | ||
*/ | ||
public function parse(): array | ||
{ | ||
$options = []; | ||
$iterable = $this->options; | ||
|
||
if (app()->runningUnitTests()) { | ||
$iterable = $this->parseTokensForTests(); | ||
} | ||
|
||
foreach ($iterable as $token) { | ||
|
||
preg_match('/--([^=]+)(=)?(.*)/', $token, $match); | ||
|
||
if (! $match) { | ||
continue; | ||
} | ||
|
||
$name = $match[1]; | ||
$equals = $match[2] ?? false; | ||
$value = $match[3] ?? false; | ||
|
||
$optionExists = array_key_exists($name, $options); | ||
|
||
if ($optionExists && ($value || $equals)) { | ||
$options[$name] = [ | ||
'mode' => InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, | ||
'value' => $options[$name]['value'] ?? [], | ||
]; | ||
$options[$name]['value'] = Arr::wrap($options[$name]['value']); | ||
$options[$name]['value'][] = $value; | ||
} elseif ($value) { | ||
$options[$name] = [ | ||
'mode' => InputOption::VALUE_REQUIRED, | ||
'value' => $value, | ||
]; | ||
} elseif (! $optionExists) { | ||
$options[$name] = [ | ||
'mode' => ($value == '' && $equals) ? InputOption::VALUE_OPTIONAL : InputOption::VALUE_NONE, | ||
'value' => ($value == '' && $equals) ? '' : true, | ||
]; | ||
} else { | ||
throw new InvalidArgumentException("The '$name' option has already been provided."); | ||
} | ||
} | ||
|
||
return $options; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.