1 | <?php |
||||
2 | /** |
||||
3 | * This file is part of the O2System Framework package. |
||||
4 | * |
||||
5 | * For the full copyright and license information, please view the LICENSE |
||||
6 | * file that was distributed with this source code. |
||||
7 | * |
||||
8 | * @author Steeve Andrian Salim |
||||
9 | * @copyright Copyright (c) Steeve Andrian Salim |
||||
10 | */ |
||||
11 | |||||
12 | // ------------------------------------------------------------------------ |
||||
13 | |||||
14 | namespace O2System\Kernel\Cli; |
||||
15 | |||||
16 | // ------------------------------------------------------------------------ |
||||
17 | |||||
18 | use O2System\Kernel\Cli\Abstracts\AbstractCommander; |
||||
19 | use O2System\Kernel\Cli\Abstracts\AbstractCommandersPool; |
||||
20 | use O2System\Kernel\Cli\Writers\Format; |
||||
21 | use O2System\Kernel\Cli\Writers\Table; |
||||
22 | use O2System\Kernel\Cli\Writers\Text; |
||||
23 | |||||
24 | /** |
||||
25 | * Class App |
||||
26 | * |
||||
27 | * Command line interface (cli) application commands container class. |
||||
28 | * |
||||
29 | * @package O2System\Kernel\Cli |
||||
30 | */ |
||||
31 | class App extends AbstractCommandersPool |
||||
32 | { |
||||
33 | /** |
||||
34 | * App::$logo |
||||
35 | * |
||||
36 | * Cli-App welcome note. |
||||
37 | * |
||||
38 | * @var string |
||||
39 | */ |
||||
40 | protected $welcomeNote; |
||||
41 | |||||
42 | /** |
||||
43 | * App::$name |
||||
44 | * |
||||
45 | * Cli-App name. |
||||
46 | * |
||||
47 | * @var string |
||||
48 | */ |
||||
49 | protected $name; |
||||
50 | |||||
51 | /** |
||||
52 | * App::$version |
||||
53 | * |
||||
54 | * Cli-App version. |
||||
55 | * |
||||
56 | * @var string |
||||
57 | */ |
||||
58 | protected $version; |
||||
59 | |||||
60 | /** |
||||
61 | * App::$description |
||||
62 | * |
||||
63 | * Cli-App description. |
||||
64 | * |
||||
65 | * @var string |
||||
66 | */ |
||||
67 | protected $description; |
||||
68 | |||||
69 | // ------------------------------------------------------------------------ |
||||
70 | |||||
71 | /** |
||||
72 | * App::__construct |
||||
73 | * |
||||
74 | * Cli-App constructor. |
||||
75 | */ |
||||
76 | public function __construct() |
||||
77 | { |
||||
78 | language()->loadFile('cli'); |
||||
79 | } |
||||
80 | |||||
81 | // ------------------------------------------------------------------------ |
||||
82 | |||||
83 | /** |
||||
84 | * App::addCommander |
||||
85 | * |
||||
86 | * Add new commander to the pool. |
||||
87 | * |
||||
88 | * @param AbstractCommander $commander |
||||
89 | */ |
||||
90 | public function addCommander(AbstractCommander $commander) |
||||
91 | { |
||||
92 | if (method_exists($commander, 'setApp')) { |
||||
93 | $commander->setApp($this); |
||||
94 | } |
||||
95 | |||||
96 | $this->commandersPool[ $commander->getCommandName() ] = $commander; |
||||
97 | } |
||||
98 | |||||
99 | // ------------------------------------------------------------------------ |
||||
100 | |||||
101 | /** |
||||
102 | * App::getWelcomeNote |
||||
103 | * |
||||
104 | * @return string |
||||
105 | */ |
||||
106 | public function getWelcomeNote() |
||||
107 | { |
||||
108 | return $this->welcomeNote; |
||||
109 | } |
||||
110 | |||||
111 | // ------------------------------------------------------------------------ |
||||
112 | |||||
113 | /** |
||||
114 | * App::setWelcomeNote |
||||
115 | * |
||||
116 | * Sets cli-app welcome note. |
||||
117 | * |
||||
118 | * @param string $welcomeNote |
||||
119 | * |
||||
120 | * @return static |
||||
121 | */ |
||||
122 | public function setWelcomeNote($welcomeNote) |
||||
123 | { |
||||
124 | $this->welcomeNote = $welcomeNote; |
||||
125 | |||||
126 | return $this; |
||||
127 | } |
||||
128 | |||||
129 | public function getName() |
||||
130 | { |
||||
131 | return $this->name; |
||||
132 | } |
||||
133 | |||||
134 | // ------------------------------------------------------------------------ |
||||
135 | |||||
136 | /** |
||||
137 | * App::setName |
||||
138 | * |
||||
139 | * Sets cli-app name. |
||||
140 | * |
||||
141 | * @param string $name |
||||
142 | * |
||||
143 | * @return static |
||||
144 | */ |
||||
145 | public function setName($name) |
||||
146 | { |
||||
147 | $this->name = trim($name); |
||||
148 | |||||
149 | return $this; |
||||
150 | } |
||||
151 | |||||
152 | /** |
||||
153 | * App::getVersion |
||||
154 | * |
||||
155 | * Gets cli-app version. |
||||
156 | * |
||||
157 | * @return string |
||||
158 | */ |
||||
159 | public function getVersion() |
||||
160 | { |
||||
161 | return $this->version; |
||||
162 | } |
||||
163 | |||||
164 | // ------------------------------------------------------------------------ |
||||
165 | |||||
166 | /** |
||||
167 | * App::setVersion |
||||
168 | * |
||||
169 | * Sets cli-app version. |
||||
170 | * |
||||
171 | * @param string $version |
||||
172 | * |
||||
173 | * @return static |
||||
174 | */ |
||||
175 | public function setVersion($version) |
||||
176 | { |
||||
177 | $this->version = trim($version); |
||||
178 | |||||
179 | return $this; |
||||
180 | } |
||||
181 | |||||
182 | /** |
||||
183 | * App::setDescription |
||||
184 | * |
||||
185 | * Sets cli-app description. |
||||
186 | * |
||||
187 | * @param string $description |
||||
188 | * |
||||
189 | * @return static |
||||
190 | */ |
||||
191 | public function setDescription($description) |
||||
192 | { |
||||
193 | $this->description = trim($description); |
||||
194 | |||||
195 | return $this; |
||||
196 | } |
||||
197 | |||||
198 | // ------------------------------------------------------------------------ |
||||
199 | |||||
200 | /** |
||||
201 | * App::run |
||||
202 | * |
||||
203 | * Run cli-app |
||||
204 | * |
||||
205 | * @return static |
||||
206 | * @throws \ReflectionException |
||||
207 | */ |
||||
208 | public function run() |
||||
209 | { |
||||
210 | $command = new \ReflectionClass($this); |
||||
211 | $options = input()->get(); |
||||
212 | |||||
213 | if (empty($options)) { |
||||
214 | if ($this->welcomeNote instanceof Format or $this->welcomeNote instanceof Text) { |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
215 | output()->write($this->welcomeNote); |
||||
216 | } elseif (is_string($this->welcomeNote) and $this->welcomeNote !== '') { |
||||
217 | output()->write( |
||||
0 ignored issues
–
show
The method
write() does not exist on O2System\Kernel\Http\Output .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||
218 | (new Format()) |
||||
219 | ->setString(language()->getLine($this->welcomeNote)) |
||||
220 | ->setNewLinesAfter(1) |
||||
221 | ); |
||||
222 | } |
||||
223 | |||||
224 | output()->write( |
||||
225 | (new Format()) |
||||
226 | ->setString($this->name . ' v' . $this->version) |
||||
227 | ->setNewLinesBefore(2) |
||||
228 | ->setNewLinesAfter(1) |
||||
229 | ); |
||||
230 | |||||
231 | // Run help option |
||||
232 | $this->optionHelp(); |
||||
233 | } else { |
||||
234 | |||||
235 | foreach ($options as $method => $arguments) { |
||||
236 | |||||
237 | if ($method === 'h') { |
||||
238 | $method = 'help'; |
||||
239 | } elseif ($method === 'v') { |
||||
240 | $method = 'version'; |
||||
241 | } elseif ($method === 'vv') { |
||||
242 | $method = 'verbose'; |
||||
243 | } |
||||
244 | |||||
245 | $optionMethod = camelcase('option-' . $method); |
||||
246 | |||||
247 | if ($command->hasMethod($optionMethod)) { |
||||
248 | |||||
249 | $commandMethod = $command->getMethod($optionMethod); |
||||
250 | |||||
251 | if ($commandMethod->getNumberOfRequiredParameters() == 0) { |
||||
252 | call_user_func([&$this, $optionMethod]); |
||||
253 | } else { |
||||
254 | $optionArguments = is_array($arguments) |
||||
255 | ? $arguments |
||||
256 | : [$arguments]; |
||||
257 | |||||
258 | call_user_func_array([&$this, $optionMethod], $optionArguments); |
||||
259 | } |
||||
260 | } |
||||
261 | } |
||||
262 | } |
||||
263 | } |
||||
264 | |||||
265 | // ------------------------------------------------------------------------ |
||||
266 | |||||
267 | /** |
||||
268 | * App::optionHelp |
||||
269 | * |
||||
270 | * @return void |
||||
271 | */ |
||||
272 | public function optionHelp() |
||||
273 | { |
||||
274 | // Show Usage |
||||
275 | output()->write( |
||||
276 | (new Format()) |
||||
277 | ->setContextualClass(Format::INFO) |
||||
278 | ->setString(language()->getLine('CLI_USAGE') . ':') |
||||
279 | ->setNewLinesBefore(1) |
||||
280 | ->setNewLinesAfter(1) |
||||
281 | ); |
||||
282 | |||||
283 | output()->write( |
||||
284 | (new Format()) |
||||
285 | ->setContextualClass(Format::INFO) |
||||
286 | ->setString('command:action --option=value') |
||||
287 | ->setNewLinesAfter(2) |
||||
288 | ); |
||||
289 | |||||
290 | // Show Commanders |
||||
291 | $this->loadCommanders(); |
||||
292 | |||||
293 | if (count($this->commandersPool)) { |
||||
294 | output()->write( |
||||
295 | (new Format()) |
||||
296 | ->setString(language()->getLine('CLI_COMMANDS') . ':') |
||||
297 | ->setNewLinesAfter(1) |
||||
298 | ); |
||||
299 | |||||
300 | $table = new Table(); |
||||
301 | $table->isShowBorder = false; |
||||
302 | |||||
303 | foreach ($this->commandersPool as $commander) { |
||||
304 | |||||
305 | if ($commander instanceof AbstractCommander) { |
||||
306 | $table |
||||
307 | ->addRow() |
||||
308 | ->addColumn($commander->getCommandName()) |
||||
309 | ->addColumn(language()->getLine($commander->getCommandDescription())); |
||||
310 | } |
||||
311 | } |
||||
312 | |||||
313 | output()->write( |
||||
314 | (new Format()) |
||||
315 | ->setString($table->render()) |
||||
316 | ->setNewLinesAfter(2) |
||||
317 | ); |
||||
318 | } |
||||
319 | |||||
320 | // Show Options |
||||
321 | output()->write( |
||||
322 | (new Format()) |
||||
323 | ->setString(language()->getLine('CLI_OPTIONS') . ':') |
||||
324 | ->setNewLinesAfter(1) |
||||
325 | ); |
||||
326 | |||||
327 | $table = new Table(); |
||||
328 | $table->isShowBorder = false; |
||||
329 | |||||
330 | $table |
||||
331 | ->addRow() |
||||
332 | ->addColumn('--version') |
||||
333 | ->addColumn('-v') |
||||
334 | ->addColumn(language()->getLine('CLI_HELP_SHOW_OPTION_VERSION')) |
||||
335 | ->addRow() |
||||
336 | ->addColumn('--help') |
||||
337 | ->addColumn('-h') |
||||
338 | ->addColumn(language()->getLine('CLI_HELP_SHOW_OPTION_HELP')) |
||||
339 | ->addRow() |
||||
340 | ->addColumn('--verbose') |
||||
341 | ->addColumn('-vv') |
||||
342 | ->addColumn(language()->getLine('CLI_HELP_SHOW_OPTION_VERBOSE')); |
||||
343 | |||||
344 | output()->write( |
||||
345 | (new Format()) |
||||
346 | ->setString($table->render()) |
||||
347 | ->setNewLinesAfter(2) |
||||
348 | ); |
||||
349 | |||||
350 | exit(EXIT_SUCCESS); |
||||
0 ignored issues
–
show
|
|||||
351 | } |
||||
352 | |||||
353 | // ------------------------------------------------------------------------ |
||||
354 | |||||
355 | /** |
||||
356 | * App::optionVersion |
||||
357 | * |
||||
358 | * @return void |
||||
359 | */ |
||||
360 | public function optionVersion() |
||||
361 | { |
||||
362 | if (property_exists($this, 'version')) { |
||||
363 | if ( ! empty($this->version)) { |
||||
364 | output()->write( |
||||
365 | (new Format()) |
||||
366 | ->setString($this->name . ' v' . $this->version . ' Copyright (c) 2011 - ' . date('Y') . ' Steeve Andrian Salim') |
||||
367 | ->setNewLinesAfter(1) |
||||
368 | ); |
||||
369 | |||||
370 | output()->write( |
||||
371 | (new Format()) |
||||
372 | ->setIndent(2) |
||||
373 | ->setString('this framework is trademark of Steeve Andrian Salim') |
||||
374 | ->setNewLinesAfter(1) |
||||
375 | ); |
||||
376 | } |
||||
377 | } |
||||
378 | } |
||||
379 | } |