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
|
3 |
|
public static function run(App $app): void { |
14
|
3 |
|
global $argv; |
15
|
|
|
|
16
|
|
|
try { |
17
|
3 |
|
$cmd = self::parse($app, $argv); |
18
|
|
|
|
19
|
3 |
|
if ($cmd !== null) { |
20
|
|
|
|
21
|
3 |
|
if ($cmd->handleOptionParseException()) { |
22
|
|
|
return; |
23
|
|
|
} |
24
|
|
|
|
25
|
3 |
|
if (self::callCallable($cmd)) { |
26
|
1 |
|
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
|
3 |
|
private static function callCallable(Cmd $cmd): bool { |
51
|
|
|
|
52
|
3 |
|
$callable = $cmd->getCallable(); |
53
|
|
|
|
54
|
3 |
|
if ($callable !== null) { |
55
|
2 |
|
$callable($cmd); |
56
|
1 |
|
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
|
18 |
|
public static function parse(App $app, array $argv): Cmd { |
86
|
|
|
|
87
|
18 |
|
$cmd = $app->setup(); |
88
|
|
|
|
89
|
18 |
|
$appspecs = $cmd->getOptionCollection(); |
90
|
|
|
|
91
|
18 |
|
$parser = new ContinuousOptionParser($appspecs); |
92
|
|
|
|
93
|
|
|
try { |
94
|
18 |
|
$cmd->optionResult = $parser->parse($argv); |
95
|
|
|
|
96
|
|
|
} catch (Exception $e) { |
97
|
|
|
|
98
|
|
|
$cmd->optionParseException = $e; |
99
|
|
|
|
100
|
|
|
return $cmd; |
101
|
|
|
} |
102
|
|
|
|
103
|
18 |
|
while (!$parser->isEnd()) { |
104
|
|
|
|
105
|
15 |
|
$currentArgument = $parser->getCurrentArgument(); |
106
|
|
|
|
107
|
15 |
|
$subcommand = self::getSubcommand($currentArgument, $cmd); |
108
|
|
|
|
109
|
15 |
|
if ($subcommand !== null) { |
110
|
|
|
|
111
|
9 |
|
$cmd = $subcommand; |
112
|
|
|
|
113
|
|
|
try { |
114
|
9 |
|
self::parseSubcommand($parser, $cmd); |
115
|
1 |
|
} catch (Exception $e) { |
116
|
1 |
|
$cmd->optionParseException = $e; |
117
|
9 |
|
return $cmd; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} else { |
121
|
7 |
|
$cmd->arguments[] = $parser->advance(); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
17 |
|
return $cmd; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $argument |
130
|
|
|
* @param Cmd $cmd |
131
|
|
|
* @return Cmd|null |
132
|
|
|
*/ |
133
|
15 |
|
private static function getSubcommand(string $argument, Cmd $cmd): ?Cmd { |
134
|
|
|
|
135
|
15 |
|
$subcommand = null; |
136
|
15 |
|
if ($cmd->hasSubCmd($argument)) { |
137
|
9 |
|
$subcommand = $cmd->getSubCmd($argument); |
138
|
|
|
} |
139
|
|
|
|
140
|
15 |
|
return $subcommand; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @param ContinuousOptionParser $parser |
145
|
|
|
* @param Cmd $cmd |
146
|
|
|
* @throws Exception |
147
|
|
|
*/ |
148
|
9 |
|
private static function parseSubcommand(ContinuousOptionParser $parser, Cmd $cmd): void { |
149
|
|
|
|
150
|
9 |
|
$parser->advance(); |
151
|
|
|
|
152
|
9 |
|
$cmd->optionResult = new OptionResult(); |
153
|
|
|
|
154
|
9 |
|
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
|
8 |
|
} |
163
|
|
|
|
164
|
|
|
|
165
|
|
|
} |