|
18 | 18 | abstract class Command extends AbstractCommand |
19 | 19 | { |
20 | 20 | // command usage message |
21 | | - const USAGE = ''; |
| 21 | + protected $usage = ''; |
| 22 | + |
| 23 | + // command arguments message |
| 24 | + protected $arguments = []; |
| 25 | + |
| 26 | + // command arguments message |
| 27 | + protected $options = []; |
22 | 28 |
|
23 | 29 | // command example message |
24 | | - const EXAMPLE = ''; |
| 30 | + protected $example = ''; |
25 | 31 |
|
26 | | - /** |
27 | | - * Command constructor. |
28 | | - * @param Input $input |
29 | | - * @param Output $output |
30 | | - */ |
31 | | - public function __construct(Input $input, Output $output) |
| 32 | + // run command |
| 33 | + public function run($name = '') |
32 | 34 | { |
33 | | - $this->input = $input; |
34 | | - $this->output = $output; |
35 | | - } |
| 35 | + $this->setName($name); |
36 | 36 |
|
37 | | - abstract public function execute(); |
| 37 | + if ($this->input->getBool('h') || $this->input->getBool('help')) { |
| 38 | + return $this->showHelp(); |
| 39 | + } |
38 | 40 |
|
39 | | - public function help() |
40 | | - { |
41 | | - $this->write('No help information.'); |
| 41 | + try { |
| 42 | + $this->beforeRun(); |
| 43 | + |
| 44 | + $status = $this->execute(); |
| 45 | + |
| 46 | + $this->afterRun(); |
| 47 | + |
| 48 | + } catch (\Exception $e) { |
| 49 | + $this->handleRuntimeException($e); |
| 50 | + } |
| 51 | + |
| 52 | + return $status; |
42 | 53 | } |
43 | 54 |
|
| 55 | + // do execute |
| 56 | + abstract protected function execute(); |
| 57 | + |
44 | 58 | /** |
45 | | - * @param string $msg |
46 | | - * @return string |
| 59 | + * handle command runtime exception |
| 60 | + * |
| 61 | + * @param \Exception $e |
| 62 | + * @throws \Exception |
47 | 63 | */ |
48 | | - protected function read($msg = '') |
| 64 | + protected function handleRuntimeException(\Exception $e) |
49 | 65 | { |
50 | | - return $this->input->read($msg); |
| 66 | + throw $e; |
51 | 67 | } |
52 | 68 |
|
53 | | - /** |
54 | | - * @param $message |
55 | | - * @param bool $nl |
56 | | - * @param bool $quit |
57 | | - */ |
58 | | - protected function write($message, $nl = true, $quit = false) |
| 69 | + protected function beforeRun() |
| 70 | + {} |
| 71 | + |
| 72 | + protected function afterRun() |
| 73 | + {} |
| 74 | + |
| 75 | + protected function configure() |
| 76 | + { |
| 77 | + return [ |
| 78 | + // 'usage' => '', |
| 79 | + |
| 80 | + // 'arguments' => [], |
| 81 | + // 'options' => [], |
| 82 | + // 'examples' => [], |
| 83 | + ]; |
| 84 | + } |
| 85 | + |
| 86 | + public function showHelp() |
59 | 87 | { |
60 | | - $this->output->write($message, $nl, $quit); |
| 88 | + $configure = $this->configure(); |
| 89 | + |
| 90 | + if ( !$configure ) { |
| 91 | + return 91; |
| 92 | + } |
| 93 | + |
| 94 | + $configure = array_merge([ |
| 95 | + 'usage' => '', |
| 96 | + |
| 97 | + 'arguments' => [], |
| 98 | + 'options' => [], |
| 99 | + 'examples' => [], |
| 100 | + ], $configure); |
| 101 | + |
| 102 | + $this->output->helpPanel( |
| 103 | + $configure['usage'], |
| 104 | + $configure['arguments'], |
| 105 | + $configure['options'], |
| 106 | + (array)$configure['examples'], |
| 107 | + static::DESCRIPTION, |
| 108 | + false |
| 109 | + ); |
| 110 | + |
| 111 | + return 0; |
61 | 112 | } |
62 | 113 | } |
0 commit comments