annotate vendor/symfony/console/Command/Command.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 7a779792577d
children c2387f117808
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Component\Console\Command;
Chris@0 13
Chris@0 14 use Symfony\Component\Console\Exception\ExceptionInterface;
Chris@0 15 use Symfony\Component\Console\Input\InputDefinition;
Chris@0 16 use Symfony\Component\Console\Input\InputOption;
Chris@0 17 use Symfony\Component\Console\Input\InputArgument;
Chris@0 18 use Symfony\Component\Console\Input\InputInterface;
Chris@0 19 use Symfony\Component\Console\Output\OutputInterface;
Chris@0 20 use Symfony\Component\Console\Application;
Chris@0 21 use Symfony\Component\Console\Helper\HelperSet;
Chris@0 22 use Symfony\Component\Console\Exception\InvalidArgumentException;
Chris@0 23 use Symfony\Component\Console\Exception\LogicException;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * Base class for all commands.
Chris@0 27 *
Chris@0 28 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 29 */
Chris@0 30 class Command
Chris@0 31 {
Chris@14 32 /**
Chris@14 33 * @var string|null The default command name
Chris@14 34 */
Chris@14 35 protected static $defaultName;
Chris@14 36
Chris@0 37 private $application;
Chris@0 38 private $name;
Chris@0 39 private $processTitle;
Chris@0 40 private $aliases = array();
Chris@0 41 private $definition;
Chris@0 42 private $hidden = false;
Chris@0 43 private $help;
Chris@0 44 private $description;
Chris@0 45 private $ignoreValidationErrors = false;
Chris@0 46 private $applicationDefinitionMerged = false;
Chris@0 47 private $applicationDefinitionMergedWithArgs = false;
Chris@0 48 private $code;
Chris@0 49 private $synopsis = array();
Chris@0 50 private $usages = array();
Chris@0 51 private $helperSet;
Chris@0 52
Chris@0 53 /**
Chris@14 54 * @return string|null The default command name or null when no default name is set
Chris@14 55 */
Chris@14 56 public static function getDefaultName()
Chris@14 57 {
Chris@14 58 $class = get_called_class();
Chris@14 59 $r = new \ReflectionProperty($class, 'defaultName');
Chris@14 60
Chris@14 61 return $class === $r->class ? static::$defaultName : null;
Chris@14 62 }
Chris@14 63
Chris@14 64 /**
Chris@0 65 * @param string|null $name The name of the command; passing null means it must be set in configure()
Chris@0 66 *
Chris@0 67 * @throws LogicException When the command name is empty
Chris@0 68 */
Chris@0 69 public function __construct($name = null)
Chris@0 70 {
Chris@0 71 $this->definition = new InputDefinition();
Chris@0 72
Chris@14 73 if (null !== $name || null !== $name = static::getDefaultName()) {
Chris@0 74 $this->setName($name);
Chris@0 75 }
Chris@0 76
Chris@0 77 $this->configure();
Chris@0 78 }
Chris@0 79
Chris@0 80 /**
Chris@0 81 * Ignores validation errors.
Chris@0 82 *
Chris@0 83 * This is mainly useful for the help command.
Chris@0 84 */
Chris@0 85 public function ignoreValidationErrors()
Chris@0 86 {
Chris@0 87 $this->ignoreValidationErrors = true;
Chris@0 88 }
Chris@0 89
Chris@0 90 public function setApplication(Application $application = null)
Chris@0 91 {
Chris@0 92 $this->application = $application;
Chris@0 93 if ($application) {
Chris@0 94 $this->setHelperSet($application->getHelperSet());
Chris@0 95 } else {
Chris@0 96 $this->helperSet = null;
Chris@0 97 }
Chris@0 98 }
Chris@0 99
Chris@0 100 public function setHelperSet(HelperSet $helperSet)
Chris@0 101 {
Chris@0 102 $this->helperSet = $helperSet;
Chris@0 103 }
Chris@0 104
Chris@0 105 /**
Chris@0 106 * Gets the helper set.
Chris@0 107 *
Chris@0 108 * @return HelperSet A HelperSet instance
Chris@0 109 */
Chris@0 110 public function getHelperSet()
Chris@0 111 {
Chris@0 112 return $this->helperSet;
Chris@0 113 }
Chris@0 114
Chris@0 115 /**
Chris@0 116 * Gets the application instance for this command.
Chris@0 117 *
Chris@0 118 * @return Application An Application instance
Chris@0 119 */
Chris@0 120 public function getApplication()
Chris@0 121 {
Chris@0 122 return $this->application;
Chris@0 123 }
Chris@0 124
Chris@0 125 /**
Chris@0 126 * Checks whether the command is enabled or not in the current environment.
Chris@0 127 *
Chris@0 128 * Override this to check for x or y and return false if the command can not
Chris@0 129 * run properly under the current conditions.
Chris@0 130 *
Chris@0 131 * @return bool
Chris@0 132 */
Chris@0 133 public function isEnabled()
Chris@0 134 {
Chris@0 135 return true;
Chris@0 136 }
Chris@0 137
Chris@0 138 /**
Chris@0 139 * Configures the current command.
Chris@0 140 */
Chris@0 141 protected function configure()
Chris@0 142 {
Chris@0 143 }
Chris@0 144
Chris@0 145 /**
Chris@0 146 * Executes the current command.
Chris@0 147 *
Chris@0 148 * This method is not abstract because you can use this class
Chris@0 149 * as a concrete class. In this case, instead of defining the
Chris@0 150 * execute() method, you set the code to execute by passing
Chris@0 151 * a Closure to the setCode() method.
Chris@0 152 *
Chris@0 153 * @return null|int null or 0 if everything went fine, or an error code
Chris@0 154 *
Chris@0 155 * @throws LogicException When this abstract method is not implemented
Chris@0 156 *
Chris@0 157 * @see setCode()
Chris@0 158 */
Chris@0 159 protected function execute(InputInterface $input, OutputInterface $output)
Chris@0 160 {
Chris@0 161 throw new LogicException('You must override the execute() method in the concrete command class.');
Chris@0 162 }
Chris@0 163
Chris@0 164 /**
Chris@0 165 * Interacts with the user.
Chris@0 166 *
Chris@0 167 * This method is executed before the InputDefinition is validated.
Chris@0 168 * This means that this is the only place where the command can
Chris@0 169 * interactively ask for values of missing required arguments.
Chris@0 170 */
Chris@0 171 protected function interact(InputInterface $input, OutputInterface $output)
Chris@0 172 {
Chris@0 173 }
Chris@0 174
Chris@0 175 /**
Chris@0 176 * Initializes the command just after the input has been validated.
Chris@0 177 *
Chris@0 178 * This is mainly useful when a lot of commands extends one main command
Chris@0 179 * where some things need to be initialized based on the input arguments and options.
Chris@0 180 */
Chris@0 181 protected function initialize(InputInterface $input, OutputInterface $output)
Chris@0 182 {
Chris@0 183 }
Chris@0 184
Chris@0 185 /**
Chris@0 186 * Runs the command.
Chris@0 187 *
Chris@0 188 * The code to execute is either defined directly with the
Chris@0 189 * setCode() method or by overriding the execute() method
Chris@0 190 * in a sub-class.
Chris@0 191 *
Chris@0 192 * @return int The command exit code
Chris@0 193 *
Chris@0 194 * @throws \Exception When binding input fails. Bypass this by calling {@link ignoreValidationErrors()}.
Chris@0 195 *
Chris@0 196 * @see setCode()
Chris@0 197 * @see execute()
Chris@0 198 */
Chris@0 199 public function run(InputInterface $input, OutputInterface $output)
Chris@0 200 {
Chris@0 201 // force the creation of the synopsis before the merge with the app definition
Chris@0 202 $this->getSynopsis(true);
Chris@0 203 $this->getSynopsis(false);
Chris@0 204
Chris@0 205 // add the application arguments and options
Chris@0 206 $this->mergeApplicationDefinition();
Chris@0 207
Chris@0 208 // bind the input against the command specific arguments/options
Chris@0 209 try {
Chris@0 210 $input->bind($this->definition);
Chris@0 211 } catch (ExceptionInterface $e) {
Chris@0 212 if (!$this->ignoreValidationErrors) {
Chris@0 213 throw $e;
Chris@0 214 }
Chris@0 215 }
Chris@0 216
Chris@0 217 $this->initialize($input, $output);
Chris@0 218
Chris@0 219 if (null !== $this->processTitle) {
Chris@0 220 if (function_exists('cli_set_process_title')) {
Chris@0 221 if (false === @cli_set_process_title($this->processTitle)) {
Chris@0 222 if ('Darwin' === PHP_OS) {
Chris@0 223 $output->writeln('<comment>Running "cli_get_process_title" as an unprivileged user is not supported on MacOS.</comment>');
Chris@0 224 } else {
Chris@0 225 $error = error_get_last();
Chris@0 226 trigger_error($error['message'], E_USER_WARNING);
Chris@0 227 }
Chris@0 228 }
Chris@0 229 } elseif (function_exists('setproctitle')) {
Chris@0 230 setproctitle($this->processTitle);
Chris@0 231 } elseif (OutputInterface::VERBOSITY_VERY_VERBOSE === $output->getVerbosity()) {
Chris@0 232 $output->writeln('<comment>Install the proctitle PECL to be able to change the process title.</comment>');
Chris@0 233 }
Chris@0 234 }
Chris@0 235
Chris@0 236 if ($input->isInteractive()) {
Chris@0 237 $this->interact($input, $output);
Chris@0 238 }
Chris@0 239
Chris@0 240 // The command name argument is often omitted when a command is executed directly with its run() method.
Chris@0 241 // It would fail the validation if we didn't make sure the command argument is present,
Chris@0 242 // since it's required by the application.
Chris@0 243 if ($input->hasArgument('command') && null === $input->getArgument('command')) {
Chris@0 244 $input->setArgument('command', $this->getName());
Chris@0 245 }
Chris@0 246
Chris@0 247 $input->validate();
Chris@0 248
Chris@0 249 if ($this->code) {
Chris@0 250 $statusCode = call_user_func($this->code, $input, $output);
Chris@0 251 } else {
Chris@0 252 $statusCode = $this->execute($input, $output);
Chris@0 253 }
Chris@0 254
Chris@0 255 return is_numeric($statusCode) ? (int) $statusCode : 0;
Chris@0 256 }
Chris@0 257
Chris@0 258 /**
Chris@0 259 * Sets the code to execute when running this command.
Chris@0 260 *
Chris@0 261 * If this method is used, it overrides the code defined
Chris@0 262 * in the execute() method.
Chris@0 263 *
Chris@0 264 * @param callable $code A callable(InputInterface $input, OutputInterface $output)
Chris@0 265 *
Chris@0 266 * @return $this
Chris@0 267 *
Chris@0 268 * @throws InvalidArgumentException
Chris@0 269 *
Chris@0 270 * @see execute()
Chris@0 271 */
Chris@0 272 public function setCode(callable $code)
Chris@0 273 {
Chris@0 274 if ($code instanceof \Closure) {
Chris@0 275 $r = new \ReflectionFunction($code);
Chris@0 276 if (null === $r->getClosureThis()) {
Chris@12 277 if (\PHP_VERSION_ID < 70000) {
Chris@0 278 // Bug in PHP5: https://bugs.php.net/bug.php?id=64761
Chris@0 279 // This means that we cannot bind static closures and therefore we must
Chris@0 280 // ignore any errors here. There is no way to test if the closure is
Chris@0 281 // bindable.
Chris@0 282 $code = @\Closure::bind($code, $this);
Chris@0 283 } else {
Chris@0 284 $code = \Closure::bind($code, $this);
Chris@0 285 }
Chris@0 286 }
Chris@0 287 }
Chris@0 288
Chris@0 289 $this->code = $code;
Chris@0 290
Chris@0 291 return $this;
Chris@0 292 }
Chris@0 293
Chris@0 294 /**
Chris@0 295 * Merges the application definition with the command definition.
Chris@0 296 *
Chris@0 297 * This method is not part of public API and should not be used directly.
Chris@0 298 *
Chris@0 299 * @param bool $mergeArgs Whether to merge or not the Application definition arguments to Command definition arguments
Chris@0 300 */
Chris@0 301 public function mergeApplicationDefinition($mergeArgs = true)
Chris@0 302 {
Chris@0 303 if (null === $this->application || (true === $this->applicationDefinitionMerged && ($this->applicationDefinitionMergedWithArgs || !$mergeArgs))) {
Chris@0 304 return;
Chris@0 305 }
Chris@0 306
Chris@0 307 $this->definition->addOptions($this->application->getDefinition()->getOptions());
Chris@0 308
Chris@0 309 if ($mergeArgs) {
Chris@0 310 $currentArguments = $this->definition->getArguments();
Chris@0 311 $this->definition->setArguments($this->application->getDefinition()->getArguments());
Chris@0 312 $this->definition->addArguments($currentArguments);
Chris@0 313 }
Chris@0 314
Chris@0 315 $this->applicationDefinitionMerged = true;
Chris@0 316 if ($mergeArgs) {
Chris@0 317 $this->applicationDefinitionMergedWithArgs = true;
Chris@0 318 }
Chris@0 319 }
Chris@0 320
Chris@0 321 /**
Chris@0 322 * Sets an array of argument and option instances.
Chris@0 323 *
Chris@0 324 * @param array|InputDefinition $definition An array of argument and option instances or a definition instance
Chris@0 325 *
Chris@0 326 * @return $this
Chris@0 327 */
Chris@0 328 public function setDefinition($definition)
Chris@0 329 {
Chris@0 330 if ($definition instanceof InputDefinition) {
Chris@0 331 $this->definition = $definition;
Chris@0 332 } else {
Chris@0 333 $this->definition->setDefinition($definition);
Chris@0 334 }
Chris@0 335
Chris@0 336 $this->applicationDefinitionMerged = false;
Chris@0 337
Chris@0 338 return $this;
Chris@0 339 }
Chris@0 340
Chris@0 341 /**
Chris@0 342 * Gets the InputDefinition attached to this Command.
Chris@0 343 *
Chris@0 344 * @return InputDefinition An InputDefinition instance
Chris@0 345 */
Chris@0 346 public function getDefinition()
Chris@0 347 {
Chris@0 348 return $this->definition;
Chris@0 349 }
Chris@0 350
Chris@0 351 /**
Chris@0 352 * Gets the InputDefinition to be used to create representations of this Command.
Chris@0 353 *
Chris@0 354 * Can be overridden to provide the original command representation when it would otherwise
Chris@0 355 * be changed by merging with the application InputDefinition.
Chris@0 356 *
Chris@0 357 * This method is not part of public API and should not be used directly.
Chris@0 358 *
Chris@0 359 * @return InputDefinition An InputDefinition instance
Chris@0 360 */
Chris@0 361 public function getNativeDefinition()
Chris@0 362 {
Chris@0 363 return $this->getDefinition();
Chris@0 364 }
Chris@0 365
Chris@0 366 /**
Chris@0 367 * Adds an argument.
Chris@0 368 *
Chris@0 369 * @param string $name The argument name
Chris@0 370 * @param int $mode The argument mode: InputArgument::REQUIRED or InputArgument::OPTIONAL
Chris@0 371 * @param string $description A description text
Chris@0 372 * @param mixed $default The default value (for InputArgument::OPTIONAL mode only)
Chris@0 373 *
Chris@0 374 * @return $this
Chris@0 375 */
Chris@0 376 public function addArgument($name, $mode = null, $description = '', $default = null)
Chris@0 377 {
Chris@0 378 $this->definition->addArgument(new InputArgument($name, $mode, $description, $default));
Chris@0 379
Chris@0 380 return $this;
Chris@0 381 }
Chris@0 382
Chris@0 383 /**
Chris@0 384 * Adds an option.
Chris@0 385 *
Chris@0 386 * @param string $name The option name
Chris@0 387 * @param string $shortcut The shortcut (can be null)
Chris@0 388 * @param int $mode The option mode: One of the InputOption::VALUE_* constants
Chris@0 389 * @param string $description A description text
Chris@0 390 * @param mixed $default The default value (must be null for InputOption::VALUE_NONE)
Chris@0 391 *
Chris@0 392 * @return $this
Chris@0 393 */
Chris@0 394 public function addOption($name, $shortcut = null, $mode = null, $description = '', $default = null)
Chris@0 395 {
Chris@0 396 $this->definition->addOption(new InputOption($name, $shortcut, $mode, $description, $default));
Chris@0 397
Chris@0 398 return $this;
Chris@0 399 }
Chris@0 400
Chris@0 401 /**
Chris@0 402 * Sets the name of the command.
Chris@0 403 *
Chris@0 404 * This method can set both the namespace and the name if
Chris@0 405 * you separate them by a colon (:)
Chris@0 406 *
Chris@0 407 * $command->setName('foo:bar');
Chris@0 408 *
Chris@0 409 * @param string $name The command name
Chris@0 410 *
Chris@0 411 * @return $this
Chris@0 412 *
Chris@0 413 * @throws InvalidArgumentException When the name is invalid
Chris@0 414 */
Chris@0 415 public function setName($name)
Chris@0 416 {
Chris@0 417 $this->validateName($name);
Chris@0 418
Chris@0 419 $this->name = $name;
Chris@0 420
Chris@0 421 return $this;
Chris@0 422 }
Chris@0 423
Chris@0 424 /**
Chris@0 425 * Sets the process title of the command.
Chris@0 426 *
Chris@0 427 * This feature should be used only when creating a long process command,
Chris@0 428 * like a daemon.
Chris@0 429 *
Chris@0 430 * PHP 5.5+ or the proctitle PECL library is required
Chris@0 431 *
Chris@0 432 * @param string $title The process title
Chris@0 433 *
Chris@0 434 * @return $this
Chris@0 435 */
Chris@0 436 public function setProcessTitle($title)
Chris@0 437 {
Chris@0 438 $this->processTitle = $title;
Chris@0 439
Chris@0 440 return $this;
Chris@0 441 }
Chris@0 442
Chris@0 443 /**
Chris@0 444 * Returns the command name.
Chris@0 445 *
Chris@0 446 * @return string The command name
Chris@0 447 */
Chris@0 448 public function getName()
Chris@0 449 {
Chris@0 450 return $this->name;
Chris@0 451 }
Chris@0 452
Chris@0 453 /**
Chris@0 454 * @param bool $hidden Whether or not the command should be hidden from the list of commands
Chris@0 455 *
Chris@0 456 * @return Command The current instance
Chris@0 457 */
Chris@0 458 public function setHidden($hidden)
Chris@0 459 {
Chris@0 460 $this->hidden = (bool) $hidden;
Chris@0 461
Chris@0 462 return $this;
Chris@0 463 }
Chris@0 464
Chris@0 465 /**
Chris@14 466 * @return bool whether the command should be publicly shown or not
Chris@0 467 */
Chris@0 468 public function isHidden()
Chris@0 469 {
Chris@0 470 return $this->hidden;
Chris@0 471 }
Chris@0 472
Chris@0 473 /**
Chris@0 474 * Sets the description for the command.
Chris@0 475 *
Chris@0 476 * @param string $description The description for the command
Chris@0 477 *
Chris@0 478 * @return $this
Chris@0 479 */
Chris@0 480 public function setDescription($description)
Chris@0 481 {
Chris@0 482 $this->description = $description;
Chris@0 483
Chris@0 484 return $this;
Chris@0 485 }
Chris@0 486
Chris@0 487 /**
Chris@0 488 * Returns the description for the command.
Chris@0 489 *
Chris@0 490 * @return string The description for the command
Chris@0 491 */
Chris@0 492 public function getDescription()
Chris@0 493 {
Chris@0 494 return $this->description;
Chris@0 495 }
Chris@0 496
Chris@0 497 /**
Chris@0 498 * Sets the help for the command.
Chris@0 499 *
Chris@0 500 * @param string $help The help for the command
Chris@0 501 *
Chris@0 502 * @return $this
Chris@0 503 */
Chris@0 504 public function setHelp($help)
Chris@0 505 {
Chris@0 506 $this->help = $help;
Chris@0 507
Chris@0 508 return $this;
Chris@0 509 }
Chris@0 510
Chris@0 511 /**
Chris@0 512 * Returns the help for the command.
Chris@0 513 *
Chris@0 514 * @return string The help for the command
Chris@0 515 */
Chris@0 516 public function getHelp()
Chris@0 517 {
Chris@0 518 return $this->help;
Chris@0 519 }
Chris@0 520
Chris@0 521 /**
Chris@0 522 * Returns the processed help for the command replacing the %command.name% and
Chris@0 523 * %command.full_name% patterns with the real values dynamically.
Chris@0 524 *
Chris@0 525 * @return string The processed help for the command
Chris@0 526 */
Chris@0 527 public function getProcessedHelp()
Chris@0 528 {
Chris@0 529 $name = $this->name;
Chris@0 530
Chris@0 531 $placeholders = array(
Chris@0 532 '%command.name%',
Chris@0 533 '%command.full_name%',
Chris@0 534 );
Chris@0 535 $replacements = array(
Chris@0 536 $name,
Chris@0 537 $_SERVER['PHP_SELF'].' '.$name,
Chris@0 538 );
Chris@0 539
Chris@0 540 return str_replace($placeholders, $replacements, $this->getHelp() ?: $this->getDescription());
Chris@0 541 }
Chris@0 542
Chris@0 543 /**
Chris@0 544 * Sets the aliases for the command.
Chris@0 545 *
Chris@0 546 * @param string[] $aliases An array of aliases for the command
Chris@0 547 *
Chris@0 548 * @return $this
Chris@0 549 *
Chris@0 550 * @throws InvalidArgumentException When an alias is invalid
Chris@0 551 */
Chris@0 552 public function setAliases($aliases)
Chris@0 553 {
Chris@0 554 if (!is_array($aliases) && !$aliases instanceof \Traversable) {
Chris@0 555 throw new InvalidArgumentException('$aliases must be an array or an instance of \Traversable');
Chris@0 556 }
Chris@0 557
Chris@0 558 foreach ($aliases as $alias) {
Chris@0 559 $this->validateName($alias);
Chris@0 560 }
Chris@0 561
Chris@0 562 $this->aliases = $aliases;
Chris@0 563
Chris@0 564 return $this;
Chris@0 565 }
Chris@0 566
Chris@0 567 /**
Chris@0 568 * Returns the aliases for the command.
Chris@0 569 *
Chris@0 570 * @return array An array of aliases for the command
Chris@0 571 */
Chris@0 572 public function getAliases()
Chris@0 573 {
Chris@0 574 return $this->aliases;
Chris@0 575 }
Chris@0 576
Chris@0 577 /**
Chris@0 578 * Returns the synopsis for the command.
Chris@0 579 *
Chris@0 580 * @param bool $short Whether to show the short version of the synopsis (with options folded) or not
Chris@0 581 *
Chris@0 582 * @return string The synopsis
Chris@0 583 */
Chris@0 584 public function getSynopsis($short = false)
Chris@0 585 {
Chris@0 586 $key = $short ? 'short' : 'long';
Chris@0 587
Chris@0 588 if (!isset($this->synopsis[$key])) {
Chris@0 589 $this->synopsis[$key] = trim(sprintf('%s %s', $this->name, $this->definition->getSynopsis($short)));
Chris@0 590 }
Chris@0 591
Chris@0 592 return $this->synopsis[$key];
Chris@0 593 }
Chris@0 594
Chris@0 595 /**
Chris@0 596 * Add a command usage example.
Chris@0 597 *
Chris@0 598 * @param string $usage The usage, it'll be prefixed with the command name
Chris@0 599 *
Chris@0 600 * @return $this
Chris@0 601 */
Chris@0 602 public function addUsage($usage)
Chris@0 603 {
Chris@0 604 if (0 !== strpos($usage, $this->name)) {
Chris@0 605 $usage = sprintf('%s %s', $this->name, $usage);
Chris@0 606 }
Chris@0 607
Chris@0 608 $this->usages[] = $usage;
Chris@0 609
Chris@0 610 return $this;
Chris@0 611 }
Chris@0 612
Chris@0 613 /**
Chris@0 614 * Returns alternative usages of the command.
Chris@0 615 *
Chris@0 616 * @return array
Chris@0 617 */
Chris@0 618 public function getUsages()
Chris@0 619 {
Chris@0 620 return $this->usages;
Chris@0 621 }
Chris@0 622
Chris@0 623 /**
Chris@0 624 * Gets a helper instance by name.
Chris@0 625 *
Chris@0 626 * @param string $name The helper name
Chris@0 627 *
Chris@0 628 * @return mixed The helper value
Chris@0 629 *
Chris@0 630 * @throws LogicException if no HelperSet is defined
Chris@0 631 * @throws InvalidArgumentException if the helper is not defined
Chris@0 632 */
Chris@0 633 public function getHelper($name)
Chris@0 634 {
Chris@0 635 if (null === $this->helperSet) {
Chris@0 636 throw new LogicException(sprintf('Cannot retrieve helper "%s" because there is no HelperSet defined. Did you forget to add your command to the application or to set the application on the command using the setApplication() method? You can also set the HelperSet directly using the setHelperSet() method.', $name));
Chris@0 637 }
Chris@0 638
Chris@0 639 return $this->helperSet->get($name);
Chris@0 640 }
Chris@0 641
Chris@0 642 /**
Chris@0 643 * Validates a command name.
Chris@0 644 *
Chris@0 645 * It must be non-empty and parts can optionally be separated by ":".
Chris@0 646 *
Chris@0 647 * @param string $name
Chris@0 648 *
Chris@0 649 * @throws InvalidArgumentException When the name is invalid
Chris@0 650 */
Chris@0 651 private function validateName($name)
Chris@0 652 {
Chris@0 653 if (!preg_match('/^[^\:]++(\:[^\:]++)*$/', $name)) {
Chris@0 654 throw new InvalidArgumentException(sprintf('Command name "%s" is invalid.', $name));
Chris@0 655 }
Chris@0 656 }
Chris@0 657 }