-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCliRequest.php
More file actions
208 lines (167 loc) · 6.4 KB
/
CliRequest.php
File metadata and controls
208 lines (167 loc) · 6.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
declare(strict_types=1);
namespace MagicPush\CliToolkit\Parametizer\CliRequest;
use LogicException;
use MagicPush\CliToolkit\Parametizer\Config\Config;
use MagicPush\CliToolkit\Parametizer\HelpFormatter;
class CliRequest {
public const string SUBCOMMAND_PREFIX = '!';
/**
* @param mixed[] $params
*/
public function __construct(
public readonly Config $config,
protected readonly array $params,
public readonly ?self $parent = null,
) {
}
/**
* @return mixed[]
*/
public function getParams(): array {
return $this->params;
}
public function getParam(string $paramName): mixed {
if (!array_key_exists($paramName, $this->params)) {
throw new LogicException(
"Parameter '" . HelpFormatter::createForStdErr()->paramTitle($paramName) . "' not found in the request."
. ' The parameters being parsed: ' . implode(', ', array_keys($this->params)),
);
}
return $this->params[$paramName];
}
/**
* Returns a subcommand name specified in a subcommand switch parameter,
* or `null` if there is no subcommand switch.
*/
public function getRequestedSubcommandName(): ?string {
$subcommandSwitchName = $this->config->getSubcommandSwitchName();
if (null === $subcommandSwitchName) {
return null;
}
return $this->getParamAsString($subcommandSwitchName);
}
/**
* Returns a subcommand request by a name specified in a subcommand switch parameter
* or `null` if there is no subcommand switch.
*/
public function getSubcommandRequest(): ?static {
$subcommandName = $this->getRequestedSubcommandName();
if (null === $subcommandName) {
return null;
}
$subcommandConfig = $this->config->getBranch($subcommandName);
if (null === $subcommandConfig) {
throw new LogicException(
"Subcommand '" . HelpFormatter::createForStdErr()->paramTitle($subcommandName) . "' not found",
);
}
$subcommandParameterValues = $this->getParam(static::SUBCOMMAND_PREFIX . $subcommandName);
self::validateValueIsArray(
$this->config->getSubcommandSwitchName() . ' -> ' . $subcommandName,
$subcommandParameterValues,
);
return new static($subcommandConfig, $subcommandParameterValues, $this);
}
public function executeBuiltInSubcommandIfRequested(): void {
$innermostSubcommandName = null;
$innermostSubcommandParentRequest = null;
$innermostSubcommandRequest = $this;
while ($subcommandName = $innermostSubcommandRequest->getRequestedSubcommandName()) {
$innermostSubcommandName = $subcommandName;
$innermostSubcommandParentRequest = $innermostSubcommandRequest;
$innermostSubcommandRequest = $innermostSubcommandParentRequest->getSubcommandRequest();
}
// No parent request means there was no subcommand request.
if (null === $innermostSubcommandParentRequest) {
return;
}
$builtInSubcommandClass = $innermostSubcommandParentRequest->config
->getBuiltInSubcommandClass($innermostSubcommandName);
if (null === $builtInSubcommandClass) {
return;
}
(new $builtInSubcommandClass($innermostSubcommandRequest))->execute();
// For a built-in subcommand we should not execute any code outside of that subcommand.
// So, for instance, a subcommand launcher script will not try to execute a built-in subcommand twice.
exit;
}
private static function validateValueIsArray(string $paramName, mixed $paramValue): void {
if (is_array($paramValue)) {
return;
}
throw new LogicException(
"Parameter '" . HelpFormatter::createForStdErr()->paramTitle($paramName) . "' contains a single value",
);
}
private static function validateValueNotArray(string $paramName, mixed $paramValue): void {
if (!is_array($paramValue)) {
return;
}
throw new LogicException(
"Parameter '" . HelpFormatter::createForStdErr()->paramTitle($paramName) . "' contains an array",
);
}
public function getParamAsBool(string $paramName): bool {
$paramValue = $this->getParam($paramName);
self::validateValueNotArray($paramName, $paramValue);
return (bool) $paramValue;
}
public function getParamAsInt(string $paramName): int {
$paramValue = $this->getParam($paramName);
self::validateValueNotArray($paramName, $paramValue);
return (int) $paramValue;
}
/**
* @return int[]
*/
public function getParamAsIntList(string $paramName): array {
$paramValue = $this->getParam($paramName);
self::validateValueIsArray($paramName, $paramValue);
array_walk(
$paramValue,
function (&$elementValue) {
$elementValue = (int) $elementValue;
},
);
return $paramValue;
}
public function getParamAsFloat(string $paramName): float {
$paramValue = $this->getParam($paramName);
self::validateValueNotArray($paramName, $paramValue);
return (float) $paramValue;
}
/**
* @return float[]
*/
public function getParamAsFloatList(string $paramName): array {
$paramValue = $this->getParam($paramName);
self::validateValueIsArray($paramName, $paramValue);
array_walk(
$paramValue,
function (&$elementValue) {
$elementValue = (float) $elementValue;
},
);
return $paramValue;
}
public function getParamAsString(string $paramName): string {
$paramValue = $this->getParam($paramName);
self::validateValueNotArray($paramName, $paramValue);
return (string) $paramValue;
}
/**
* @return string[]
*/
public function getParamAsStringList(string $paramName): array {
$paramValue = $this->getParam($paramName);
self::validateValueIsArray($paramName, $paramValue);
array_walk(
$paramValue,
function (&$elementValue) {
$elementValue = (string) $elementValue;
},
);
return $paramValue;
}
}