1 |
cb15593b
|
Cajova-Houba
|
<?php
|
2 |
|
|
|
3 |
|
|
namespace Illuminate\Console;
|
4 |
|
|
|
5 |
|
|
use Illuminate\Support\Str;
|
6 |
|
|
use InvalidArgumentException;
|
7 |
|
|
use Symfony\Component\Console\Input\InputOption;
|
8 |
|
|
use Symfony\Component\Console\Input\InputArgument;
|
9 |
|
|
|
10 |
|
|
class Parser
|
11 |
|
|
{
|
12 |
|
|
/**
|
13 |
|
|
* Parse the given console command definition into an array.
|
14 |
|
|
*
|
15 |
|
|
* @param string $expression
|
16 |
|
|
* @return array
|
17 |
|
|
*
|
18 |
|
|
* @throws \InvalidArgumentException
|
19 |
|
|
*/
|
20 |
|
|
public static function parse($expression)
|
21 |
|
|
{
|
22 |
|
|
if (trim($expression) === '') {
|
23 |
|
|
throw new InvalidArgumentException('Console command definition is empty.');
|
24 |
|
|
}
|
25 |
|
|
|
26 |
|
|
preg_match('/[^\s]+/', $expression, $matches);
|
27 |
|
|
|
28 |
|
|
if (isset($matches[0])) {
|
29 |
|
|
$name = $matches[0];
|
30 |
|
|
} else {
|
31 |
|
|
throw new InvalidArgumentException('Unable to determine command name from signature.');
|
32 |
|
|
}
|
33 |
|
|
|
34 |
|
|
preg_match_all('/\{\s*(.*?)\s*\}/', $expression, $matches);
|
35 |
|
|
|
36 |
|
|
$tokens = isset($matches[1]) ? $matches[1] : [];
|
37 |
|
|
|
38 |
|
|
if (count($tokens)) {
|
39 |
|
|
return array_merge([$name], static::parameters($tokens));
|
40 |
|
|
}
|
41 |
|
|
|
42 |
|
|
return [$name, [], []];
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
/**
|
46 |
|
|
* Extract all of the parameters from the tokens.
|
47 |
|
|
*
|
48 |
|
|
* @param array $tokens
|
49 |
|
|
* @return array
|
50 |
|
|
*/
|
51 |
|
|
protected static function parameters(array $tokens)
|
52 |
|
|
{
|
53 |
|
|
$arguments = [];
|
54 |
|
|
|
55 |
|
|
$options = [];
|
56 |
|
|
|
57 |
|
|
foreach ($tokens as $token) {
|
58 |
|
|
if (! Str::startsWith($token, '--')) {
|
59 |
|
|
$arguments[] = static::parseArgument($token);
|
60 |
|
|
} else {
|
61 |
|
|
$options[] = static::parseOption(ltrim($token, '-'));
|
62 |
|
|
}
|
63 |
|
|
}
|
64 |
|
|
|
65 |
|
|
return [$arguments, $options];
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
/**
|
69 |
|
|
* Parse an argument expression.
|
70 |
|
|
*
|
71 |
|
|
* @param string $token
|
72 |
|
|
* @return \Symfony\Component\Console\Input\InputArgument
|
73 |
|
|
*/
|
74 |
|
|
protected static function parseArgument($token)
|
75 |
|
|
{
|
76 |
|
|
$description = null;
|
77 |
|
|
|
78 |
|
|
if (Str::contains($token, ' : ')) {
|
79 |
|
|
list($token, $description) = explode(' : ', $token, 2);
|
80 |
|
|
|
81 |
|
|
$token = trim($token);
|
82 |
|
|
|
83 |
|
|
$description = trim($description);
|
84 |
|
|
}
|
85 |
|
|
|
86 |
|
|
switch (true) {
|
87 |
|
|
case Str::endsWith($token, '?*'):
|
88 |
|
|
return new InputArgument(trim($token, '?*'), InputArgument::IS_ARRAY, $description);
|
89 |
|
|
case Str::endsWith($token, '*'):
|
90 |
|
|
return new InputArgument(trim($token, '*'), InputArgument::IS_ARRAY | InputArgument::REQUIRED, $description);
|
91 |
|
|
case Str::endsWith($token, '?'):
|
92 |
|
|
return new InputArgument(trim($token, '?'), InputArgument::OPTIONAL, $description);
|
93 |
|
|
case preg_match('/(.+)\=(.+)/', $token, $matches):
|
94 |
|
|
return new InputArgument($matches[1], InputArgument::OPTIONAL, $description, $matches[2]);
|
95 |
|
|
default:
|
96 |
|
|
return new InputArgument($token, InputArgument::REQUIRED, $description);
|
97 |
|
|
}
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
/**
|
101 |
|
|
* Parse an option expression.
|
102 |
|
|
*
|
103 |
|
|
* @param string $token
|
104 |
|
|
* @return \Symfony\Component\Console\Input\InputOption
|
105 |
|
|
*/
|
106 |
|
|
protected static function parseOption($token)
|
107 |
|
|
{
|
108 |
|
|
$description = null;
|
109 |
|
|
|
110 |
|
|
if (Str::contains($token, ' : ')) {
|
111 |
|
|
list($token, $description) = explode(' : ', $token);
|
112 |
|
|
$token = trim($token);
|
113 |
|
|
$description = trim($description);
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
$shortcut = null;
|
117 |
|
|
|
118 |
|
|
$matches = preg_split('/\s*\|\s*/', $token, 2);
|
119 |
|
|
|
120 |
|
|
if (isset($matches[1])) {
|
121 |
|
|
$shortcut = $matches[0];
|
122 |
|
|
$token = $matches[1];
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
switch (true) {
|
126 |
|
|
case Str::endsWith($token, '='):
|
127 |
|
|
return new InputOption(trim($token, '='), $shortcut, InputOption::VALUE_OPTIONAL, $description);
|
128 |
|
|
case Str::endsWith($token, '=*'):
|
129 |
|
|
return new InputOption(trim($token, '=*'), $shortcut, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, $description);
|
130 |
|
|
case preg_match('/(.+)\=(.+)/', $token, $matches):
|
131 |
|
|
return new InputOption($matches[1], $shortcut, InputOption::VALUE_OPTIONAL, $description, $matches[2]);
|
132 |
|
|
default:
|
133 |
|
|
return new InputOption($token, $shortcut, InputOption::VALUE_NONE, $description);
|
134 |
|
|
}
|
135 |
|
|
}
|
136 |
|
|
}
|