Skip to content

Commit afddfbf

Browse files
committed
update, some logic modify, more event support ...
1 parent 4d46499 commit afddfbf

21 files changed

+1154
-255
lines changed

examples/HomeController.php

+3
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ public function downCommand()
230230
return 0;
231231
}
232232

233+
/**
234+
* show cursor move on the screen
235+
*/
233236
public function cursorCommand()
234237
{
235238
$this->write('hello, this in ' . __METHOD__);

src/AbstractApp.php

+29-47
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,28 @@
1010

1111
use inhere\console\io\Input;
1212
use inhere\console\io\Output;
13-
use inhere\console\traits\TraitInputOutput;
13+
use inhere\console\traits\InputOutputTrait;
14+
use inhere\console\traits\SimpleEventStaticTrait;
1415

1516
/**
1617
* Class AbstractApp
1718
* @package inhere\console
1819
*/
1920
abstract class AbstractApp
2021
{
21-
use TraitInputOutput;
22+
use InputOutputTrait;
23+
use SimpleEventStaticTrait;
2224

2325
// event name list
2426
const ON_BEFORE_RUN = 'beforeRun';
2527
const ON_AFTER_RUN = 'afterRun';
26-
const ON_APP_STOP = 'appStop';
28+
const ON_RUN_ERROR = 'runError';
29+
const ON_BEFORE_EXEC = 'beforeExec';
30+
const ON_AFTER_EXEC = 'afterExec';
31+
const ON_EXEC_ERROR = 'execError';
32+
const ON_STOP_RUN = 'stopRun';
2733
const ON_NOT_FOUND = 'notFound';
2834

29-
/**
30-
* @var array
31-
*/
32-
protected static $hooks = [
33-
// 'appInit' => '',
34-
'beforeRun' => '',
35-
'afterRun' => '',
36-
'appStop' => '',
37-
'notFound' => '',
38-
];
39-
4035
/**
4136
* app config
4237
* @var array
@@ -69,6 +64,11 @@ abstract class AbstractApp
6964
*/
7065
protected $commands = [];
7166

67+
/**
68+
* @var string
69+
*/
70+
private $commandName;
71+
7272
/**
7373
* App constructor.
7474
* @param array $config
@@ -86,7 +86,7 @@ public function __construct(array $config = [], Input $input = null, Output $out
8686

8787
protected function init()
8888
{
89-
// ...
89+
$this->commandName = $this->input->getCommand();
9090
}
9191

9292
/**********************************************************
@@ -96,7 +96,6 @@ protected function init()
9696
protected function prepareRun()
9797
{
9898
date_default_timezone_set($this->config('timeZone', 'UTC'));
99-
10099
// ...
101100
}
102101

@@ -109,17 +108,13 @@ public function run($exit = true)
109108
$this->prepareRun();
110109

111110
// call 'onBeforeRun' service, if it is registered.
112-
if ($cb = self::$hooks[self::ON_BEFORE_RUN]) {
113-
$cb($this);
114-
}
111+
self::fire(self::ON_BEFORE_RUN, [$this]);
115112

116113
// do run ...
117114
$returnCode = $this->doRun();
118115

119116
// call 'onAfterRun' service, if it is registered.
120-
if ($cb = self::$hooks[self::ON_AFTER_RUN]) {
121-
$cb($this);
122-
}
117+
self::fire(self::ON_AFTER_RUN, [$this]);
123118

124119
if ($exit) {
125120
$this->stop((int)$returnCode);
@@ -137,9 +132,7 @@ abstract public function doRun();
137132
public function stop($code = 0)
138133
{
139134
// call 'onAppStop' service, if it is registered.
140-
if ($cb = self::$hooks[self::ON_APP_STOP]) {
141-
$cb($this);
142-
}
135+
self::fire(self::ON_STOP_RUN, [$this]);
143136

144137
exit((int)$code);
145138
}
@@ -160,7 +153,7 @@ public function controller(string $name, string $controller)
160153
throw new \InvalidArgumentException('Parameters are not allowed to is empty!');
161154
}
162155

163-
$this->checkName($name, true);
156+
$this->validateName($name, true);
164157

165158
if (!class_exists($controller)) {
166159
throw new \InvalidArgumentException("The console controller class [$controller] not exists!");
@@ -193,7 +186,7 @@ public function command(string $name, $handler)
193186
throw new \InvalidArgumentException('Parameters are not allowed to is empty!');
194187
}
195188

196-
$this->checkName($name);
189+
$this->validateName($name);
197190

198191
// is an class name string
199192
$this->commands[$name] = $handler;
@@ -211,25 +204,6 @@ public function commands(array $commands)
211204
}
212205
}
213206

214-
/**
215-
* @return array
216-
*/
217-
public static function hooks(): array
218-
{
219-
return array_keys(self::$hooks);
220-
}
221-
222-
/**
223-
* @param $event
224-
* @param callable $handler
225-
*/
226-
public function on(string $event, callable $handler)
227-
{
228-
if (isset(self::$hooks[$event])) {
229-
self::$hooks[$event] = $handler;
230-
}
231-
}
232-
233207
/**
234208
* @return array
235209
*/
@@ -314,7 +288,7 @@ public function isDebug(): bool
314288
* @param $name
315289
* @param bool $isGroup
316290
*/
317-
protected function checkName(string $name, $isGroup = false)
291+
protected function validateName(string $name, $isGroup = false)
318292
{
319293
$pattern = $isGroup ? '/^[a-z][\w-]+$/' : '/^[a-z][\w-]*:?([a-z][\w-]+)?$/';
320294

@@ -326,4 +300,12 @@ protected function checkName(string $name, $isGroup = false)
326300
throw new \InvalidArgumentException("The command name [$name] is not allowed. It is a built in command.");
327301
}
328302
}
303+
304+
/**
305+
* @return string
306+
*/
307+
public function getCommandName(): string
308+
{
309+
return $this->commandName;
310+
}
329311
}

src/AbstractCommand.php

+92-13
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,21 @@
88

99
namespace inhere\console;
1010

11-
use inhere\console\helpers\Annotation;
1211
use inhere\console\io\Input;
12+
use inhere\console\io\InputDefinition;
1313
use inhere\console\io\Output;
14-
use inhere\console\traits\TraitInputOutput;
15-
use inhere\console\traits\TraitInteract;
14+
use inhere\console\traits\InputOutputTrait;
15+
use inhere\console\traits\UserInteractTrait;
16+
use inhere\console\utils\Annotation;
1617

1718
/**
1819
* Class AbstractCommand
1920
* @package inhere\console
2021
*/
2122
abstract class AbstractCommand
2223
{
23-
use TraitInputOutput;
24-
use TraitInteract;
24+
use InputOutputTrait;
25+
use UserInteractTrait;
2526

2627
// command description message
2728
// please use the const setting current controller/command description
@@ -57,33 +58,73 @@ abstract class AbstractCommand
5758
'example' => true,
5859
];
5960

61+
/**
62+
* @var InputDefinition
63+
*/
64+
private $definition;
65+
66+
////// for strict mode //////
67+
6068
/**
6169
* Command constructor.
6270
* @param Input $input
6371
* @param Output $output
72+
* @param InputDefinition|null $definition
6473
*/
65-
public function __construct(Input $input, Output $output)
74+
public function __construct(Input $input, Output $output, InputDefinition $definition = null)
6675
{
6776
$this->input = $input;
6877
$this->output = $output;
78+
79+
if (null === $definition) {
80+
$this->definition = new InputDefinition();
81+
} else {
82+
$this->definition = $definition;
83+
$this->validate();
84+
}
85+
}
86+
87+
/**
88+
* @return array
89+
*/
90+
protected function configure()
91+
{
92+
return [
93+
// 'arguments' => [],
94+
// 'options' => [],
95+
];
96+
}
97+
98+
public function validate()
99+
{
100+
$definition = $this->definition;
101+
$givenArguments = $this->input->getArgs();
102+
103+
$missingArguments = array_filter(array_keys($definition->getArguments()), function ($name) use ($definition, $givenArguments) {
104+
return !array_key_exists($name, $givenArguments) && $definition->argumentIsRequired($name);
105+
});
106+
107+
if (count($missingArguments) > 0) {
108+
throw new \RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
109+
}
69110
}
70111

71112
/**
72-
* @param string $arg
113+
* run
73114
*/
74-
abstract public function run($arg = '');
115+
abstract public function run();
75116

76117
/**
77-
* @param string $action
118+
* beforeRun
78119
*/
79-
protected function beforeRun($action)
120+
protected function beforeRun()
80121
{
81122
}
82123

83124
/**
84-
* @param string $action
125+
* afterRun
85126
*/
86-
protected function afterRun($action)
127+
protected function afterRun()
87128
{
88129
}
89130

@@ -129,7 +170,11 @@ protected function showHelpByMethodAnnotation($method, $action = null)
129170
if (isset(self::$allowTags[$tag])) {
130171
// need multi align
131172
if (self::$allowTags[$tag]) {
132-
$msg = implode("\n ", array_filter(explode("\n", $msg), 'trim'));
173+
$lines = array_map(function ($line) {
174+
return trim($line);
175+
}, explode("\n", $msg));
176+
177+
$msg = implode("\n ", array_filter($lines, 'trim'));
133178
}
134179

135180
$tag = ucfirst($tag);
@@ -182,4 +227,38 @@ public static function setAllowTags(array $allowTags)
182227
{
183228
self::$allowTags = $allowTags;
184229
}
230+
231+
/**
232+
* @return string
233+
*/
234+
public static function getDescription(): string
235+
{
236+
return self::$description;
237+
}
238+
239+
/**
240+
* @param string $description
241+
*/
242+
public static function setDescription(string $description)
243+
{
244+
self::$description = $description;
245+
}
246+
247+
/**
248+
* @return InputDefinition
249+
*/
250+
public function getDefinition(): InputDefinition
251+
{
252+
return $this->definition;
253+
}
254+
255+
/**
256+
* @param InputDefinition $definition
257+
*/
258+
public function setDefinition(InputDefinition $definition)
259+
{
260+
$this->definition = $definition;
261+
}
262+
263+
185264
}

0 commit comments

Comments
 (0)