-
Notifications
You must be signed in to change notification settings - Fork 26
/
Command.php
executable file
·327 lines (281 loc) · 8.63 KB
/
Command.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
namespace Illuminate\Console;
use Illuminate\Console\View\Components\Factory;
use Illuminate\Contracts\Console\Isolatable;
use Illuminate\Support\Traits\Macroable;
use Symfony\Component\Console\Command\Command as SymfonyCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Throwable;
class Command extends SymfonyCommand
{
use Concerns\CallsCommands,
Concerns\ConfiguresPrompts,
Concerns\HasParameters,
Concerns\InteractsWithIO,
Concerns\InteractsWithSignals,
Concerns\PromptsForMissingInput,
Macroable;
/**
* The Laravel application instance.
*
* @var \Illuminate\Contracts\Foundation\Application
*/
protected $laravel;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature;
/**
* The console command name.
*
* @var string
*/
protected $name;
/**
* The console command description.
*
* @var string|null
*/
protected $description;
/**
* The console command help text.
*
* @var string
*/
protected $help;
/**
* Indicates whether the command should be shown in the Artisan command list.
*
* @var bool
*/
protected $hidden = false;
/**
* Indicates whether only one instance of the command can run at any given time.
*
* @var bool
*/
protected $isolated = false;
/**
* The default exit code for isolated commands.
*
* @var int
*/
protected $isolatedExitCode = self::SUCCESS;
/**
* The console command name aliases.
*
* @var array
*/
protected $aliases;
/**
* Create a new console command instance.
*
* @return void
*/
public function __construct()
{
// We will go ahead and set the name, description, and parameters on console
// commands just to make things a little easier on the developer. This is
// so they don't have to all be manually specified in the constructors.
if (isset($this->signature)) {
$this->configureUsingFluentDefinition();
} else {
parent::__construct($this->name);
}
// Once we have constructed the command, we'll set the description and other
// related properties of the command. If a signature wasn't used to build
// the command we'll set the arguments and the options on this command.
if (! isset($this->description)) {
$this->setDescription((string) static::getDefaultDescription());
} else {
$this->setDescription((string) $this->description);
}
$this->setHelp((string) $this->help);
$this->setHidden($this->isHidden());
if (isset($this->aliases)) {
$this->setAliases((array) $this->aliases);
}
if (! isset($this->signature)) {
$this->specifyParameters();
}
if ($this instanceof Isolatable) {
$this->configureIsolation();
}
}
/**
* Configure the console command using a fluent definition.
*
* @return void
*/
protected function configureUsingFluentDefinition()
{
[$name, $arguments, $options] = Parser::parse($this->signature);
parent::__construct($this->name = $name);
// After parsing the signature we will spin through the arguments and options
// and set them on this command. These will already be changed into proper
// instances of these "InputArgument" and "InputOption" Symfony classes.
$this->getDefinition()->addArguments($arguments);
$this->getDefinition()->addOptions($options);
}
/**
* Configure the console command for isolation.
*
* @return void
*/
protected function configureIsolation()
{
$this->getDefinition()->addOption(new InputOption(
'isolated',
null,
InputOption::VALUE_OPTIONAL,
'Do not run the command if another instance of the command is already running',
$this->isolated
));
}
/**
* Run the console command.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @return int
*/
#[\Override]
public function run(InputInterface $input, OutputInterface $output): int
{
$this->output = $output instanceof OutputStyle ? $output : $this->laravel->make(
OutputStyle::class, ['input' => $input, 'output' => $output]
);
$this->components = $this->laravel->make(Factory::class, ['output' => $this->output]);
$this->configurePrompts($input);
try {
return parent::run(
$this->input = $input, $this->output
);
} finally {
$this->untrap();
}
}
/**
* Execute the console command.
*
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*/
#[\Override]
protected function execute(InputInterface $input, OutputInterface $output): int
{
if ($this instanceof Isolatable && $this->option('isolated') !== false &&
! $this->commandIsolationMutex()->create($this)) {
$this->comment(sprintf(
'The [%s] command is already running.', $this->getName()
));
return (int) (is_numeric($this->option('isolated'))
? $this->option('isolated')
: $this->isolatedExitCode);
}
$method = method_exists($this, 'handle') ? 'handle' : '__invoke';
try {
return (int) $this->laravel->call([$this, $method]);
} catch (ManuallyFailedException $e) {
$this->components->error($e->getMessage());
return static::FAILURE;
} finally {
if ($this instanceof Isolatable && $this->option('isolated') !== false) {
$this->commandIsolationMutex()->forget($this);
}
}
}
/**
* Get a command isolation mutex instance for the command.
*
* @return \Illuminate\Console\CommandMutex
*/
protected function commandIsolationMutex()
{
return $this->laravel->bound(CommandMutex::class)
? $this->laravel->make(CommandMutex::class)
: $this->laravel->make(CacheCommandMutex::class);
}
/**
* Resolve the console command instance for the given command.
*
* @param \Symfony\Component\Console\Command\Command|string $command
* @return \Symfony\Component\Console\Command\Command
*/
protected function resolveCommand($command)
{
if (is_string($command)) {
if (! class_exists($command)) {
return $this->getApplication()->find($command);
}
$command = $this->laravel->make($command);
}
if ($command instanceof SymfonyCommand) {
$command->setApplication($this->getApplication());
}
if ($command instanceof self) {
$command->setLaravel($this->getLaravel());
}
return $command;
}
/**
* Fail the command manually.
*
* @param \Throwable|string|null $exception
* @return void
*
* @throws \Illuminate\Console\ManuallyFailedException|\Throwable
*/
public function fail(Throwable|string|null $exception = null)
{
if (is_null($exception)) {
$exception = 'Command failed manually.';
}
if (is_string($exception)) {
$exception = new ManuallyFailedException($exception);
}
throw $exception;
}
/**
* {@inheritdoc}
*
* @return bool
*/
#[\Override]
public function isHidden(): bool
{
return $this->hidden;
}
/**
* {@inheritdoc}
*/
#[\Override]
public function setHidden(bool $hidden = true): static
{
parent::setHidden($this->hidden = $hidden);
return $this;
}
/**
* Get the Laravel application instance.
*
* @return \Illuminate\Contracts\Foundation\Application
*/
public function getLaravel()
{
return $this->laravel;
}
/**
* Set the Laravel application instance.
*
* @param \Illuminate\Contracts\Container\Container $laravel
* @return void
*/
public function setLaravel($laravel)
{
$this->laravel = $laravel;
}
}