Skip to content

Commit 473abb6

Browse files
committed
update, some base class modify. add some base interface
1 parent 3882b81 commit 473abb6

File tree

10 files changed

+270
-164
lines changed

10 files changed

+270
-164
lines changed

src/App.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,131 @@
88

99
namespace inhere\console;
1010

11+
use inhere\console\base\AbstractApp;
12+
1113
/**
1214
* Class App
1315
* @package inhere\console
1416
*/
1517
class App extends AbstractApp
1618
{
19+
20+
/**********************************************************
21+
* register console controller/command
22+
**********************************************************/
23+
24+
/**
25+
* Register a app group command(by controller)
26+
* @param string $name The controller name
27+
* @param string $controller The controller class
28+
* @return static
29+
*/
30+
public function controller(string $name, string $controller = null)
31+
{
32+
if (!$controller && class_exists($name)) {
33+
/** @var Controller $controller */
34+
$controller = $name;
35+
$name = $controller::getName();
36+
}
37+
38+
if (!$name || !$controller) {
39+
throw new \InvalidArgumentException('Group-command "name" and "controller" not allowed to is empty! name: '
40+
. $name . ', controller: ' .$controller);
41+
}
42+
43+
$this->validateName($name, true);
44+
45+
if (!class_exists($controller)) {
46+
throw new \InvalidArgumentException("The console controller class [$controller] not exists!");
47+
}
48+
49+
if (!is_subclass_of($controller, Controller::class)) {
50+
throw new \InvalidArgumentException('The console controller class must is subclass of the: ' . Controller::class);
51+
}
52+
53+
$this->controllers[$name] = $controller;
54+
55+
return $this;
56+
}
57+
58+
/**
59+
* @param array $controllers
60+
*/
61+
public function controllers(array $controllers)
62+
{
63+
foreach ($controllers as $name => $controller) {
64+
if (is_int($name)) {
65+
$this->controller($controller);
66+
} else {
67+
$this->controller($name, $controller);
68+
}
69+
}
70+
}
71+
72+
/**
73+
* Register a app independent console command
74+
* @param string $name
75+
* @param string|\Closure $handler
76+
* @param null|string $description
77+
* @return $this
78+
*/
79+
public function command(string $name, $handler = null, $description = null)
80+
{
81+
if (!$handler && class_exists($name)) {
82+
/** @var Command $handler */
83+
$handler = $name;
84+
$name = $handler::getName();
85+
}
86+
87+
if (!$name || !$handler) {
88+
throw new \InvalidArgumentException('Command "name" and "handler" not allowed to is empty!');
89+
}
90+
91+
$this->validateName($name);
92+
93+
if (isset($this->commands[$name])) {
94+
throw new \InvalidArgumentException("Command '$name' have been registered!");
95+
}
96+
97+
if (is_string($handler)) {
98+
if (!class_exists($handler)) {
99+
throw new \InvalidArgumentException("The console command class [$handler] not exists!");
100+
}
101+
102+
if (!is_subclass_of($handler, Command::class)) {
103+
throw new \InvalidArgumentException('The console command class must is subclass of the: ' . Command::class);
104+
}
105+
} elseif (!is_object($handler) || !method_exists($handler, '__invoke')) {
106+
throw new \InvalidArgumentException(sprintf(
107+
'The console command handler must is an subclass of %s OR a Closure OR a object have method __invoke()',
108+
Command::class
109+
));
110+
}
111+
112+
// is an class name string
113+
$this->commands[$name] = $handler;
114+
115+
if ($description) {
116+
$this->addCommandMessage($name, $description);
117+
}
118+
119+
return $this;
120+
}
121+
122+
/**
123+
* @param array $commands
124+
*/
125+
public function commands(array $commands)
126+
{
127+
foreach ($commands as $name => $handler) {
128+
if (is_int($name)) {
129+
$this->command($handler);
130+
} else {
131+
$this->command($name, $handler);
132+
}
133+
}
134+
}
135+
17136
/**
18137
* addCommand
19138
* @param string $name

src/AppInterface.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/Command.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
namespace inhere\console;
1010

11-
use inhere\console\io\Input;
12-
use inhere\console\io\Output;
11+
use inhere\console\base\AbstractCommand;
1312

1413
/**
1514
* Class Command
@@ -19,8 +18,8 @@ abstract class Command extends AbstractCommand
1918
{
2019
/*
2120
* do execute
22-
* @param Input $input
23-
* @param Output $output
21+
* @param \inhere\console\io\Input $input
22+
* @param \inhere\console\io\Output $output
2423
* @return int
2524
*/
2625
// protected function execute($input, $output)

src/Controller.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace inhere\console;
1010

11+
use inhere\console\base\AbstractCommand;
12+
use inhere\console\base\ControllerInterface;
1113
use inhere\console\io\Input;
1214
use inhere\console\io\Output;
1315
use inhere\console\utils\Helper;
@@ -17,7 +19,7 @@
1719
* Class Command
1820
* @package inhere\console
1921
*/
20-
abstract class Controller extends AbstractCommand
22+
abstract class Controller extends AbstractCommand implements ControllerInterface
2123
{
2224
/**
2325
* @var string
Lines changed: 1 addition & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Time: 18:37
77
*/
88

9-
namespace inhere\console;
9+
namespace inhere\console\base;
1010

1111
use inhere\console\io\Input;
1212
use inhere\console\io\Output;
@@ -22,16 +22,6 @@ abstract class AbstractApp implements AppInterface
2222
use InputOutputTrait;
2323
use SimpleEventStaticTrait;
2424

25-
// event name list
26-
const ON_BEFORE_RUN = 'beforeRun';
27-
const ON_AFTER_RUN = 'afterRun';
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';
33-
const ON_NOT_FOUND = 'notFound';
34-
3525
/** @var bool render no color */
3626
private static $noColor = false;
3727

@@ -191,121 +181,6 @@ public function stop($code = 0)
191181
exit((int) $code);
192182
}
193183

194-
/**********************************************************
195-
* register console controller/command
196-
**********************************************************/
197-
198-
/**
199-
* Register a app group command(by controller)
200-
* @param string $name The controller name
201-
* @param string $controller The controller class
202-
* @return static
203-
*/
204-
public function controller(string $name, string $controller = null)
205-
{
206-
if (!$controller && class_exists($name)) {
207-
/** @var Controller $controller */
208-
$controller = $name;
209-
$name = $controller::getName();
210-
}
211-
212-
if (!$name || !$controller) {
213-
throw new \InvalidArgumentException('Group-command "name" and "controller" not allowed to is empty! name: '
214-
. $name . ', controller: ' .$controller);
215-
}
216-
217-
$this->validateName($name, true);
218-
219-
if (!class_exists($controller)) {
220-
throw new \InvalidArgumentException("The console controller class [$controller] not exists!");
221-
}
222-
223-
if (!is_subclass_of($controller, Controller::class)) {
224-
throw new \InvalidArgumentException('The console controller class must is subclass of the: ' . Controller::class);
225-
}
226-
227-
$this->controllers[$name] = $controller;
228-
229-
return $this;
230-
}
231-
232-
/**
233-
* @param array $controllers
234-
*/
235-
public function controllers(array $controllers)
236-
{
237-
foreach ($controllers as $name => $controller) {
238-
if (is_int($name)) {
239-
$this->controller($controller);
240-
} else {
241-
$this->controller($name, $controller);
242-
}
243-
}
244-
}
245-
246-
/**
247-
* Register a app independent console command
248-
* @param string $name
249-
* @param string|\Closure $handler
250-
* @param null|string $description
251-
* @return $this
252-
*/
253-
public function command(string $name, $handler = null, $description = null)
254-
{
255-
if (!$handler && class_exists($name)) {
256-
/** @var Command $handler */
257-
$handler = $name;
258-
$name = $handler::getName();
259-
}
260-
261-
if (!$name || !$handler) {
262-
throw new \InvalidArgumentException('Command "name" and "handler" not allowed to is empty!');
263-
}
264-
265-
$this->validateName($name);
266-
267-
if (isset($this->commands[$name])) {
268-
throw new \InvalidArgumentException("Command '$name' have been registered!");
269-
}
270-
271-
if (is_string($handler)) {
272-
if (!class_exists($handler)) {
273-
throw new \InvalidArgumentException("The console command class [$handler] not exists!");
274-
}
275-
276-
if (!is_subclass_of($handler, Command::class)) {
277-
throw new \InvalidArgumentException('The console command class must is subclass of the: ' . Command::class);
278-
}
279-
} elseif (!is_object($handler) || !method_exists($handler, '__invoke')) {
280-
throw new \InvalidArgumentException(sprintf(
281-
'The console command handler must is an subclass of %s OR a Closure OR a object have method __invoke()',
282-
Command::class
283-
));
284-
}
285-
286-
// is an class name string
287-
$this->commands[$name] = $handler;
288-
289-
if ($description) {
290-
$this->addCommandMessage($name, $description);
291-
}
292-
293-
return $this;
294-
}
295-
296-
/**
297-
* @param array $commands
298-
*/
299-
public function commands(array $commands)
300-
{
301-
foreach ($commands as $name => $handler) {
302-
if (is_int($name)) {
303-
$this->command($handler);
304-
} else {
305-
$this->command($name, $handler);
306-
}
307-
}
308-
}
309184

310185
/**********************************************************
311186
* helper method for the application

0 commit comments

Comments
 (0)