Skip to content

Commit 31a482e

Browse files
committed
some update, remove internal config
1 parent 8d21083 commit 31a482e

File tree

4 files changed

+62
-63
lines changed

4 files changed

+62
-63
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,13 @@ use inhere\console\io\Input;
4646
use inhere\console\io\Output;
4747
use inhere\console\App;
4848

49-
$config = [];
49+
$meta = [
50+
'name' => 'My Console App',
51+
'version' => '1.0.2',
52+
];
5053
$input = new Input;
5154
$output = new Output;
52-
$app = new App($config, $input, $output);
55+
$app = new App($meta, $input, $output);
5356

5457
// add command routes
5558
$app->command('demo', function (Input $in, Output $out) {

README_zh.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,13 @@ use inhere\console\io\Input;
5050
use inhere\console\io\Output;
5151
use inhere\console\App;
5252

53-
$config = [];
53+
$meta = [
54+
'name' => 'My Console App',
55+
'version' => '1.0.2',
56+
];
5457
$input = new Input;
5558
$output = new Output;
56-
$app = new App($config, $input, $output);
59+
$app = new App($meta, $input, $output);
5760

5861
// add command routes
5962
$app->command('demo', function (Input $in, Output $out) {

document.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11

2-
## 根据位置设置参数
2+
## 注册命令
3+
4+
### 注册独立命令
5+
### 注册组命令
6+
### 设置命令名称
7+
### 设置命令描述
8+
9+
## 独立命令
10+
11+
## 组命令(controller)
12+
13+
## 输入定义(InputDefinition)
14+
15+
16+
## 设置参数
17+
18+
### 使用名称设置参数
19+
20+
### 根据位置设置参数
321

422
```
523
$ php examples/app demo john male 43 --opt1 value1 -y

src/AbstractApp.php

Lines changed: 33 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ abstract class AbstractApp
3434
const ON_NOT_FOUND = 'notFound';
3535

3636
/**
37-
* app config
37+
* app meta config
3838
* @var array
3939
*/
40-
private $config = [
41-
'env' => 'pdt', // dev test pdt
42-
'debug' => false,
40+
private $meta = [
4341
'name' => 'My Console',
4442
'version' => '0.5.1',
4543
'publishAt' => '2017.03.24',
4644
'rootPath' => '',
4745
'hideRootPath' => true,
48-
'charset' => 'UTF-8',
49-
'timeZone' => 'Asia/Shanghai',
46+
// 'env' => 'pdt', // dev test pdt
47+
// 'debug' => false,
48+
// 'charset' => 'UTF-8',
49+
// 'timeZone' => 'Asia/Shanghai',
5050
];
5151

5252
/**
@@ -58,6 +58,14 @@ abstract class AbstractApp
5858
'list' => 'List all group and independent commands',
5959
];
6060

61+
/**
62+
* @var array
63+
*/
64+
protected static $internalOptions = [
65+
'--debug' => 'setting the application runtime debug level',
66+
'--no-color' => 'setting no color for message output',
67+
];
68+
6169
/**
6270
* @var array
6371
*/
@@ -80,14 +88,14 @@ abstract class AbstractApp
8088

8189
/**
8290
* App constructor.
83-
* @param array $config
91+
* @param array $meta
8492
* @param Input $input
8593
* @param Output $output
8694
*/
87-
public function __construct(array $config = [], Input $input = null, Output $output = null)
95+
public function __construct(array $meta = [], Input $input = null, Output $output = null)
8896
{
8997
$this->runtimeCheck();
90-
$this->setConfig($config);
98+
$this->setMeta($meta);
9199

92100
$this->input = $input ?: new Input();
93101
$this->output = $output ?: new Output();
@@ -118,7 +126,7 @@ protected function init()
118126

119127
protected function prepareRun()
120128
{
121-
date_default_timezone_set($this->config('timeZone', 'UTC'));
129+
// date_default_timezone_set($this->config('timeZone', 'UTC'));
122130
}
123131

124132
/**
@@ -127,11 +135,9 @@ protected function prepareRun()
127135
*/
128136
public function run($exit = true)
129137
{
130-
$this->prepareRun();
131-
132138
$command = $this->input->getCommand();
133139

134-
// like show help info
140+
$this->prepareRun();
135141
$this->filterSpecialCommand($command);
136142

137143
// call 'onBeforeRun' service, if it is registered.
@@ -302,7 +308,7 @@ public function exceptionHandler($e)
302308
$e->getTraceAsString()
303309
);
304310

305-
if ($this->config('hideRootPath') && $rootPath = $this->config('rootPath')) {
311+
if ($this->meta['hideRootPath'] && $rootPath = $this->meta['rootPath']) {
306312
$message = str_replace($rootPath, '{ROOT}', $message);
307313
}
308314

@@ -439,7 +445,8 @@ public function showCommandList($quit = true)
439445
//'There are all console controllers and independent commands.',
440446
'Group Commands:(by controller)' => $controllerArr ?: '... No register any group command(controller)',
441447
'Independent Commands:' => $commandArr ?: '... No register any independent command',
442-
'Internal Commands:' => $internalCommands
448+
'Internal Commands:' => $internalCommands,
449+
'Internal Options:' => self::$internalOptions
443450
]);
444451

445452
$this->output->write("More please see: <cyan>$script [controller|command] -h</cyan>");
@@ -518,63 +525,31 @@ public function isInternalCommand(string $name): bool
518525
}
519526

520527
/**
521-
* get/set config
522-
* @param array|string $name
523-
* @param mixed $default
524-
* @return mixed
525-
*/
526-
public function config($name, $default = null)
527-
{
528-
// `$name` is array, set config.
529-
if (is_array($name)) {
530-
foreach ((array) $name as $key => $value) {
531-
$this->config[$key] = $value;
532-
}
533-
534-
return true;
535-
}
536-
537-
// is string, get config
538-
if (!is_string($name)) {
539-
return $default;
540-
}
541-
542-
// allow get $config['top']['sub'] by 'top.sub'
543-
if (strpos($name, '.') > 1) {
544-
$nodes = array_filter(explode('.', $name));
545-
546-
return Helper::findValueByNodes($this->config, $nodes, $default);
547-
}
548-
549-
return $this->config[$name] ?? $default;
550-
}
551-
552-
/**
553-
* set config
554-
* @param array $config
528+
* set meta info
529+
* @param array $meta
555530
*/
556-
public function setConfig($config)
531+
public function setMeta(array $meta)
557532
{
558-
if ($config) {
559-
$this->config = array_merge($this->config, (array)$config);
533+
if ($meta) {
534+
$this->meta = array_merge($this->meta, (array)$meta);
560535
}
561536
}
562537

563538
/**
564-
* get config
539+
* get meta
565540
* @return array
566541
*/
567-
public function getConfig()
542+
public function getMeta()
568543
{
569-
return $this->config;
544+
return $this->meta;
570545
}
571546

572547
/**
573548
* is Debug
574-
* @return boolean
549+
* @return boolean|int
575550
*/
576-
public function isDebug(): bool
551+
public function isDebug()
577552
{
578-
return (bool) $this->config('debug');
553+
return $this->input->getOpt('debug');
579554
}
580555
}

0 commit comments

Comments
 (0)