1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Parser.php |
4
|
|
|
* |
5
|
|
|
* PHP version 7.3 and up. |
6
|
|
|
* |
7
|
|
|
* @category Core |
8
|
|
|
* @package Redbox_Cli |
9
|
|
|
* @author Johnny Mast <[email protected]> |
10
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
11
|
|
|
* @link https://github.com/johnnymast/redbox-cli |
12
|
|
|
* @since 1.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Redbox\Cli\Arguments; |
16
|
|
|
|
17
|
|
|
use Exception; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* This class will parse the given arguments. |
21
|
|
|
* |
22
|
|
|
* @category Core |
23
|
|
|
* @package Redbox_Cli |
24
|
|
|
* @author Johnny Mast <[email protected]> |
25
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
26
|
|
|
* @link https://github.com/johnnymast/redbox-cli |
27
|
|
|
* @since 1.0 |
28
|
|
|
*/ |
29
|
|
|
class Parser |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* The argument filter. |
33
|
|
|
* |
34
|
|
|
* @var \Redbox\Cli\Arguments\Filter |
35
|
|
|
*/ |
36
|
|
|
protected $filter; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* The argument manager. |
40
|
|
|
* |
41
|
|
|
* @var \Redbox\Cli\Arguments\Manager |
42
|
|
|
*/ |
43
|
|
|
protected $manager; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Parser constructor. |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $arguments; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Parser constructor. |
54
|
|
|
* |
55
|
|
|
* @param Manager $manager The argument manager. |
56
|
|
|
*/ |
57
|
13 |
|
public function __construct(Manager $manager) |
58
|
|
|
{ |
59
|
13 |
|
$this->manager = $manager; |
60
|
13 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Set the filter for the parser. |
64
|
|
|
* |
65
|
|
|
* @param Filter $filter The argument filter. |
66
|
|
|
* @param array $arguments The array containing the arguments. |
67
|
|
|
* |
68
|
|
|
* @return void |
69
|
|
|
*/ |
70
|
9 |
|
public function setFilter(Filter $filter, array $arguments = []): void |
71
|
|
|
{ |
72
|
9 |
|
$this->filter = $filter; |
73
|
9 |
|
$this->arguments = $arguments; |
74
|
9 |
|
$this->filter->setArguments($arguments); |
75
|
9 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Return the script name. |
79
|
|
|
* |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
2 |
|
public function getCommand(): string |
83
|
|
|
{ |
84
|
2 |
|
global $argv; |
85
|
2 |
|
return $argv[0]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* This is it, we parse the given arguments. |
90
|
|
|
* |
91
|
|
|
* @return void |
92
|
|
|
* @throws \Exception |
93
|
|
|
*/ |
94
|
9 |
|
public function parse(): void |
95
|
|
|
{ |
96
|
9 |
|
list($shortOptions, $longOptions) = $this->_buildOptions(); |
97
|
9 |
|
$results = getopt($shortOptions, $longOptions); |
98
|
|
|
|
99
|
9 |
|
foreach ($this->arguments as $argument) { |
100
|
9 |
|
$name = $argument->name; |
101
|
9 |
|
$value = ''; |
102
|
|
|
|
103
|
9 |
|
if (isset($results[$argument->prefix]) |
104
|
9 |
|
|| isset($results[$argument->longPrefix]) |
105
|
|
|
) { |
106
|
|
|
// @codingStandardsIgnoreStart |
107
|
2 |
|
$value = $results[$argument->prefix] ?? $results[$argument->longPrefix]; |
108
|
|
|
// @codingStandardsIgnoreEnd |
109
|
|
|
} else { |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* If we set the default value for this argument we also add it to |
113
|
|
|
* the result array or it will fail the argument has the option |
114
|
|
|
* required by mistake. |
115
|
|
|
*/ |
116
|
7 |
|
if ($argument->defaultValue) { |
117
|
5 |
|
$value = $argument->defaultValue; |
118
|
5 |
|
$this->manager->setHasDefaultValue($name, true); |
119
|
5 |
|
$results[$argument->name] = $value; |
120
|
|
|
} else { |
121
|
3 |
|
if ($argument->required === true) { |
122
|
1 |
|
throw new Exception( |
123
|
|
|
'The following arguments are required: ' |
124
|
1 |
|
. print_r($argument->name, true) . '.' |
|
|
|
|
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
8 |
|
$this->manager->set($name, $value); |
130
|
|
|
} |
131
|
8 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Build the option arrays that could be used with getopt() |
135
|
|
|
* |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
9 |
|
private function _buildOptions(): array |
139
|
|
|
{ |
140
|
9 |
|
$short_prefixes = $this->filter->withShortPrefix(); |
141
|
9 |
|
$long_prefixes = $this->filter->withLongPrefix(); |
142
|
|
|
|
143
|
9 |
|
$short = ''; |
144
|
9 |
|
$long = array(); |
145
|
|
|
|
146
|
9 |
|
foreach ($short_prefixes as $argument) { |
147
|
9 |
|
$short .= $argument->prefix; |
148
|
9 |
|
$short .= ($argument->required == true) ? ':' : '::'; |
149
|
|
|
} |
150
|
|
|
|
151
|
9 |
|
foreach ($long_prefixes as $argument) { |
152
|
9 |
|
$rule = $argument->longPrefix; |
153
|
9 |
|
if (strlen($rule) > 0) { |
154
|
6 |
|
$rule .= ($argument->required == true) ? ':' : '::'; |
155
|
6 |
|
$long[] = $rule; |
156
|
|
|
} |
157
|
|
|
} |
158
|
9 |
|
return [$short, $long]; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|