Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
34 / 34 |
|
100.00% |
14 / 14 |
CRAP | |
100.00% |
1 / 1 |
Command | |
100.00% |
34 / 34 |
|
100.00% |
14 / 14 |
19 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
run | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getConsole | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setConsole | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getName | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
setName | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getDescription | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
setDescription | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getUsage | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setUsage | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getOptions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
setOptions | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
isActive | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
activate | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
deactivate | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
1 | <?php declare(strict_types=1); |
2 | /* |
3 | * This file is part of Aplus Framework CLI Library. |
4 | * |
5 | * (c) Natan Felles <natanfelles@gmail.com> |
6 | * |
7 | * For the full copyright and license information, please view the LICENSE |
8 | * file that was distributed with this source code. |
9 | */ |
10 | namespace Framework\CLI; |
11 | |
12 | use JetBrains\PhpStorm\Pure; |
13 | |
14 | /** |
15 | * Class Command. |
16 | * |
17 | * @package cli |
18 | */ |
19 | abstract class Command |
20 | { |
21 | /** |
22 | * Console instance of the current command. |
23 | */ |
24 | protected Console $console; |
25 | /** |
26 | * Command name. |
27 | */ |
28 | protected string $name; |
29 | /** |
30 | * Command description. |
31 | */ |
32 | protected string $description; |
33 | /** |
34 | * Command usage. |
35 | */ |
36 | protected string $usage = 'command [options] -- [arguments]'; |
37 | /** |
38 | * Command options. |
39 | * |
40 | * @var array<string,string> |
41 | */ |
42 | protected array $options = []; |
43 | /** |
44 | * Tells if command is active. |
45 | */ |
46 | protected bool $active = true; |
47 | |
48 | /** |
49 | * Command constructor. |
50 | * |
51 | * @param Console|null $console |
52 | */ |
53 | public function __construct(Console $console = null) |
54 | { |
55 | if ($console) { |
56 | $this->console = $console; |
57 | } |
58 | } |
59 | |
60 | /** |
61 | * Run the command. |
62 | */ |
63 | abstract public function run() : void; |
64 | |
65 | /** |
66 | * Get console instance. |
67 | * |
68 | * @return Console |
69 | */ |
70 | public function getConsole() : Console |
71 | { |
72 | return $this->console; |
73 | } |
74 | |
75 | /** |
76 | * Set console instance. |
77 | * |
78 | * @param Console $console |
79 | * |
80 | * @return static |
81 | */ |
82 | public function setConsole(Console $console) : static |
83 | { |
84 | $this->console = $console; |
85 | return $this; |
86 | } |
87 | |
88 | /** |
89 | * Get command name. |
90 | * |
91 | * @return string |
92 | */ |
93 | public function getName() : string |
94 | { |
95 | if (isset($this->name)) { |
96 | return $this->name; |
97 | } |
98 | $name = static::class; |
99 | $pos = \strrpos($name, '\\'); |
100 | if ($pos !== false) { |
101 | $name = \substr($name, $pos + 1); |
102 | } |
103 | if (\str_ends_with($name, 'Command')) { |
104 | $name = \substr($name, 0, -7); |
105 | } |
106 | $name = \strtolower($name); |
107 | return $this->name = $name; |
108 | } |
109 | |
110 | /** |
111 | * Set command name. |
112 | * |
113 | * @param string $name |
114 | * |
115 | * @return static |
116 | */ |
117 | public function setName(string $name) : static |
118 | { |
119 | $this->name = $name; |
120 | return $this; |
121 | } |
122 | |
123 | /** |
124 | * Get command description. |
125 | * |
126 | * @return string |
127 | */ |
128 | public function getDescription() : string |
129 | { |
130 | if (isset($this->description)) { |
131 | return $this->description; |
132 | } |
133 | $description = $this->console->getLanguage()->render('cli', 'noDescription'); |
134 | return $this->description = $description; |
135 | } |
136 | |
137 | /** |
138 | * Set command description. |
139 | * |
140 | * @param string $description |
141 | * |
142 | * @return static |
143 | */ |
144 | public function setDescription(string $description) : static |
145 | { |
146 | $this->description = $description; |
147 | return $this; |
148 | } |
149 | |
150 | /** |
151 | * Get command usage. |
152 | * |
153 | * @return string |
154 | */ |
155 | #[Pure] |
156 | public function getUsage() : string |
157 | { |
158 | return $this->usage; |
159 | } |
160 | |
161 | /** |
162 | * Set command usage. |
163 | * |
164 | * @param string $usage |
165 | * |
166 | * @return static |
167 | */ |
168 | public function setUsage(string $usage) : static |
169 | { |
170 | $this->usage = $usage; |
171 | return $this; |
172 | } |
173 | |
174 | /** |
175 | * Get command options. |
176 | * |
177 | * @return array<string,string> |
178 | */ |
179 | #[Pure] |
180 | public function getOptions() : array |
181 | { |
182 | return $this->options; |
183 | } |
184 | |
185 | /** |
186 | * Set command options. |
187 | * |
188 | * @param array<string,string> $options |
189 | * |
190 | * @return static |
191 | */ |
192 | public function setOptions(array $options) : static |
193 | { |
194 | $this->options = $options; |
195 | return $this; |
196 | } |
197 | |
198 | /** |
199 | * Tells if the command is active. |
200 | * |
201 | * @return bool |
202 | */ |
203 | #[Pure] |
204 | public function isActive() : bool |
205 | { |
206 | return $this->active; |
207 | } |
208 | |
209 | /** |
210 | * Activate the command. |
211 | * |
212 | * @return static |
213 | */ |
214 | public function activate() : static |
215 | { |
216 | $this->active = true; |
217 | return $this; |
218 | } |
219 | |
220 | /** |
221 | * Deactivate the command. |
222 | * |
223 | * @return static |
224 | */ |
225 | public function deactivate() : static |
226 | { |
227 | $this->active = false; |
228 | return $this; |
229 | } |
230 | } |