-
Notifications
You must be signed in to change notification settings - Fork 2
/
CommandCollection.php
247 lines (221 loc) · 7.08 KB
/
CommandCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.5.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Console;
use ArrayIterator;
use Countable;
use InvalidArgumentException;
use IteratorAggregate;
use Traversable;
/**
* Collection for Commands.
*
* Used by Applications to specify their console commands.
* CakePHP will use the mapped commands to construct and dispatch
* shell commands.
*/
class CommandCollection implements IteratorAggregate, Countable
{
/**
* Command list
*
* @var array
* @psalm-var (\Cake\Console\Shell|\Cake\Console\CommandInterface|class-string)[]
* @psalm-suppress DeprecatedClass
*/
protected $commands = [];
/**
* Constructor
*
* @param array $commands The map of commands to add to the collection.
*/
public function __construct(array $commands = [])
{
foreach ($commands as $name => $command) {
$this->add($name, $command);
}
}
/**
* Add a command to the collection
*
* @param string $name The name of the command you want to map.
* @param string|\Cake\Console\Shell|\Cake\Console\CommandInterface $command The command to map.
* Can be a FQCN, Shell instance or CommandInterface instance.
* @return $this
* @throws \InvalidArgumentException
*/
public function add(string $name, $command)
{
if (!is_subclass_of($command, Shell::class) && !is_subclass_of($command, CommandInterface::class)) {
$class = is_string($command) ? $command : get_class($command);
throw new InvalidArgumentException(sprintf(
"Cannot use '%s' for command '%s'. " .
"It is not a subclass of Cake\Console\Shell or Cake\Command\CommandInterface.",
$class,
$name
));
}
if (!preg_match('/^[^\s]+(?:(?: [^\s]+){1,2})?$/ui', $name)) {
throw new InvalidArgumentException(
"The command name `{$name}` is invalid. Names can only be a maximum of three words."
);
}
$this->commands[$name] = $command;
return $this;
}
/**
* Add multiple commands at once.
*
* @param array $commands A map of command names => command classes/instances.
* @return $this
* @see \Cake\Console\CommandCollection::add()
*/
public function addMany(array $commands)
{
foreach ($commands as $name => $class) {
$this->add($name, $class);
}
return $this;
}
/**
* Remove a command from the collection if it exists.
*
* @param string $name The named shell.
* @return $this
*/
public function remove(string $name)
{
unset($this->commands[$name]);
return $this;
}
/**
* Check whether the named shell exists in the collection.
*
* @param string $name The named shell.
* @return bool
*/
public function has(string $name): bool
{
return isset($this->commands[$name]);
}
/**
* Get the target for a command.
*
* @param string $name The named shell.
* @return string|\Cake\Console\Shell|\Cake\Console\CommandInterface Either the command class or an instance.
* @throws \InvalidArgumentException when unknown commands are fetched.
* @psalm-return class-string|\Cake\Console\Shell|\Cake\Console\CommandInterface
*/
public function get(string $name)
{
if (!$this->has($name)) {
throw new InvalidArgumentException("The $name is not a known command name.");
}
return $this->commands[$name];
}
/**
* Implementation of IteratorAggregate.
*
* @return \Traversable
* @psalm-return \Traversable<string, \Cake\Console\Shell|\Cake\Console\CommandInterface|class-string>
*/
public function getIterator(): Traversable
{
return new ArrayIterator($this->commands);
}
/**
* Implementation of Countable.
*
* Get the number of commands in the collection.
*
* @return int
*/
public function count(): int
{
return count($this->commands);
}
/**
* Auto-discover shell & commands from the named plugin.
*
* Discovered commands will have their names de-duplicated with
* existing commands in the collection. If a command is already
* defined in the collection and discovered in a plugin, only
* the long name (`plugin.command`) will be returned.
*
* @param string $plugin The plugin to scan.
* @return string[] Discovered plugin commands.
*/
public function discoverPlugin(string $plugin): array
{
$scanner = new CommandScanner();
$shells = $scanner->scanPlugin($plugin);
return $this->resolveNames($shells);
}
/**
* Resolve names based on existing commands
*
* @param array $input The results of a CommandScanner operation.
* @return string[] A flat map of command names => class names.
*/
protected function resolveNames(array $input): array
{
$out = [];
foreach ($input as $info) {
$name = $info['name'];
$addLong = $name !== $info['fullName'];
// If the short name has been used, use the full name.
// This allows app shells to have name preference.
// and app shells to overwrite core shells.
if ($this->has($name) && $addLong) {
$name = $info['fullName'];
}
$out[$name] = $info['class'];
if ($addLong) {
$out[$info['fullName']] = $info['class'];
}
}
return $out;
}
/**
* Automatically discover shell commands in CakePHP, the application and all plugins.
*
* Commands will be located using filesystem conventions. Commands are
* discovered in the following order:
*
* - CakePHP provided commands
* - Application commands
*
* Commands defined in the application will overwrite commands with
* the same name provided by CakePHP.
*
* @return string[] An array of command names and their classes.
*/
public function autoDiscover(): array
{
$scanner = new CommandScanner();
$core = $this->resolveNames($scanner->scanCore());
$app = $this->resolveNames($scanner->scanApp());
return array_merge($core, $app);
}
/**
* Get the list of available command names.
*
* @return string[] Command names
*/
public function keys(): array
{
return array_keys($this->commands);
}
}