1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Shopware\Psh\Application; |
4
|
|
|
|
5
|
|
|
use function array_merge; |
6
|
|
|
use function array_slice; |
7
|
|
|
use function array_splice; |
8
|
|
|
use function array_unique; |
9
|
|
|
use function count; |
10
|
|
|
use function explode; |
11
|
|
|
use function in_array; |
12
|
|
|
use function mb_strpos; |
13
|
|
|
use function mb_substr; |
14
|
|
|
use function sprintf; |
15
|
|
|
use function str_replace; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* ./psh --no-header unit --filter once |
19
|
|
|
*/ |
20
|
|
|
class ParameterParser |
21
|
|
|
{ |
22
|
|
|
public function parseAllParams(array $params): RuntimeParameters |
23
|
|
|
{ |
24
|
|
|
if (count($params) === 0) { |
25
|
|
|
return new RuntimeParameters( |
26
|
|
|
[], |
27
|
|
|
[], |
28
|
|
|
[] |
29
|
|
|
); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$params = array_splice($params, 1); |
33
|
|
|
$commandsAreAt = 0; |
34
|
|
|
|
35
|
|
|
$appOptions = []; |
36
|
|
|
foreach ($params as $commandsAreAt => $param) { |
37
|
|
|
if (!in_array($param, ApplicationOptions::getAllFlags(), true)) { |
38
|
|
|
break; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$appOptions[] = $param; |
42
|
|
|
$commandsAreAt++; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$scriptNames = [[], $this->explodeScriptNames($params, $commandsAreAt)]; |
46
|
|
|
$overwrites = [[], $this->extractParams(array_slice($params, $commandsAreAt + 1))]; |
47
|
|
|
|
48
|
|
|
return new RuntimeParameters( |
49
|
|
|
array_unique($appOptions), |
50
|
|
|
array_merge(...$scriptNames), |
51
|
|
|
array_merge(...$overwrites) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function extractParams(array $params): array |
56
|
|
|
{ |
57
|
|
|
$reformattedParams = []; |
58
|
|
|
$paramsCount = count($params); |
59
|
|
|
for ($i = 0; $i < $paramsCount; $i++) { |
60
|
|
|
$key = $params[$i]; |
61
|
|
|
|
62
|
|
|
$this->testParameterFormat($key); |
63
|
|
|
|
64
|
|
|
if ($this->isKeyValuePair($key)) { |
65
|
|
|
list($key, $value) = explode('=', $key, 2); |
66
|
|
|
|
67
|
|
|
if ($this->isEnclosedInAmpersand($value)) { |
68
|
|
|
$value = mb_substr($value, 1, -1); |
69
|
|
|
} |
70
|
|
|
} else { |
71
|
|
|
$i++; |
72
|
|
|
$value = $params[$i]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$key = str_replace('--', '', $key); |
76
|
|
|
$reformattedParams[$key] = $value; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $reformattedParams; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function testParameterFormat(string $key): void |
83
|
|
|
{ |
84
|
|
|
if (mb_strpos($key, '--') !== 0) { |
85
|
|
|
throw new InvalidParameter( |
86
|
|
|
sprintf('Unable to parse parameter "%s". Use -- for correct usage', $key) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function isKeyValuePair(string $key): bool |
92
|
|
|
{ |
93
|
|
|
return mb_strpos($key, '=') !== false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
private function isEnclosedInAmpersand(string $value): bool |
97
|
|
|
{ |
98
|
|
|
return mb_strpos($value, '"') === 0; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
private function explodeScriptNames(array $params, int $position): array |
102
|
|
|
{ |
103
|
|
|
if (!isset($params[$position])) { |
104
|
|
|
return []; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return explode(',', $params[$position]); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|