comparison vendor/symfony/console/Command/Command.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents c2387f117808
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
9 * file that was distributed with this source code. 9 * file that was distributed with this source code.
10 */ 10 */
11 11
12 namespace Symfony\Component\Console\Command; 12 namespace Symfony\Component\Console\Command;
13 13
14 use Symfony\Component\Console\Application;
14 use Symfony\Component\Console\Exception\ExceptionInterface; 15 use Symfony\Component\Console\Exception\ExceptionInterface;
15 use Symfony\Component\Console\Input\InputDefinition;
16 use Symfony\Component\Console\Input\InputOption;
17 use Symfony\Component\Console\Input\InputArgument;
18 use Symfony\Component\Console\Input\InputInterface;
19 use Symfony\Component\Console\Output\OutputInterface;
20 use Symfony\Component\Console\Application;
21 use Symfony\Component\Console\Helper\HelperSet;
22 use Symfony\Component\Console\Exception\InvalidArgumentException; 16 use Symfony\Component\Console\Exception\InvalidArgumentException;
23 use Symfony\Component\Console\Exception\LogicException; 17 use Symfony\Component\Console\Exception\LogicException;
18 use Symfony\Component\Console\Helper\HelperSet;
19 use Symfony\Component\Console\Input\InputArgument;
20 use Symfony\Component\Console\Input\InputDefinition;
21 use Symfony\Component\Console\Input\InputInterface;
22 use Symfony\Component\Console\Input\InputOption;
23 use Symfony\Component\Console\Output\OutputInterface;
24 24
25 /** 25 /**
26 * Base class for all commands. 26 * Base class for all commands.
27 * 27 *
28 * @author Fabien Potencier <fabien@symfony.com> 28 * @author Fabien Potencier <fabien@symfony.com>
35 protected static $defaultName; 35 protected static $defaultName;
36 36
37 private $application; 37 private $application;
38 private $name; 38 private $name;
39 private $processTitle; 39 private $processTitle;
40 private $aliases = array(); 40 private $aliases = [];
41 private $definition; 41 private $definition;
42 private $hidden = false; 42 private $hidden = false;
43 private $help; 43 private $help;
44 private $description; 44 private $description;
45 private $ignoreValidationErrors = false; 45 private $ignoreValidationErrors = false;
46 private $applicationDefinitionMerged = false; 46 private $applicationDefinitionMerged = false;
47 private $applicationDefinitionMergedWithArgs = false; 47 private $applicationDefinitionMergedWithArgs = false;
48 private $code; 48 private $code;
49 private $synopsis = array(); 49 private $synopsis = [];
50 private $usages = array(); 50 private $usages = [];
51 private $helperSet; 51 private $helperSet;
52 52
53 /** 53 /**
54 * @return string|null The default command name or null when no default name is set 54 * @return string|null The default command name or null when no default name is set
55 */ 55 */
56 public static function getDefaultName() 56 public static function getDefaultName()
57 { 57 {
58 $class = get_called_class(); 58 $class = \get_called_class();
59 $r = new \ReflectionProperty($class, 'defaultName'); 59 $r = new \ReflectionProperty($class, 'defaultName');
60 60
61 return $class === $r->class ? static::$defaultName : null; 61 return $class === $r->class ? static::$defaultName : null;
62 } 62 }
63 63
148 * This method is not abstract because you can use this class 148 * This method is not abstract because you can use this class
149 * as a concrete class. In this case, instead of defining the 149 * as a concrete class. In this case, instead of defining the
150 * execute() method, you set the code to execute by passing 150 * execute() method, you set the code to execute by passing
151 * a Closure to the setCode() method. 151 * a Closure to the setCode() method.
152 * 152 *
153 * @return null|int null or 0 if everything went fine, or an error code 153 * @return int|null null or 0 if everything went fine, or an error code
154 * 154 *
155 * @throws LogicException When this abstract method is not implemented 155 * @throws LogicException When this abstract method is not implemented
156 * 156 *
157 * @see setCode() 157 * @see setCode()
158 */ 158 */
171 protected function interact(InputInterface $input, OutputInterface $output) 171 protected function interact(InputInterface $input, OutputInterface $output)
172 { 172 {
173 } 173 }
174 174
175 /** 175 /**
176 * Initializes the command just after the input has been validated. 176 * Initializes the command after the input has been bound and before the input
177 * is validated.
177 * 178 *
178 * This is mainly useful when a lot of commands extends one main command 179 * This is mainly useful when a lot of commands extends one main command
179 * where some things need to be initialized based on the input arguments and options. 180 * where some things need to be initialized based on the input arguments and options.
181 *
182 * @see InputInterface::bind()
183 * @see InputInterface::validate()
180 */ 184 */
181 protected function initialize(InputInterface $input, OutputInterface $output) 185 protected function initialize(InputInterface $input, OutputInterface $output)
182 { 186 {
183 } 187 }
184 188
215 } 219 }
216 220
217 $this->initialize($input, $output); 221 $this->initialize($input, $output);
218 222
219 if (null !== $this->processTitle) { 223 if (null !== $this->processTitle) {
220 if (function_exists('cli_set_process_title')) { 224 if (\function_exists('cli_set_process_title')) {
221 if (!@cli_set_process_title($this->processTitle)) { 225 if (!@cli_set_process_title($this->processTitle)) {
222 if ('Darwin' === PHP_OS) { 226 if ('Darwin' === PHP_OS) {
223 $output->writeln('<comment>Running "cli_get_process_title" as an unprivileged user is not supported on MacOS.</comment>'); 227 $output->writeln('<comment>Running "cli_set_process_title" as an unprivileged user is not supported on MacOS.</comment>', OutputInterface::VERBOSITY_VERY_VERBOSE);
224 } else { 228 } else {
225 cli_set_process_title($this->processTitle); 229 cli_set_process_title($this->processTitle);
226 } 230 }
227 } 231 }
228 } elseif (function_exists('setproctitle')) { 232 } elseif (\function_exists('setproctitle')) {
229 setproctitle($this->processTitle); 233 setproctitle($this->processTitle);
230 } elseif (OutputInterface::VERBOSITY_VERY_VERBOSE === $output->getVerbosity()) { 234 } elseif (OutputInterface::VERBOSITY_VERY_VERBOSE === $output->getVerbosity()) {
231 $output->writeln('<comment>Install the proctitle PECL to be able to change the process title.</comment>'); 235 $output->writeln('<comment>Install the proctitle PECL to be able to change the process title.</comment>');
232 } 236 }
233 } 237 }
244 } 248 }
245 249
246 $input->validate(); 250 $input->validate();
247 251
248 if ($this->code) { 252 if ($this->code) {
249 $statusCode = call_user_func($this->code, $input, $output); 253 $statusCode = \call_user_func($this->code, $input, $output);
250 } else { 254 } else {
251 $statusCode = $this->execute($input, $output); 255 $statusCode = $this->execute($input, $output);
252 } 256 }
253 257
254 return is_numeric($statusCode) ? (int) $statusCode : 0; 258 return is_numeric($statusCode) ? (int) $statusCode : 0;
303 return; 307 return;
304 } 308 }
305 309
306 $this->definition->addOptions($this->application->getDefinition()->getOptions()); 310 $this->definition->addOptions($this->application->getDefinition()->getOptions());
307 311
312 $this->applicationDefinitionMerged = true;
313
308 if ($mergeArgs) { 314 if ($mergeArgs) {
309 $currentArguments = $this->definition->getArguments(); 315 $currentArguments = $this->definition->getArguments();
310 $this->definition->setArguments($this->application->getDefinition()->getArguments()); 316 $this->definition->setArguments($this->application->getDefinition()->getArguments());
311 $this->definition->addArguments($currentArguments); 317 $this->definition->addArguments($currentArguments);
312 } 318
313
314 $this->applicationDefinitionMerged = true;
315 if ($mergeArgs) {
316 $this->applicationDefinitionMergedWithArgs = true; 319 $this->applicationDefinitionMergedWithArgs = true;
317 } 320 }
318 } 321 }
319 322
320 /** 323 /**
363 } 366 }
364 367
365 /** 368 /**
366 * Adds an argument. 369 * Adds an argument.
367 * 370 *
368 * @param string $name The argument name 371 * @param string $name The argument name
369 * @param int $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL 372 * @param int|null $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
370 * @param string $description A description text 373 * @param string $description A description text
371 * @param mixed $default The default value (for InputArgument::OPTIONAL mode only) 374 * @param string|string[]|null $default The default value (for InputArgument::OPTIONAL mode only)
375 *
376 * @throws InvalidArgumentException When argument mode is not valid
372 * 377 *
373 * @return $this 378 * @return $this
374 */ 379 */
375 public function addArgument($name, $mode = null, $description = '', $default = null) 380 public function addArgument($name, $mode = null, $description = '', $default = null)
376 { 381 {
380 } 385 }
381 386
382 /** 387 /**
383 * Adds an option. 388 * Adds an option.
384 * 389 *
385 * @param string $name The option name 390 * @param string $name The option name
386 * @param string $shortcut The shortcut (can be null) 391 * @param string|array|null $shortcut The shortcuts, can be null, a string of shortcuts delimited by | or an array of shortcuts
387 * @param int $mode The option mode: One of the InputOption::VALUE_* constants 392 * @param int|null $mode The option mode: One of the InputOption::VALUE_* constants
388 * @param string $description A description text 393 * @param string $description A description text
389 * @param mixed $default The default value (must be null for InputOption::VALUE_NONE) 394 * @param string|string[]|int|bool|null $default The default value (must be null for InputOption::VALUE_NONE)
395 *
396 * @throws InvalidArgumentException If option mode is invalid or incompatible
390 * 397 *
391 * @return $this 398 * @return $this
392 */ 399 */
393 public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null) 400 public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null)
394 { 401 {
524 * @return string The processed help for the command 531 * @return string The processed help for the command
525 */ 532 */
526 public function getProcessedHelp() 533 public function getProcessedHelp()
527 { 534 {
528 $name = $this->name; 535 $name = $this->name;
529 536 $isSingleCommand = $this->application && $this->application->isSingleCommand();
530 $placeholders = array( 537
538 $placeholders = [
531 '%command.name%', 539 '%command.name%',
532 '%command.full_name%', 540 '%command.full_name%',
533 ); 541 ];
534 $replacements = array( 542 $replacements = [
535 $name, 543 $name,
536 $_SERVER['PHP_SELF'].' '.$name, 544 $isSingleCommand ? $_SERVER['PHP_SELF'] : $_SERVER['PHP_SELF'].' '.$name,
537 ); 545 ];
538 546
539 return str_replace($placeholders, $replacements, $this->getHelp() ?: $this->getDescription()); 547 return str_replace($placeholders, $replacements, $this->getHelp() ?: $this->getDescription());
540 } 548 }
541 549
542 /** 550 /**
548 * 556 *
549 * @throws InvalidArgumentException When an alias is invalid 557 * @throws InvalidArgumentException When an alias is invalid
550 */ 558 */
551 public function setAliases($aliases) 559 public function setAliases($aliases)
552 { 560 {
553 if (!is_array($aliases) && !$aliases instanceof \Traversable) { 561 if (!\is_array($aliases) && !$aliases instanceof \Traversable) {
554 throw new InvalidArgumentException('$aliases must be an array or an instance of \Traversable'); 562 throw new InvalidArgumentException('$aliases must be an array or an instance of \Traversable');
555 } 563 }
556 564
557 foreach ($aliases as $alias) { 565 foreach ($aliases as $alias) {
558 $this->validateName($alias); 566 $this->validateName($alias);