1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bmatovu\ArtisanGui\Support; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Console\Kernel; |
6
|
|
|
use Symfony\Component\Console\Command\Command; |
7
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
8
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
9
|
|
|
use Symfony\Component\Console\Input\InputOption; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @see \Symfony\Component\Console\Descriptor\JsonDescriptor |
13
|
|
|
*/ |
14
|
|
|
class Commander |
15
|
|
|
{ |
16
|
|
|
const GLOBAL_NAMESPACE = 'laravel'; |
17
|
|
|
|
18
|
|
|
private $kernel; |
19
|
|
|
private $namespace; |
20
|
|
|
private $namespaces; |
21
|
|
|
private $commands; |
22
|
|
|
private $aliases; |
23
|
|
|
private $definition; |
24
|
|
|
|
25
|
1 |
|
public function __construct(Kernel $kernel) |
26
|
|
|
{ |
27
|
1 |
|
$this->kernel = $kernel; |
28
|
|
|
|
29
|
1 |
|
$this->loadApplicationDefinition(); |
30
|
|
|
|
31
|
1 |
|
$this->loadNamespacesAndCommands(); |
32
|
1 |
|
} |
33
|
|
|
|
34
|
|
|
public function getKernel() |
35
|
|
|
{ |
36
|
|
|
return $this->kernel; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getNamespace() |
40
|
|
|
{ |
41
|
|
|
return $this->namespace; |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
public function getNamespaces() |
45
|
|
|
{ |
46
|
1 |
|
return $this->namespaces; |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
public function getCommands() |
50
|
|
|
{ |
51
|
1 |
|
return $this->commands; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function getAliases() |
55
|
|
|
{ |
56
|
|
|
return $this->aliases; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function getDefinition() |
60
|
|
|
{ |
61
|
1 |
|
return $this->definition; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
protected function extractNamespace(string $name, int $limit = null) |
65
|
|
|
{ |
66
|
1 |
|
$parts = explode(':', $name, -1); |
67
|
|
|
|
68
|
1 |
|
return implode(':', null === $limit ? $parts : \array_slice($parts, 0, $limit)); |
69
|
|
|
} |
70
|
|
|
|
71
|
1 |
|
protected function sortCommands(array $commands): array |
72
|
|
|
{ |
73
|
1 |
|
$namespacedCommands = []; |
74
|
1 |
|
$globalCommands = []; |
75
|
1 |
|
$sortedCommands = []; |
76
|
1 |
|
foreach ($commands as $name => $command) { |
77
|
1 |
|
$key = $this->extractNamespace($name, 1); |
78
|
1 |
|
if (\in_array($key, ['', self::GLOBAL_NAMESPACE], true)) { |
79
|
1 |
|
$globalCommands[$name] = $command; |
80
|
|
|
} else { |
81
|
1 |
|
$namespacedCommands[$key][$name] = $command; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
if ($globalCommands) { |
|
|
|
|
86
|
1 |
|
ksort($globalCommands); |
87
|
1 |
|
$sortedCommands[self::GLOBAL_NAMESPACE] = $globalCommands; |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
if ($namespacedCommands) { |
|
|
|
|
91
|
1 |
|
ksort($namespacedCommands); |
92
|
1 |
|
foreach ($namespacedCommands as $key => $commandsSet) { |
93
|
1 |
|
ksort($commandsSet); |
94
|
1 |
|
$sortedCommands[$key] = $commandsSet; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
return $sortedCommands; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @see \Symfony\Component\Console\Descriptor\ApplicationDescription::inspectApplication |
103
|
|
|
*/ |
104
|
1 |
|
public function loadNamespacesAndCommands() |
105
|
|
|
{ |
106
|
1 |
|
$this->commands = []; |
107
|
1 |
|
$this->namespaces = []; |
108
|
|
|
|
109
|
1 |
|
$all = $this->kernel->all($this->namespace); |
|
|
|
|
110
|
|
|
|
111
|
1 |
|
foreach ($this->sortCommands($all) as $namespace => $commands) { |
112
|
1 |
|
$names = []; |
113
|
|
|
|
114
|
|
|
/** @var Command $command */ |
115
|
1 |
|
foreach ($commands as $name => $command) { |
116
|
|
|
// if (!$command->getName() || (!$this->showHidden && $command->isHidden())) { |
117
|
1 |
|
if (! $command->getName()) { |
118
|
|
|
continue; |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
if ($command->getName() === $name) { |
122
|
1 |
|
$this->commands[$name] = $this->getCommandData($command); |
123
|
|
|
} else { |
124
|
|
|
$this->aliases[$name] = $command; |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
$names[] = $name; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
// $this->namespaces[$namespace] = ['id' => $namespace, 'commands' => $names]; |
131
|
1 |
|
$this->namespaces[$namespace] = $names; |
132
|
|
|
} |
133
|
1 |
|
} |
134
|
|
|
|
135
|
1 |
|
protected function loadApplicationDefinition() |
136
|
|
|
{ |
137
|
1 |
|
$this->definition = []; |
138
|
|
|
|
139
|
1 |
|
$commands = $this->kernel->all(); |
140
|
|
|
|
141
|
1 |
|
$command = array_values($commands)[0]; |
142
|
|
|
|
143
|
1 |
|
$application = $command->getApplication(); |
144
|
|
|
|
145
|
1 |
|
$this->definition = $this->getInputDefinitionData($application->getDefinition()); |
146
|
|
|
|
147
|
|
|
// $this->arguments = $this->getInputDefinitionArguments($application->getDefinition()); |
148
|
|
|
|
149
|
|
|
// $this->options = $this->getInputDefinitionOptions($application->getDefinition()); |
150
|
1 |
|
} |
151
|
|
|
|
152
|
1 |
|
protected function getInputArgumentData(InputArgument $argument): array |
153
|
|
|
{ |
154
|
|
|
return [ |
155
|
1 |
|
'name' => $argument->getName(), |
156
|
1 |
|
'is_required' => $argument->isRequired(), |
157
|
1 |
|
'is_array' => $argument->isArray(), |
158
|
1 |
|
'description' => preg_replace('/\s*[\r\n]\s*/', ' ', $argument->getDescription()), |
159
|
1 |
|
'value' => \INF === $argument->getDefault() ? 'INF' : $argument->getDefault(), |
160
|
|
|
]; |
161
|
|
|
} |
162
|
|
|
|
163
|
1 |
|
protected function getInputOptionData(InputOption $option): array |
164
|
|
|
{ |
165
|
|
|
return [ |
166
|
1 |
|
'name' => '--'.$option->getName(), |
167
|
1 |
|
'shortcut' => $option->getShortcut() ? '-'.str_replace('|', '|-', $option->getShortcut()) : '', |
168
|
1 |
|
'is_flag' => ! $option->acceptValue(), |
169
|
1 |
|
'is_required' => $option->isValueRequired(), |
170
|
1 |
|
'is_array' => $option->isArray(), |
171
|
1 |
|
'description' => preg_replace('/\s*[\r\n]\s*/', ' ', $option->getDescription()), |
172
|
1 |
|
'value' => \INF === $option->getDefault() ? 'INF' : $option->getDefault(), |
173
|
|
|
]; |
174
|
|
|
} |
175
|
|
|
|
176
|
1 |
|
protected function getInputDefinitionData(InputDefinition $definition): array |
177
|
|
|
{ |
178
|
1 |
|
$inputArguments = []; |
179
|
1 |
|
foreach ($definition->getArguments() as $name => $argument) { |
180
|
1 |
|
$inputArguments[$name] = $this->getInputArgumentData($argument); |
181
|
|
|
} |
182
|
|
|
|
183
|
1 |
|
$inputOptions = []; |
184
|
1 |
|
foreach ($definition->getOptions() as $name => $option) { |
185
|
1 |
|
$inputOptions[$name] = $this->getInputOptionData($option); |
186
|
|
|
} |
187
|
|
|
|
188
|
1 |
|
return ['arguments' => $inputArguments, 'options' => $inputOptions]; |
189
|
|
|
} |
190
|
|
|
|
191
|
1 |
|
protected function getInputDefinitionArguments(InputDefinition $definition): array |
192
|
|
|
{ |
193
|
1 |
|
$inputArguments = []; |
194
|
1 |
|
foreach ($definition->getArguments() as $name => $argument) { |
195
|
1 |
|
$inputArguments[$name] = $this->getInputArgumentData($argument); |
196
|
|
|
} |
197
|
|
|
|
198
|
1 |
|
return $inputArguments; |
199
|
|
|
} |
200
|
|
|
|
201
|
1 |
|
protected function getInputDefinitionOptions(InputDefinition $definition): array |
202
|
|
|
{ |
203
|
1 |
|
$inputOptions = []; |
204
|
1 |
|
foreach ($definition->getOptions() as $name => $option) { |
205
|
1 |
|
$inputOptions[$name] = $this->getInputOptionData($option); |
206
|
|
|
} |
207
|
|
|
|
208
|
1 |
|
return $inputOptions; |
209
|
|
|
} |
210
|
|
|
|
211
|
1 |
|
protected function getCommandData(Command $command): array |
212
|
|
|
{ |
213
|
|
|
// $command->mergeApplicationDefinition(false); |
214
|
|
|
|
215
|
|
|
return [ |
216
|
1 |
|
'name' => $command->getName(), |
217
|
1 |
|
'synopsis' => $command->getSynopsis(false), |
218
|
|
|
// 'usages' => $command->getUsages(), |
219
|
|
|
// 'aliases' => $command->getAliases(), |
220
|
1 |
|
'description' => $command->getDescription(), |
221
|
1 |
|
'help' => $command->getProcessedHelp(), |
222
|
|
|
// 'definition' => $this->getInputDefinitionData($command->getDefinition()), |
223
|
1 |
|
'arguments' => $this->getInputDefinitionArguments($command->getDefinition()), |
224
|
1 |
|
'options' => $this->getInputDefinitionOptions($command->getDefinition()), |
225
|
1 |
|
'hidden' => $command->isHidden(), |
226
|
|
|
]; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.