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