| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Console;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Console\View\Components\Factory;
|
|
|
6 |
use Illuminate\Contracts\Console\Isolatable;
|
|
|
7 |
use Illuminate\Support\Traits\Macroable;
|
|
|
8 |
use Symfony\Component\Console\Command\Command as SymfonyCommand;
|
|
|
9 |
use Symfony\Component\Console\Input\InputInterface;
|
|
|
10 |
use Symfony\Component\Console\Input\InputOption;
|
|
|
11 |
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
12 |
|
|
|
13 |
class Command extends SymfonyCommand
|
|
|
14 |
{
|
|
|
15 |
use Concerns\CallsCommands,
|
|
|
16 |
Concerns\HasParameters,
|
|
|
17 |
Concerns\InteractsWithIO,
|
|
|
18 |
Concerns\InteractsWithSignals,
|
|
|
19 |
Macroable;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* The Laravel application instance.
|
|
|
23 |
*
|
|
|
24 |
* @var \Illuminate\Contracts\Foundation\Application
|
|
|
25 |
*/
|
|
|
26 |
protected $laravel;
|
|
|
27 |
|
|
|
28 |
/**
|
|
|
29 |
* The name and signature of the console command.
|
|
|
30 |
*
|
|
|
31 |
* @var string
|
|
|
32 |
*/
|
|
|
33 |
protected $signature;
|
|
|
34 |
|
|
|
35 |
/**
|
|
|
36 |
* The console command name.
|
|
|
37 |
*
|
|
|
38 |
* @var string
|
|
|
39 |
*/
|
|
|
40 |
protected $name;
|
|
|
41 |
|
|
|
42 |
/**
|
|
|
43 |
* The console command description.
|
|
|
44 |
*
|
|
|
45 |
* @var string|null
|
|
|
46 |
*/
|
|
|
47 |
protected $description;
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* The console command help text.
|
|
|
51 |
*
|
|
|
52 |
* @var string
|
|
|
53 |
*/
|
|
|
54 |
protected $help;
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Indicates whether the command should be shown in the Artisan command list.
|
|
|
58 |
*
|
|
|
59 |
* @var bool
|
|
|
60 |
*/
|
|
|
61 |
protected $hidden = false;
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Create a new console command instance.
|
|
|
65 |
*
|
|
|
66 |
* @return void
|
|
|
67 |
*/
|
|
|
68 |
public function __construct()
|
|
|
69 |
{
|
|
|
70 |
// We will go ahead and set the name, description, and parameters on console
|
|
|
71 |
// commands just to make things a little easier on the developer. This is
|
|
|
72 |
// so they don't have to all be manually specified in the constructors.
|
|
|
73 |
if (isset($this->signature)) {
|
|
|
74 |
$this->configureUsingFluentDefinition();
|
|
|
75 |
} else {
|
|
|
76 |
parent::__construct($this->name);
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
// Once we have constructed the command, we'll set the description and other
|
|
|
80 |
// related properties of the command. If a signature wasn't used to build
|
|
|
81 |
// the command we'll set the arguments and the options on this command.
|
|
|
82 |
if (! isset($this->description)) {
|
|
|
83 |
$this->setDescription((string) static::getDefaultDescription());
|
|
|
84 |
} else {
|
|
|
85 |
$this->setDescription((string) $this->description);
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
$this->setHelp((string) $this->help);
|
|
|
89 |
|
|
|
90 |
$this->setHidden($this->isHidden());
|
|
|
91 |
|
|
|
92 |
if (! isset($this->signature)) {
|
|
|
93 |
$this->specifyParameters();
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
if ($this instanceof Isolatable) {
|
|
|
97 |
$this->configureIsolation();
|
|
|
98 |
}
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
/**
|
|
|
102 |
* Configure the console command using a fluent definition.
|
|
|
103 |
*
|
|
|
104 |
* @return void
|
|
|
105 |
*/
|
|
|
106 |
protected function configureUsingFluentDefinition()
|
|
|
107 |
{
|
|
|
108 |
[$name, $arguments, $options] = Parser::parse($this->signature);
|
|
|
109 |
|
|
|
110 |
parent::__construct($this->name = $name);
|
|
|
111 |
|
|
|
112 |
// After parsing the signature we will spin through the arguments and options
|
|
|
113 |
// and set them on this command. These will already be changed into proper
|
|
|
114 |
// instances of these "InputArgument" and "InputOption" Symfony classes.
|
|
|
115 |
$this->getDefinition()->addArguments($arguments);
|
|
|
116 |
$this->getDefinition()->addOptions($options);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Configure the console command for isolation.
|
|
|
121 |
*
|
|
|
122 |
* @return void
|
|
|
123 |
*/
|
|
|
124 |
protected function configureIsolation()
|
|
|
125 |
{
|
|
|
126 |
$this->getDefinition()->addOption(new InputOption(
|
|
|
127 |
'isolated',
|
|
|
128 |
null,
|
|
|
129 |
InputOption::VALUE_OPTIONAL,
|
|
|
130 |
'Do not run the command if another instance of the command is already running',
|
|
|
131 |
false
|
|
|
132 |
));
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
/**
|
|
|
136 |
* Run the console command.
|
|
|
137 |
*
|
|
|
138 |
* @param \Symfony\Component\Console\Input\InputInterface $input
|
|
|
139 |
* @param \Symfony\Component\Console\Output\OutputInterface $output
|
|
|
140 |
* @return int
|
|
|
141 |
*/
|
|
|
142 |
public function run(InputInterface $input, OutputInterface $output): int
|
|
|
143 |
{
|
|
|
144 |
$this->output = $this->laravel->make(
|
|
|
145 |
OutputStyle::class, ['input' => $input, 'output' => $output]
|
|
|
146 |
);
|
|
|
147 |
|
|
|
148 |
$this->components = $this->laravel->make(Factory::class, ['output' => $this->output]);
|
|
|
149 |
|
|
|
150 |
try {
|
|
|
151 |
return parent::run(
|
|
|
152 |
$this->input = $input, $this->output
|
|
|
153 |
);
|
|
|
154 |
} finally {
|
|
|
155 |
$this->untrap();
|
|
|
156 |
}
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
/**
|
|
|
160 |
* Execute the console command.
|
|
|
161 |
*
|
|
|
162 |
* @param \Symfony\Component\Console\Input\InputInterface $input
|
|
|
163 |
* @param \Symfony\Component\Console\Output\OutputInterface $output
|
|
|
164 |
* @return int
|
|
|
165 |
*/
|
|
|
166 |
protected function execute(InputInterface $input, OutputInterface $output)
|
|
|
167 |
{
|
|
|
168 |
if ($this instanceof Isolatable && $this->option('isolated') !== false &&
|
|
|
169 |
! $this->commandIsolationMutex()->create($this)) {
|
|
|
170 |
$this->comment(sprintf(
|
|
|
171 |
'The [%s] command is already running.', $this->getName()
|
|
|
172 |
));
|
|
|
173 |
|
|
|
174 |
return (int) (is_numeric($this->option('isolated'))
|
|
|
175 |
? $this->option('isolated')
|
|
|
176 |
: self::SUCCESS);
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
$method = method_exists($this, 'handle') ? 'handle' : '__invoke';
|
|
|
180 |
|
|
|
181 |
try {
|
|
|
182 |
return (int) $this->laravel->call([$this, $method]);
|
|
|
183 |
} finally {
|
|
|
184 |
if ($this instanceof Isolatable && $this->option('isolated') !== false) {
|
|
|
185 |
$this->commandIsolationMutex()->forget($this);
|
|
|
186 |
}
|
|
|
187 |
}
|
|
|
188 |
}
|
|
|
189 |
|
|
|
190 |
/**
|
|
|
191 |
* Get a command isolation mutex instance for the command.
|
|
|
192 |
*
|
|
|
193 |
* @return \Illuminate\Console\CommandMutex
|
|
|
194 |
*/
|
|
|
195 |
protected function commandIsolationMutex()
|
|
|
196 |
{
|
|
|
197 |
return $this->laravel->bound(CommandMutex::class)
|
|
|
198 |
? $this->laravel->make(CommandMutex::class)
|
|
|
199 |
: $this->laravel->make(CacheCommandMutex::class);
|
|
|
200 |
}
|
|
|
201 |
|
|
|
202 |
/**
|
|
|
203 |
* Resolve the console command instance for the given command.
|
|
|
204 |
*
|
|
|
205 |
* @param \Symfony\Component\Console\Command\Command|string $command
|
|
|
206 |
* @return \Symfony\Component\Console\Command\Command
|
|
|
207 |
*/
|
|
|
208 |
protected function resolveCommand($command)
|
|
|
209 |
{
|
|
|
210 |
if (! class_exists($command)) {
|
|
|
211 |
return $this->getApplication()->find($command);
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
$command = $this->laravel->make($command);
|
|
|
215 |
|
|
|
216 |
if ($command instanceof SymfonyCommand) {
|
|
|
217 |
$command->setApplication($this->getApplication());
|
|
|
218 |
}
|
|
|
219 |
|
|
|
220 |
if ($command instanceof self) {
|
|
|
221 |
$command->setLaravel($this->getLaravel());
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
return $command;
|
|
|
225 |
}
|
|
|
226 |
|
|
|
227 |
/**
|
|
|
228 |
* {@inheritdoc}
|
|
|
229 |
*
|
|
|
230 |
* @return bool
|
|
|
231 |
*/
|
|
|
232 |
public function isHidden(): bool
|
|
|
233 |
{
|
|
|
234 |
return $this->hidden;
|
|
|
235 |
}
|
|
|
236 |
|
|
|
237 |
/**
|
|
|
238 |
* {@inheritdoc}
|
|
|
239 |
*/
|
|
|
240 |
public function setHidden(bool $hidden = true): static
|
|
|
241 |
{
|
|
|
242 |
parent::setHidden($this->hidden = $hidden);
|
|
|
243 |
|
|
|
244 |
return $this;
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
/**
|
|
|
248 |
* Get the Laravel application instance.
|
|
|
249 |
*
|
|
|
250 |
* @return \Illuminate\Contracts\Foundation\Application
|
|
|
251 |
*/
|
|
|
252 |
public function getLaravel()
|
|
|
253 |
{
|
|
|
254 |
return $this->laravel;
|
|
|
255 |
}
|
|
|
256 |
|
|
|
257 |
/**
|
|
|
258 |
* Set the Laravel application instance.
|
|
|
259 |
*
|
|
|
260 |
* @param \Illuminate\Contracts\Container\Container $laravel
|
|
|
261 |
* @return void
|
|
|
262 |
*/
|
|
|
263 |
public function setLaravel($laravel)
|
|
|
264 |
{
|
|
|
265 |
$this->laravel = $laravel;
|
|
|
266 |
}
|
|
|
267 |
}
|