|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Stefaminator\Cli; |
|
5
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
8
|
|
|
use GetOptionKit\ContinuousOptionParser; |
|
9
|
|
|
use GetOptionKit\OptionResult; |
|
10
|
|
|
|
|
11
|
|
|
class AppParser { |
|
12
|
|
|
|
|
13
|
2 |
|
public static function run(App $app): void { |
|
14
|
2 |
|
global $argv; |
|
15
|
|
|
|
|
16
|
|
|
try { |
|
17
|
2 |
|
$cmd = self::parse($app, $argv); |
|
18
|
|
|
|
|
19
|
2 |
|
if ($cmd !== null) { |
|
20
|
|
|
|
|
21
|
2 |
|
if ($cmd->handleOptionParseException()) { |
|
22
|
|
|
return; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
2 |
|
if (self::callCallable($cmd)) { |
|
26
|
|
|
return; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
1 |
|
if (self::callMethod($app, $cmd)) { |
|
30
|
1 |
|
return; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
} |
|
34
|
1 |
|
} catch (Exception $e) { |
|
35
|
|
|
|
|
36
|
1 |
|
App::eol(); |
|
37
|
1 |
|
App::echo('Uups, someting went wrong!', Color::FOREGROUND_COLOR_RED); |
|
38
|
1 |
|
App::eol(); |
|
39
|
1 |
|
App::echo($e->getMessage(), Color::FOREGROUND_COLOR_RED); |
|
40
|
1 |
|
App::eol(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param Cmd $cmd |
|
47
|
|
|
* @return bool |
|
48
|
|
|
* @throws Exception |
|
49
|
|
|
*/ |
|
50
|
2 |
|
private static function callCallable(Cmd $cmd): bool { |
|
51
|
|
|
|
|
52
|
2 |
|
$callable = $cmd->getCallable(); |
|
53
|
|
|
|
|
54
|
2 |
|
if ($callable !== null) { |
|
55
|
1 |
|
$callable($cmd); |
|
56
|
|
|
return true; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param App $app |
|
64
|
|
|
* @param Cmd $cmd |
|
65
|
|
|
* @return bool |
|
66
|
|
|
* @throws Exception |
|
67
|
|
|
*/ |
|
68
|
1 |
|
private static function callMethod(App $app, Cmd $cmd): bool { |
|
69
|
|
|
|
|
70
|
1 |
|
$methodName = $cmd->getMethodName(); |
|
71
|
|
|
|
|
72
|
1 |
|
if (method_exists($app, $methodName)) { |
|
73
|
1 |
|
$app->$methodName($cmd); |
|
74
|
1 |
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return false; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param App $app |
|
82
|
|
|
* @param array $argv |
|
83
|
|
|
* @return Cmd |
|
84
|
|
|
*/ |
|
85
|
17 |
|
public static function parse(App $app, array $argv): Cmd { |
|
86
|
|
|
|
|
87
|
17 |
|
$cmd = $app->setup(); |
|
88
|
|
|
|
|
89
|
17 |
|
$appspecs = $cmd->getOptionCollection(); |
|
90
|
|
|
|
|
91
|
17 |
|
$parser = new ContinuousOptionParser($appspecs); |
|
92
|
|
|
|
|
93
|
|
|
try { |
|
94
|
17 |
|
$cmd->optionResult = $parser->parse($argv); |
|
95
|
|
|
|
|
96
|
|
|
} catch (Exception $e) { |
|
97
|
|
|
|
|
98
|
|
|
$cmd->optionParseException = $e; |
|
99
|
|
|
|
|
100
|
|
|
return $cmd; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
17 |
|
while (!$parser->isEnd()) { |
|
104
|
|
|
|
|
105
|
14 |
|
$currentArgument = $parser->getCurrentArgument(); |
|
106
|
|
|
|
|
107
|
14 |
|
$subcommand = self::getSubcommand($currentArgument, $cmd); |
|
108
|
|
|
|
|
109
|
14 |
|
if ($subcommand !== null) { |
|
110
|
|
|
|
|
111
|
8 |
|
$cmd = $subcommand; |
|
112
|
|
|
|
|
113
|
|
|
try { |
|
114
|
8 |
|
self::parseSubcommand($parser, $cmd); |
|
115
|
1 |
|
} catch (Exception $e) { |
|
116
|
1 |
|
$cmd->optionParseException = $e; |
|
117
|
8 |
|
return $cmd; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
} else { |
|
121
|
7 |
|
$cmd->arguments[] = $parser->advance(); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
16 |
|
return $cmd; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param string $argument |
|
130
|
|
|
* @param Cmd $cmd |
|
131
|
|
|
* @return Cmd|null |
|
132
|
|
|
*/ |
|
133
|
14 |
|
private static function getSubcommand(string $argument, Cmd $cmd): ?Cmd { |
|
134
|
|
|
|
|
135
|
14 |
|
$subcommand = null; |
|
136
|
14 |
|
if ($cmd->hasSubCmd($argument)) { |
|
137
|
8 |
|
$subcommand = $cmd->getSubCmd($argument); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
14 |
|
return $subcommand; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param ContinuousOptionParser $parser |
|
145
|
|
|
* @param Cmd $cmd |
|
146
|
|
|
* @throws Exception |
|
147
|
|
|
*/ |
|
148
|
8 |
|
private static function parseSubcommand(ContinuousOptionParser $parser, Cmd $cmd): void { |
|
149
|
|
|
|
|
150
|
8 |
|
$parser->advance(); |
|
151
|
|
|
|
|
152
|
8 |
|
$cmd->optionResult = new OptionResult(); |
|
153
|
|
|
|
|
154
|
8 |
|
if (!empty($cmd->optionSpecs)) { |
|
155
|
|
|
|
|
156
|
5 |
|
$specs = $cmd->getOptionCollection(); |
|
157
|
|
|
|
|
158
|
5 |
|
$parser->setSpecs($specs); |
|
159
|
|
|
|
|
160
|
5 |
|
$cmd->optionResult = $parser->continueParse(); |
|
161
|
|
|
} |
|
162
|
7 |
|
} |
|
163
|
|
|
|
|
164
|
|
|
|
|
165
|
|
|
} |