1
|
|
|
<?php |
2
|
|
|
namespace rtens\domin\delivery\cli; |
3
|
|
|
|
4
|
|
|
use rtens\domin\Action; |
5
|
|
|
use rtens\domin\ActionRegistry; |
6
|
|
|
use rtens\domin\delivery\cli\fields\ArrayField; |
7
|
|
|
use rtens\domin\delivery\cli\fields\BooleanField; |
8
|
|
|
use rtens\domin\delivery\cli\fields\DateIntervalField; |
9
|
|
|
use rtens\domin\delivery\cli\fields\DateTimeField; |
10
|
|
|
use rtens\domin\delivery\cli\fields\EnumerationField; |
11
|
|
|
use rtens\domin\delivery\cli\fields\FileField; |
12
|
|
|
use rtens\domin\delivery\cli\fields\HtmlField; |
13
|
|
|
use rtens\domin\delivery\cli\fields\IdentifierField; |
14
|
|
|
use rtens\domin\delivery\cli\fields\MultiField; |
15
|
|
|
use rtens\domin\delivery\cli\fields\NullableField; |
16
|
|
|
use rtens\domin\delivery\cli\fields\ObjectField; |
17
|
|
|
use rtens\domin\delivery\cli\fields\PrimitiveField; |
18
|
|
|
use rtens\domin\delivery\cli\renderers\ArrayRenderer; |
19
|
|
|
use rtens\domin\delivery\cli\renderers\BooleanRenderer; |
20
|
|
|
use rtens\domin\delivery\cli\renderers\ChartRenderer; |
21
|
|
|
use rtens\domin\delivery\cli\renderers\DateIntervalRenderer; |
22
|
|
|
use rtens\domin\delivery\cli\renderers\DateTimeRenderer; |
23
|
|
|
use rtens\domin\delivery\cli\renderers\DelayedOutputRenderer; |
24
|
|
|
use rtens\domin\delivery\cli\renderers\FileRenderer; |
25
|
|
|
use rtens\domin\delivery\cli\renderers\HtmlRenderer; |
26
|
|
|
use rtens\domin\delivery\cli\renderers\IdentifierRenderer; |
27
|
|
|
use rtens\domin\delivery\cli\renderers\ObjectRenderer; |
28
|
|
|
use rtens\domin\delivery\cli\renderers\PrimitiveRenderer; |
29
|
|
|
use rtens\domin\delivery\cli\renderers\tables\DataTableRenderer; |
30
|
|
|
use rtens\domin\delivery\cli\renderers\tables\ObjectTableRenderer; |
31
|
|
|
use rtens\domin\delivery\cli\renderers\tables\TableRenderer; |
32
|
|
|
use rtens\domin\delivery\FieldRegistry; |
33
|
|
|
use rtens\domin\delivery\ParameterReader; |
34
|
|
|
use rtens\domin\delivery\RendererRegistry; |
35
|
|
|
use rtens\domin\execution\ExecutionResult; |
36
|
|
|
use rtens\domin\execution\FailedResult; |
37
|
|
|
use rtens\domin\execution\MissingParametersResult; |
38
|
|
|
use rtens\domin\execution\NoResult; |
39
|
|
|
use rtens\domin\execution\NotPermittedResult; |
40
|
|
|
use rtens\domin\execution\RedirectResult; |
41
|
|
|
use rtens\domin\execution\RenderedResult; |
42
|
|
|
use rtens\domin\Executor; |
43
|
|
|
use rtens\domin\reflection\CommentParser; |
44
|
|
|
use rtens\domin\reflection\types\TypeFactory; |
45
|
|
|
use watoki\factory\Factory; |
46
|
|
|
use watoki\reflect\ValuePrinter; |
47
|
|
|
|
48
|
|
|
class CliApplication { |
49
|
|
|
|
50
|
|
|
const OK = 0; |
51
|
|
|
const ERROR = 1; |
52
|
|
|
|
53
|
|
|
/** @var Factory */ |
54
|
|
|
public $factory; |
55
|
|
|
|
56
|
|
|
/** @var ActionRegistry */ |
57
|
|
|
public $actions; |
58
|
|
|
|
59
|
|
|
/** @var FieldRegistry */ |
60
|
|
|
public $fields; |
61
|
|
|
|
62
|
|
|
/** @var RendererRegistry */ |
63
|
|
|
public $renderers; |
64
|
|
|
|
65
|
|
|
/** @var TypeFactory */ |
66
|
|
|
public $types; |
67
|
|
|
|
68
|
|
|
/** @var CommentParser */ |
69
|
|
|
public $parser; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Factory $factory <- |
73
|
|
|
* @param ActionRegistry $actions <- |
74
|
|
|
* @param FieldRegistry $fields <- |
75
|
|
|
* @param RendererRegistry $renderers <- |
76
|
|
|
* @param TypeFactory $types <- |
77
|
|
|
* @param CommentParser $parser <- |
78
|
|
|
*/ |
79
|
|
|
public function __construct(Factory $factory, ActionRegistry $actions, FieldRegistry $fields, |
80
|
|
|
RendererRegistry $renderers, TypeFactory $types, CommentParser $parser) { |
81
|
|
|
$this->factory = $factory; |
82
|
|
|
|
83
|
|
|
$this->actions = $actions; |
84
|
|
|
$this->fields = $fields; |
85
|
|
|
$this->renderers = $renderers; |
86
|
|
|
$this->types = $types; |
87
|
|
|
$this->parser = $parser; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param callable $callback Receives the CliApplication instance |
92
|
|
|
* @param null|Factory $factory |
93
|
|
|
* @return Factory |
94
|
|
|
*/ |
95
|
|
|
public static function init(callable $callback, Factory $factory = null) { |
96
|
|
|
$factory = $factory ?: new Factory(); |
97
|
|
|
$callback($factory->setSingleton($factory->getInstance(self::class))); |
98
|
|
|
return $factory; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public static function run(Factory $factory, Console $console = null) { |
102
|
|
|
global $argv; |
|
|
|
|
103
|
|
|
|
104
|
|
|
/** @var self $app */ |
105
|
|
|
$app = $factory->getInstance(self::class); |
106
|
|
|
exit($app->doRun($console ?: new Console($argv))); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private function doRun(Console $console) { |
|
|
|
|
110
|
|
|
if ($console->getArguments()) { |
111
|
|
|
if ($console->getArguments()[0] == '!') { |
112
|
|
|
$actionId = $this->selectAction($console); |
113
|
|
|
$reader = new InteractiveCliParameterReader($this->fields, $console); |
114
|
|
|
|
115
|
|
|
$this->printActionHeader($console, $actionId); |
116
|
|
|
} else { |
117
|
|
|
$actionId = $console->getArguments()[0]; |
118
|
|
|
$reader = new CliParameterReader($console); |
119
|
|
|
} |
120
|
|
|
} else { |
121
|
|
|
$this->printUsage($console); |
122
|
|
|
$this->printActions($console); |
123
|
|
|
return self::OK; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$this->registerFields($reader); |
127
|
|
|
$this->registerRenderers(); |
128
|
|
|
|
129
|
|
|
$executor = new Executor($this->actions, $this->fields, $this->renderers, $reader); |
130
|
|
|
return $this->printResult($console, $executor->execute($actionId)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
private function printResult(Console $console, ExecutionResult $result) { |
134
|
|
|
if ($result instanceof RenderedResult) { |
135
|
|
|
$console->writeLine((string)$result->getOutput()); |
136
|
|
|
return self::OK; |
137
|
|
|
} else if ($result instanceof MissingParametersResult) { |
138
|
|
|
$console->writeLine("Missing parameters " . ValuePrinter::serialize($result->getParameters())); |
139
|
|
|
return self::ERROR; |
140
|
|
|
} else if ($result instanceof NotPermittedResult) { |
141
|
|
|
$console->writeLine('Permission denied'); |
142
|
|
|
return self::ERROR; |
143
|
|
|
} else if ($result instanceof FailedResult) { |
144
|
|
|
$console->writeLine("Error: " . $result->getMessage()); |
145
|
|
|
|
146
|
|
|
$exception = $result->getException(); |
147
|
|
|
$console->error( |
148
|
|
|
$exception->getMessage() . ' ' . |
149
|
|
|
'[' . $exception->getFile() . ':' . $exception->getLine() . ']' . "\n" . |
150
|
|
|
$exception->getTraceAsString() |
151
|
|
|
); |
152
|
|
|
return $exception->getCode() ?: self::ERROR; |
153
|
|
|
} else if ($result instanceof NoResult || $result instanceof RedirectResult) { |
154
|
|
|
return self::OK; |
155
|
|
|
} else { |
156
|
|
|
$console->writeLine('Cannot print [' . (new \ReflectionClass($result))->getShortName() . ']'); |
157
|
|
|
return self::OK; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
private function selectAction(Console $console) { |
162
|
|
|
$console->writeLine(); |
163
|
|
|
$console->writeLine('Available Actions'); |
164
|
|
|
$console->writeLine('~~~~~~~~~~~~~~~~~'); |
165
|
|
|
|
166
|
|
|
$i = 1; |
167
|
|
|
$actionIds = []; |
168
|
|
View Code Duplication |
foreach ($this->actions->getAllActions() as $id => $action) { |
|
|
|
|
169
|
|
|
$console->writeLine($i++ . " - " . $action->caption() . $this->shortDescription($action)); |
170
|
|
|
$actionIds[] = $id; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$console->writeLine(); |
174
|
|
|
$actionIndex = $console->read('Action: '); |
175
|
|
|
|
176
|
|
|
return $actionIds[$actionIndex - 1]; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
private function printActionHeader(Console $console, $actionId) { |
180
|
|
|
$action = $this->actions->getAction($actionId); |
181
|
|
|
$console->writeLine(); |
182
|
|
|
$console->writeLine($action->caption()); |
183
|
|
|
$console->writeLine(str_repeat('~', strlen($action->caption()))); |
184
|
|
|
$console->writeLine(); |
185
|
|
|
|
186
|
|
|
if ($action->description()) { |
|
|
|
|
187
|
|
|
$console->writeLine($action->description()); |
188
|
|
|
$console->writeLine(); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
private function printUsage(Console $console) { |
193
|
|
|
$console->writeLine(); |
194
|
|
|
|
195
|
|
|
$console->writeLine("Interactive mode: php {$console->getScriptName()} !"); |
196
|
|
|
$console->writeLine("Execute Action: php {$console->getScriptName()} <actionId> --<parameterName> <parameterValue> ..."); |
197
|
|
|
$console->writeLine(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
private function printActions(Console $console) { |
201
|
|
|
$console->writeLine('Available Actions'); |
202
|
|
|
$console->writeLine('~~~~~~~~~~~~~~~~~'); |
203
|
|
|
|
204
|
|
View Code Duplication |
foreach ($this->actions->getAllActions() as $id => $action) { |
|
|
|
|
205
|
|
|
$console->writeLine($id . ' - ' . $action->caption() . $this->shortDescription($action)); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
private function registerFields(ParameterReader $reader) { |
210
|
|
|
$this->fields->add(new PrimitiveField()); |
211
|
|
|
$this->fields->add(new BooleanField()); |
212
|
|
|
$this->fields->add(new FileField()); |
213
|
|
|
$this->fields->add(new HtmlField($reader)); |
214
|
|
|
$this->fields->add(new DateTimeField()); |
215
|
|
|
$this->fields->add(new DateIntervalField()); |
216
|
|
|
$this->fields->add(new ArrayField($this->fields, $reader)); |
217
|
|
|
$this->fields->add(new NullableField($this->fields, $reader)); |
218
|
|
|
$this->fields->add(new ObjectField($this->types, $this->fields, $reader)); |
219
|
|
|
$this->fields->add(new MultiField($this->fields, $reader)); |
220
|
|
|
$this->fields->add(new IdentifierField($this->fields)); |
221
|
|
|
$this->fields->add(new EnumerationField($this->fields)); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
private function registerRenderers() { |
225
|
|
|
$this->renderers->add(new BooleanRenderer()); |
226
|
|
|
$this->renderers->add(new PrimitiveRenderer()); |
227
|
|
|
$this->renderers->add(new DateTimeRenderer()); |
228
|
|
|
$this->renderers->add(new DateIntervalRenderer()); |
229
|
|
|
$this->renderers->add(new HtmlRenderer()); |
230
|
|
|
$this->renderers->add(new IdentifierRenderer()); |
231
|
|
|
$this->renderers->add(new FileRenderer('')); |
232
|
|
|
$this->renderers->add(new DelayedOutputRenderer()); |
233
|
|
|
$this->renderers->add(new ObjectTableRenderer($this->renderers)); |
234
|
|
|
$this->renderers->add(new DataTableRenderer($this->renderers)); |
235
|
|
|
$this->renderers->add(new TableRenderer($this->renderers)); |
236
|
|
|
$this->renderers->add(new ChartRenderer($this->renderers)); |
237
|
|
|
$this->renderers->add(new ArrayRenderer($this->renderers)); |
238
|
|
|
$this->renderers->add(new ObjectRenderer($this->renderers, $this->types)); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
private function shortDescription(Action $action) { |
242
|
|
|
$description = $this->parser->shorten($action->description()); |
243
|
|
|
return $description ? " ($description)" : ''; |
244
|
|
|
} |
245
|
|
|
} |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state