1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Console initializer |
4
|
|
|
* User: moyo |
5
|
|
|
* Date: 12/12/2017 |
6
|
|
|
* Time: 4:20 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Carno\Console; |
10
|
|
|
|
11
|
|
|
use Carno\Console\Chips\CMDKit; |
12
|
|
|
use Carno\Console\Chips\Dumps; |
13
|
|
|
use Carno\Container\DI; |
14
|
|
|
use Symfony\Component\Console\Application; |
15
|
|
|
use Throwable; |
16
|
|
|
|
17
|
|
|
class Initializer |
18
|
|
|
{ |
19
|
|
|
use CMDKit; |
20
|
|
|
use Dumps; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Application |
24
|
|
|
*/ |
25
|
|
|
private $app = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Bootstrap |
29
|
|
|
*/ |
30
|
|
|
private $boot = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Initializer constructor. |
34
|
|
|
*/ |
35
|
|
|
public function __construct() |
36
|
|
|
{ |
37
|
|
|
DI::set(Bootstrap::class, $this->boot = DI::object(Bootstrap::class)); |
38
|
|
|
|
39
|
|
|
($this->app = new Application()) |
40
|
|
|
->setCatchExceptions(false) |
41
|
|
|
; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return static |
46
|
|
|
*/ |
47
|
|
|
public function components() : self |
48
|
|
|
{ |
49
|
|
|
foreach (get_defined_constants(true)['user'] ?? [] as $name => $value) { |
50
|
|
|
if (substr($name, 5, 12) === '_COMPONENTS_') { |
51
|
|
|
$this->bootstrap(...$value); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string ...$mods |
59
|
|
|
* @return static |
60
|
|
|
*/ |
61
|
|
|
public function bootstrap(string ...$mods) : self |
62
|
|
|
{ |
63
|
|
|
$this->boot->register(...$mods); |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return static |
69
|
|
|
*/ |
70
|
|
|
public function additions() : self |
71
|
|
|
{ |
72
|
|
|
$this->addCommand(...$this->provides()); |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string ...$cmds |
78
|
|
|
* @return static |
79
|
|
|
*/ |
80
|
|
|
public function commands(string ...$cmds) : self |
81
|
|
|
{ |
82
|
|
|
$this->addCommand(...$cmds); |
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
*/ |
88
|
|
|
public function start() : void |
89
|
|
|
{ |
90
|
|
|
try { |
91
|
|
|
$this->app->run(); |
92
|
|
|
} catch (Throwable $e) { |
93
|
|
|
$this->failure($e, 1); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $named |
99
|
|
|
* @return Proxy |
100
|
|
|
*/ |
101
|
|
|
public function proxy(string $named) : Proxy |
102
|
|
|
{ |
103
|
|
|
return $this->app->get($named); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string ...$classes |
108
|
|
|
*/ |
109
|
|
|
private function addCommand(string ...$classes) : void |
110
|
|
|
{ |
111
|
|
|
foreach ($classes as $class) { |
112
|
|
|
$this->app->add( |
113
|
|
|
$this->setCommandPTS( |
114
|
|
|
(new Proxy())->setBootstrap($this->boot)->setCommand($class), |
115
|
|
|
$class, |
116
|
|
|
['app', 'name', 'help', 'description'] |
117
|
|
|
) |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
private function provides() : array |
126
|
|
|
{ |
127
|
|
|
return (defined('CWD') && is_file($cf = CWD . '/commands.php')) ? (array) include $cf : []; |
|
|
|
|
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|