Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Dry-up a lot Console parser. #16197

Merged
merged 1 commit into from
Nov 1, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 25 additions & 26 deletions src/Illuminate/Console/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,18 @@ public static function parse($expression)
throw new InvalidArgumentException('Console command definition is empty.');
}

preg_match('/[^\s]+/', $expression, $matches);

if (isset($matches[0])) {
$name = $matches[0];
} else {
if (! preg_match('/[^\s]+/', $expression, $matches)) {
throw new InvalidArgumentException('Unable to determine command name from signature.');
}

preg_match_all('/\{\s*(.*?)\s*\}/', $expression, $matches);
$name = $matches[0];

$tokens = isset($matches[1]) ? $matches[1] : [];
if (preg_match_all('/\{\s*(.*?)\s*\}/', $expression, $matches)) {
$tokens = $matches[1];

if (count($tokens)) {
return array_merge([$name], static::parameters($tokens));
if (count($tokens)) {
return array_merge([$name], static::parameters($tokens));
}
}

return [$name, [], []];
Expand All @@ -55,10 +53,10 @@ protected static function parameters(array $tokens)
$options = [];

foreach ($tokens as $token) {
if (! Str::startsWith($token, '--')) {
$arguments[] = static::parseArgument($token);
if (preg_match('/-{2,}(.*)/', $token, $matches)) {
$options[] = static::parseOption($matches[1]);
} else {
$options[] = static::parseOption(ltrim($token, '-'));
$arguments[] = static::parseArgument($token);
}
}

Expand All @@ -73,13 +71,7 @@ protected static function parameters(array $tokens)
*/
protected static function parseArgument($token)
{
if (Str::contains($token, ' : ')) {
list($token, $description) = explode(' : ', $token, 2);
$token = trim($token);
$description = trim($description);
} else {
$description = null;
}
list($token, $description) = static::parseToken($token);

switch (true) {
case Str::endsWith($token, '?*'):
Expand All @@ -103,13 +95,7 @@ protected static function parseArgument($token)
*/
protected static function parseOption($token)
{
if (Str::contains($token, ' : ')) {
list($token, $description) = explode(' : ', $token);
$token = trim($token);
$description = trim($description);
} else {
$description = null;
}
list($token, $description) = static::parseToken($token);

$matches = preg_split('/\s*\|\s*/', $token, 2);

Expand All @@ -131,4 +117,17 @@ protected static function parseOption($token)
return new InputOption($token, $shortcut, InputOption::VALUE_NONE, $description);
}
}

/**
* Parse the token.
*
* @param string $token
* @return array
*/
protected static function parseToken($token)
{
$parts = preg_split('/\s+:\s+/', trim($token), 2);

return count($parts) === 2 ? $parts : [$token, null];
}
}