Skip to content

Commit c4bb8e6

Browse files
committed
update, some logic update
1 parent c553213 commit c4bb8e6

File tree

7 files changed

+135
-67
lines changed

7 files changed

+135
-67
lines changed

src/AbstractApp.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ abstract class AbstractApp
4141
protected $config = [
4242
'env' => 'pdt', // dev test pdt
4343
'debug' => false,
44+
'name' => 'My Console',
45+
'version' => '0.5.1',
4446
'charset' => 'UTF-8',
4547
'timeZone' => 'Asia/Shanghai',
46-
'version' => '0.5.1',
4748
];
4849

4950
/**
@@ -350,10 +351,10 @@ public function setOutput(Output $output)
350351

351352
protected function checkName($name, $isGroup = false)
352353
{
353-
$pattern = $isGroup ? '/^[a-z][\w-]+$/' : '/^[a-z][\w-]+:?([a-z][\w-]+)?$/';
354+
$pattern = $isGroup ? '/^[a-z][\w-]+$/' : '/^[a-z][\w-]*:?([a-z][\w-]+)?$/';
354355

355356
if (1 !== preg_match($pattern, $name)) {
356-
throw new \InvalidArgumentException('The command name is must match: ^[a-z][\w-]+$');
357+
throw new \InvalidArgumentException('The command name is must match: ' . $pattern);
357358
}
358359

359360
if ( $this->isInternalCommand($name) ) {

src/AbstractCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ abstract class AbstractCommand
3434
*/
3535
protected static $allowTags = ['description', 'usage', 'example'];
3636

37+
abstract public function run($arg = '');
38+
3739
/**
3840
* @param string $name
3941
*/
@@ -65,4 +67,4 @@ public static function setAllowTags($allowTags)
6567
{
6668
self::$allowTags = $allowTags;
6769
}
68-
}
70+
}

src/App.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,12 @@ public function runCommand($name, $believable = false)
103103

104104
/** @var Command $object */
105105
$object = new $handler($this->input, $this->output);
106-
$object->setName($name);
107106

108107
if ( !($object instanceof Command ) ) {
109108
throw new \InvalidArgumentException("The console command class [$handler] must instanceof the " . Command::class);
110109
}
111110

112-
$status = $object->execute();
111+
$status = $object->run($name);
113112
}
114113

115114
return $status;
@@ -197,7 +196,7 @@ public function showHelpInfo($quit = true)
197196
$message = <<<EOF
198197
<comment>Usage:</comment>
199198
$script [route|command] [arg1=value1 arg2=value ...] [-v|-h ...]
200-
199+
201200
<comment>Example:</comment>
202201
$script test
203202
$script home/index
@@ -219,7 +218,7 @@ public function showVersionInfo($quit = true)
219218

220219
$message = <<<EOF
221220
Console App Version <comment>$version</comment>
222-
221+
223222
<comment>System:</comment>
224223
PHP <info>$phpVersion</info>, on OS <info>$os</info>
225224
EOF;

src/Command.php

Lines changed: 77 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,96 @@
1818
abstract class Command extends AbstractCommand
1919
{
2020
// 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 = [];
2228

2329
// command example message
24-
const EXAMPLE = '';
30+
protected $example = '';
2531

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 = '')
3234
{
33-
$this->input = $input;
34-
$this->output = $output;
35-
}
35+
$this->setName($name);
3636

37-
abstract public function execute();
37+
if ($this->input->getBool('h') || $this->input->getBool('help')) {
38+
return $this->showHelp();
39+
}
3840

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;
4253
}
4354

55+
// do execute
56+
abstract protected function execute();
57+
4458
/**
45-
* @param string $msg
46-
* @return string
59+
* handle command runtime exception
60+
*
61+
* @param \Exception $e
62+
* @throws \Exception
4763
*/
48-
protected function read($msg = '')
64+
protected function handleRuntimeException(\Exception $e)
4965
{
50-
return $this->input->read($msg);
66+
throw $e;
5167
}
5268

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()
5987
{
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;
61112
}
62113
}

src/Controller.php

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,6 @@ abstract class Controller extends AbstractCommand
3232
*/
3333
protected $notFoundCallback = 'notFound';
3434

35-
/**
36-
* Command constructor.
37-
* @param Input $input
38-
* @param Output $output
39-
*/
40-
public function __construct(Input $input, Output $output)
41-
{
42-
$this->input = $input;
43-
$this->output = $output;
44-
}
45-
4635
/**
4736
* 运行控制器的 action
4837
* @param $action
@@ -88,7 +77,7 @@ public function run($action = '')
8877
$this->afterRun($action);
8978

9079
} catch (\Exception $e) {
91-
$this->handleActionException($e);
80+
$this->handleRuntimeException($e);
9281
}
9382

9483
// if you defined the method '$this->notFoundCallback' , will call it
@@ -109,7 +98,7 @@ public function run($action = '')
10998
* @param \Exception $e
11099
* @throws \Exception
111100
*/
112-
protected function handleActionException(\Exception $e)
101+
protected function handleRuntimeException(\Exception $e)
113102
{
114103
throw $e;
115104
}
@@ -120,25 +109,6 @@ protected function beforeRun($action)
120109
protected function afterRun($action)
121110
{}
122111

123-
/**
124-
* @param string $msg
125-
* @return string
126-
*/
127-
protected function read($msg = '')
128-
{
129-
return $this->input->read($msg);
130-
}
131-
132-
/**
133-
* @param $message
134-
* @param bool $nl
135-
* @param bool $quit
136-
*/
137-
protected function write($message, $nl = true, $quit = false)
138-
{
139-
$this->output->write($message, $nl, $quit);
140-
}
141-
142112
/**
143113
* Show help of the controller command group or specified command action
144114
* @usage <info>{name}/[action] -h</info> OR <info>{name}/help [action]</info> OR <info>{name} [action]</info>

src/io/Input.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ public function getArgs()
102102
* @param mixed $default
103103
* @return mixed
104104
*/
105+
public function getArgument($name=null, $default = null)
106+
{
107+
return $this->get($name, $default);
108+
}
105109
public function getArg($name=null, $default = null)
106110
{
107111
return $this->get($name, $default);
@@ -140,6 +144,10 @@ public function getOpts()
140144
* @param null $default
141145
* @return bool|mixed|null
142146
*/
147+
public function getOption($name, $default = null)
148+
{
149+
return $this->getOpt($name, $default);
150+
}
143151
public function getOpt($name, $default = null)
144152
{
145153
if ( !$this->hasOpt($name) ) {

src/utils/TraitInputOutput.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,43 @@ trait TraitInputOutput
2727
*/
2828
protected $output;
2929

30+
/**
31+
* Command constructor.
32+
* @param Input $input
33+
* @param Output $output
34+
*/
35+
public function __construct(Input $input, Output $output)
36+
{
37+
$this->input = $input;
38+
$this->output = $output;
39+
}
40+
41+
/**
42+
* @return string
43+
*/
44+
public function getScript()
45+
{
46+
return $this->input->getScript();
47+
}
48+
49+
/**
50+
* @param string $msg
51+
* @return string
52+
*/
53+
protected function read($msg = '')
54+
{
55+
return $this->input->read($msg);
56+
}
57+
58+
/**
59+
* @param $message
60+
* @param bool $nl
61+
* @param bool $quit
62+
*/
63+
protected function write($message, $nl = true, $quit = false)
64+
{
65+
$this->output->write($message, $nl, $quit);
66+
}
3067

3168
/**
3269
* @return Input
@@ -60,4 +97,4 @@ public function setOutput(Output $output)
6097
$this->output = $output;
6198
}
6299

63-
}
100+
}

0 commit comments

Comments
 (0)