forked from inhere/php-console
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractApp.php
More file actions
329 lines (278 loc) · 7.21 KB
/
Copy pathAbstractApp.php
File metadata and controls
329 lines (278 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
/**
* Created by PhpStorm.
* User: inhere
* Date: 2017-03-09
* Time: 18:37
*/
namespace inhere\console;
use inhere\console\io\Input;
use inhere\console\io\Output;
use inhere\console\utils\TraitInputOutput;
/**
* Class AbstractApp
* @package inhere\console
*/
abstract class AbstractApp
{
use TraitInputOutput;
// event name list
const ON_BEFORE_RUN = 'beforeRun';
const ON_AFTER_RUN = 'afterRun';
const ON_APP_STOP = 'appStop';
const ON_NOT_FOUND = 'notFound';
/**
* @var array
*/
protected static $hooks = [
// 'appInit' => '',
'beforeRun' => '',
'afterRun' => '',
'appStop' => '',
'notFound' => '',
];
/**
* app config
* @var array
*/
protected $config = [
'env' => 'pdt', // dev test pdt
'debug' => false,
'name' => 'My Console',
'version' => '0.5.1',
'charset' => 'UTF-8',
'timeZone' => 'Asia/Shanghai',
];
/**
* @var array
*/
protected $internalCommands = [
'version' => 'Show application version information',
'help' => 'Show application help information',
'list' => 'List all group and independent commands',
];
/**
* @var array
*/
protected $controllers = [];
/**
* @var array
*/
protected $commands = [];
/**
* App constructor.
* @param array $config
* @param Input $input
* @param Output $output
*/
public function __construct(array $config = [], Input $input = null, Output $output = null)
{
$this->input = $input ?: new Input();
$this->output = $output ?: new Output();
$this->setConfig($config);
$this->init();
}
protected function init()
{
// ...
}
/**********************************************************
* app run
**********************************************************/
protected function prepareRun()
{
date_default_timezone_set($this->config('timeZone', 'UTC'));
// ...
}
/**
* run app
* @param bool $exit
*/
public function run($exit = true)
{
$this->prepareRun();
// call 'onBeforeRun' service, if it is registered.
if ($cb = self::$hooks[self::ON_BEFORE_RUN]) {
$cb($this);
}
// do run ...
$returnCode = $this->doRun();
// call 'onAfterRun' service, if it is registered.
if ($cb = self::$hooks[self::ON_AFTER_RUN]) {
$cb($this);
}
if ($exit) {
$this->stop((int)$returnCode);
}
}
/**
* do run
*/
abstract public function doRun();
/**
* @param int $code
*/
public function stop($code = 0)
{
// call 'onAppStop' service, if it is registered.
if ($cb = self::$hooks[self::ON_APP_STOP]) {
$cb($this);
}
exit((int)$code);
}
/**********************************************************
* register console controller/command
**********************************************************/
/**
* Register a app group command(by controller)
* @param string $name The controller name
* @param string $controller The controller class
* @return static
*/
public function controller($name, $controller)
{
if (!$name || !$controller) {
throw new \InvalidArgumentException('Parameters are not allowed to is empty!');
}
$this->checkName($name, true);
if (!class_exists($controller)) {
throw new \InvalidArgumentException("The console controller class [$controller] not exists!");
}
$this->controllers[$name] = $controller;
return $this;
}
/**
* @param array $controllers
*/
public function controllers(array $controllers)
{
foreach ($controllers as $name => $controller) {
$this->controller($name, $controller);
}
}
/**
* Register a app independent console command
* @param string $name
* @param string|\Closure $handler
* @return $this
*/
public function command($name, $handler)
{
if (!$name || !$handler) {
throw new \InvalidArgumentException('Parameters are not allowed to is empty!');
}
$this->checkName($name);
// is an class name string
$this->commands[$name] = $handler;
return $this;
}
/**
* @param array $commands
*/
public function commands(array $commands)
{
foreach ($commands as $name => $handler) {
$this->command($name, $handler);
}
}
/**
* @return array
*/
public static function hooks()
{
return array_keys(self::$hooks);
}
/**
* @param $event
* @param callable $handler
*/
public function on($event, callable $handler)
{
if (isset(self::$hooks[$event])) {
self::$hooks[$event] = $handler;
}
}
/**
* @return array
*/
public function getInternalCommands()
{
return $this->internalCommands;
}
/**
* @param $name
* @return bool
*/
public function isInternalCommand($name)
{
return isset($this->internalCommands[$name]);
}
/**
* get/set config
* @param array|string $name
* @param mixed $default
* @return mixed
*/
public function config($name, $default = null)
{
// `$name` is array, set config.
if (is_array($name)) {
foreach ((array)$name as $key => $value) {
$this->config[$key] = $value;
}
return true;
}
// is string, get config
if (!is_string($name)) {
return $default;
}
// allow get $config['top']['sub'] by 'top.sub'
if (strpos($name, '.') > 1) {
list($topKey, $subKey) = explode('.', $name, 2);
if (isset($this->config[$topKey], $this->config[$topKey][$subKey])) {
return $this->config[$topKey][$subKey];
}
}
return isset($this->config[$name]) ? $this->config[$name] : $default;
}
/**
* set config
* @param array $config
*/
public function setConfig(array $config)
{
if ($config) {
$this->config = array_merge($this->config, $config);
}
}
/**
* get config
* @return array
*/
public function getConfig()
{
return $this->config;
}
/**
* is Debug
* @return boolean
*/
public function isDebug()
{
return (bool)$this->config['debug'];
}
/**
* @param $name
* @param bool $isGroup
*/
protected function checkName($name, $isGroup = false)
{
$pattern = $isGroup ? '/^[a-z][\w-]+$/' : '/^[a-z][\w-]*:?([a-z][\w-]+)?$/';
if (1 !== preg_match($pattern, $name)) {
throw new \InvalidArgumentException('The command name is must match: ' . $pattern);
}
if ($this->isInternalCommand($name)) {
throw new \InvalidArgumentException("The command name [$name] is not allowed. It is a built in command.");
}
}
}