annotate vendor/symfony/console/Application.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children 1fec387a4317
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;
Chris@0 13
Chris@0 14 use Symfony\Component\Console\Exception\ExceptionInterface;
Chris@0 15 use Symfony\Component\Console\Formatter\OutputFormatter;
Chris@0 16 use Symfony\Component\Console\Helper\DebugFormatterHelper;
Chris@12 17 use Symfony\Component\Console\Helper\Helper;
Chris@0 18 use Symfony\Component\Console\Helper\ProcessHelper;
Chris@0 19 use Symfony\Component\Console\Helper\QuestionHelper;
Chris@0 20 use Symfony\Component\Console\Input\InputInterface;
Chris@0 21 use Symfony\Component\Console\Input\StreamableInputInterface;
Chris@0 22 use Symfony\Component\Console\Input\ArgvInput;
Chris@0 23 use Symfony\Component\Console\Input\ArrayInput;
Chris@0 24 use Symfony\Component\Console\Input\InputDefinition;
Chris@0 25 use Symfony\Component\Console\Input\InputOption;
Chris@0 26 use Symfony\Component\Console\Input\InputArgument;
Chris@0 27 use Symfony\Component\Console\Input\InputAwareInterface;
Chris@0 28 use Symfony\Component\Console\Output\OutputInterface;
Chris@0 29 use Symfony\Component\Console\Output\ConsoleOutput;
Chris@0 30 use Symfony\Component\Console\Output\ConsoleOutputInterface;
Chris@0 31 use Symfony\Component\Console\Command\Command;
Chris@0 32 use Symfony\Component\Console\Command\HelpCommand;
Chris@0 33 use Symfony\Component\Console\Command\ListCommand;
Chris@0 34 use Symfony\Component\Console\Helper\HelperSet;
Chris@0 35 use Symfony\Component\Console\Helper\FormatterHelper;
Chris@0 36 use Symfony\Component\Console\Event\ConsoleCommandEvent;
Chris@0 37 use Symfony\Component\Console\Event\ConsoleExceptionEvent;
Chris@0 38 use Symfony\Component\Console\Event\ConsoleTerminateEvent;
Chris@0 39 use Symfony\Component\Console\Exception\CommandNotFoundException;
Chris@0 40 use Symfony\Component\Console\Exception\LogicException;
Chris@0 41 use Symfony\Component\Debug\Exception\FatalThrowableError;
Chris@0 42 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
Chris@0 43
Chris@0 44 /**
Chris@0 45 * An Application is the container for a collection of commands.
Chris@0 46 *
Chris@0 47 * It is the main entry point of a Console application.
Chris@0 48 *
Chris@0 49 * This class is optimized for a standard CLI environment.
Chris@0 50 *
Chris@0 51 * Usage:
Chris@0 52 *
Chris@0 53 * $app = new Application('myapp', '1.0 (stable)');
Chris@0 54 * $app->add(new SimpleCommand());
Chris@0 55 * $app->run();
Chris@0 56 *
Chris@0 57 * @author Fabien Potencier <fabien@symfony.com>
Chris@0 58 */
Chris@0 59 class Application
Chris@0 60 {
Chris@0 61 private $commands = array();
Chris@0 62 private $wantHelps = false;
Chris@0 63 private $runningCommand;
Chris@0 64 private $name;
Chris@0 65 private $version;
Chris@0 66 private $catchExceptions = true;
Chris@0 67 private $autoExit = true;
Chris@0 68 private $definition;
Chris@0 69 private $helperSet;
Chris@0 70 private $dispatcher;
Chris@0 71 private $terminal;
Chris@0 72 private $defaultCommand;
Chris@0 73 private $singleCommand;
Chris@0 74
Chris@0 75 /**
Chris@0 76 * @param string $name The name of the application
Chris@0 77 * @param string $version The version of the application
Chris@0 78 */
Chris@0 79 public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
Chris@0 80 {
Chris@0 81 $this->name = $name;
Chris@0 82 $this->version = $version;
Chris@0 83 $this->terminal = new Terminal();
Chris@0 84 $this->defaultCommand = 'list';
Chris@0 85 $this->helperSet = $this->getDefaultHelperSet();
Chris@0 86 $this->definition = $this->getDefaultInputDefinition();
Chris@0 87
Chris@0 88 foreach ($this->getDefaultCommands() as $command) {
Chris@0 89 $this->add($command);
Chris@0 90 }
Chris@0 91 }
Chris@0 92
Chris@0 93 public function setDispatcher(EventDispatcherInterface $dispatcher)
Chris@0 94 {
Chris@0 95 $this->dispatcher = $dispatcher;
Chris@0 96 }
Chris@0 97
Chris@0 98 /**
Chris@0 99 * Runs the current application.
Chris@0 100 *
Chris@0 101 * @param InputInterface $input An Input instance
Chris@0 102 * @param OutputInterface $output An Output instance
Chris@0 103 *
Chris@0 104 * @return int 0 if everything went fine, or an error code
Chris@0 105 *
Chris@0 106 * @throws \Exception When running fails. Bypass this when {@link setCatchExceptions()}.
Chris@0 107 */
Chris@0 108 public function run(InputInterface $input = null, OutputInterface $output = null)
Chris@0 109 {
Chris@0 110 putenv('LINES='.$this->terminal->getHeight());
Chris@0 111 putenv('COLUMNS='.$this->terminal->getWidth());
Chris@0 112
Chris@0 113 if (null === $input) {
Chris@0 114 $input = new ArgvInput();
Chris@0 115 }
Chris@0 116
Chris@0 117 if (null === $output) {
Chris@0 118 $output = new ConsoleOutput();
Chris@0 119 }
Chris@0 120
Chris@0 121 $this->configureIO($input, $output);
Chris@0 122
Chris@0 123 try {
Chris@0 124 $e = null;
Chris@0 125 $exitCode = $this->doRun($input, $output);
Chris@0 126 } catch (\Exception $x) {
Chris@0 127 $e = $x;
Chris@0 128 } catch (\Throwable $x) {
Chris@0 129 $e = new FatalThrowableError($x);
Chris@0 130 }
Chris@0 131
Chris@0 132 if (null !== $e) {
Chris@12 133 if (!$this->catchExceptions || !$x instanceof \Exception) {
Chris@0 134 throw $x;
Chris@0 135 }
Chris@0 136
Chris@0 137 if ($output instanceof ConsoleOutputInterface) {
Chris@0 138 $this->renderException($e, $output->getErrorOutput());
Chris@0 139 } else {
Chris@0 140 $this->renderException($e, $output);
Chris@0 141 }
Chris@0 142
Chris@0 143 $exitCode = $e->getCode();
Chris@0 144 if (is_numeric($exitCode)) {
Chris@0 145 $exitCode = (int) $exitCode;
Chris@0 146 if (0 === $exitCode) {
Chris@0 147 $exitCode = 1;
Chris@0 148 }
Chris@0 149 } else {
Chris@0 150 $exitCode = 1;
Chris@0 151 }
Chris@0 152 }
Chris@0 153
Chris@0 154 if ($this->autoExit) {
Chris@0 155 if ($exitCode > 255) {
Chris@0 156 $exitCode = 255;
Chris@0 157 }
Chris@0 158
Chris@0 159 exit($exitCode);
Chris@0 160 }
Chris@0 161
Chris@0 162 return $exitCode;
Chris@0 163 }
Chris@0 164
Chris@0 165 /**
Chris@0 166 * Runs the current application.
Chris@0 167 *
Chris@0 168 * @param InputInterface $input An Input instance
Chris@0 169 * @param OutputInterface $output An Output instance
Chris@0 170 *
Chris@0 171 * @return int 0 if everything went fine, or an error code
Chris@0 172 */
Chris@0 173 public function doRun(InputInterface $input, OutputInterface $output)
Chris@0 174 {
Chris@0 175 if (true === $input->hasParameterOption(array('--version', '-V'), true)) {
Chris@0 176 $output->writeln($this->getLongVersion());
Chris@0 177
Chris@0 178 return 0;
Chris@0 179 }
Chris@0 180
Chris@0 181 $name = $this->getCommandName($input);
Chris@0 182 if (true === $input->hasParameterOption(array('--help', '-h'), true)) {
Chris@0 183 if (!$name) {
Chris@0 184 $name = 'help';
Chris@0 185 $input = new ArrayInput(array('command_name' => $this->defaultCommand));
Chris@0 186 } else {
Chris@0 187 $this->wantHelps = true;
Chris@0 188 }
Chris@0 189 }
Chris@0 190
Chris@0 191 if (!$name) {
Chris@0 192 $name = $this->defaultCommand;
Chris@12 193 $this->definition->setArguments(array_merge(
Chris@12 194 $this->definition->getArguments(),
Chris@12 195 array(
Chris@12 196 'command' => new InputArgument('command', InputArgument::OPTIONAL, $this->definition->getArgument('command')->getDescription(), $name),
Chris@12 197 )
Chris@12 198 ));
Chris@0 199 }
Chris@0 200
Chris@0 201 $this->runningCommand = null;
Chris@0 202 // the command name MUST be the first element of the input
Chris@0 203 $command = $this->find($name);
Chris@0 204
Chris@0 205 $this->runningCommand = $command;
Chris@0 206 $exitCode = $this->doRunCommand($command, $input, $output);
Chris@0 207 $this->runningCommand = null;
Chris@0 208
Chris@0 209 return $exitCode;
Chris@0 210 }
Chris@0 211
Chris@0 212 /**
Chris@0 213 * Set a helper set to be used with the command.
Chris@0 214 *
Chris@0 215 * @param HelperSet $helperSet The helper set
Chris@0 216 */
Chris@0 217 public function setHelperSet(HelperSet $helperSet)
Chris@0 218 {
Chris@0 219 $this->helperSet = $helperSet;
Chris@0 220 }
Chris@0 221
Chris@0 222 /**
Chris@0 223 * Get the helper set associated with the command.
Chris@0 224 *
Chris@0 225 * @return HelperSet The HelperSet instance associated with this command
Chris@0 226 */
Chris@0 227 public function getHelperSet()
Chris@0 228 {
Chris@0 229 return $this->helperSet;
Chris@0 230 }
Chris@0 231
Chris@0 232 /**
Chris@0 233 * Set an input definition to be used with this application.
Chris@0 234 *
Chris@0 235 * @param InputDefinition $definition The input definition
Chris@0 236 */
Chris@0 237 public function setDefinition(InputDefinition $definition)
Chris@0 238 {
Chris@0 239 $this->definition = $definition;
Chris@0 240 }
Chris@0 241
Chris@0 242 /**
Chris@0 243 * Gets the InputDefinition related to this Application.
Chris@0 244 *
Chris@0 245 * @return InputDefinition The InputDefinition instance
Chris@0 246 */
Chris@0 247 public function getDefinition()
Chris@0 248 {
Chris@0 249 if ($this->singleCommand) {
Chris@0 250 $inputDefinition = $this->definition;
Chris@0 251 $inputDefinition->setArguments();
Chris@0 252
Chris@0 253 return $inputDefinition;
Chris@0 254 }
Chris@0 255
Chris@0 256 return $this->definition;
Chris@0 257 }
Chris@0 258
Chris@0 259 /**
Chris@0 260 * Gets the help message.
Chris@0 261 *
Chris@0 262 * @return string A help message
Chris@0 263 */
Chris@0 264 public function getHelp()
Chris@0 265 {
Chris@0 266 return $this->getLongVersion();
Chris@0 267 }
Chris@0 268
Chris@0 269 /**
Chris@0 270 * Gets whether to catch exceptions or not during commands execution.
Chris@0 271 *
Chris@0 272 * @return bool Whether to catch exceptions or not during commands execution
Chris@0 273 */
Chris@0 274 public function areExceptionsCaught()
Chris@0 275 {
Chris@0 276 return $this->catchExceptions;
Chris@0 277 }
Chris@0 278
Chris@0 279 /**
Chris@0 280 * Sets whether to catch exceptions or not during commands execution.
Chris@0 281 *
Chris@0 282 * @param bool $boolean Whether to catch exceptions or not during commands execution
Chris@0 283 */
Chris@0 284 public function setCatchExceptions($boolean)
Chris@0 285 {
Chris@0 286 $this->catchExceptions = (bool) $boolean;
Chris@0 287 }
Chris@0 288
Chris@0 289 /**
Chris@0 290 * Gets whether to automatically exit after a command execution or not.
Chris@0 291 *
Chris@0 292 * @return bool Whether to automatically exit after a command execution or not
Chris@0 293 */
Chris@0 294 public function isAutoExitEnabled()
Chris@0 295 {
Chris@0 296 return $this->autoExit;
Chris@0 297 }
Chris@0 298
Chris@0 299 /**
Chris@0 300 * Sets whether to automatically exit after a command execution or not.
Chris@0 301 *
Chris@0 302 * @param bool $boolean Whether to automatically exit after a command execution or not
Chris@0 303 */
Chris@0 304 public function setAutoExit($boolean)
Chris@0 305 {
Chris@0 306 $this->autoExit = (bool) $boolean;
Chris@0 307 }
Chris@0 308
Chris@0 309 /**
Chris@0 310 * Gets the name of the application.
Chris@0 311 *
Chris@0 312 * @return string The application name
Chris@0 313 */
Chris@0 314 public function getName()
Chris@0 315 {
Chris@0 316 return $this->name;
Chris@0 317 }
Chris@0 318
Chris@0 319 /**
Chris@0 320 * Sets the application name.
Chris@0 321 *
Chris@0 322 * @param string $name The application name
Chris@0 323 */
Chris@0 324 public function setName($name)
Chris@0 325 {
Chris@0 326 $this->name = $name;
Chris@0 327 }
Chris@0 328
Chris@0 329 /**
Chris@0 330 * Gets the application version.
Chris@0 331 *
Chris@0 332 * @return string The application version
Chris@0 333 */
Chris@0 334 public function getVersion()
Chris@0 335 {
Chris@0 336 return $this->version;
Chris@0 337 }
Chris@0 338
Chris@0 339 /**
Chris@0 340 * Sets the application version.
Chris@0 341 *
Chris@0 342 * @param string $version The application version
Chris@0 343 */
Chris@0 344 public function setVersion($version)
Chris@0 345 {
Chris@0 346 $this->version = $version;
Chris@0 347 }
Chris@0 348
Chris@0 349 /**
Chris@0 350 * Returns the long version of the application.
Chris@0 351 *
Chris@0 352 * @return string The long application version
Chris@0 353 */
Chris@0 354 public function getLongVersion()
Chris@0 355 {
Chris@0 356 if ('UNKNOWN' !== $this->getName()) {
Chris@0 357 if ('UNKNOWN' !== $this->getVersion()) {
Chris@0 358 return sprintf('%s <info>%s</info>', $this->getName(), $this->getVersion());
Chris@0 359 }
Chris@0 360
Chris@0 361 return $this->getName();
Chris@0 362 }
Chris@0 363
Chris@0 364 return 'Console Tool';
Chris@0 365 }
Chris@0 366
Chris@0 367 /**
Chris@0 368 * Registers a new command.
Chris@0 369 *
Chris@0 370 * @param string $name The command name
Chris@0 371 *
Chris@0 372 * @return Command The newly created command
Chris@0 373 */
Chris@0 374 public function register($name)
Chris@0 375 {
Chris@0 376 return $this->add(new Command($name));
Chris@0 377 }
Chris@0 378
Chris@0 379 /**
Chris@0 380 * Adds an array of command objects.
Chris@0 381 *
Chris@0 382 * If a Command is not enabled it will not be added.
Chris@0 383 *
Chris@0 384 * @param Command[] $commands An array of commands
Chris@0 385 */
Chris@0 386 public function addCommands(array $commands)
Chris@0 387 {
Chris@0 388 foreach ($commands as $command) {
Chris@0 389 $this->add($command);
Chris@0 390 }
Chris@0 391 }
Chris@0 392
Chris@0 393 /**
Chris@0 394 * Adds a command object.
Chris@0 395 *
Chris@0 396 * If a command with the same name already exists, it will be overridden.
Chris@0 397 * If the command is not enabled it will not be added.
Chris@0 398 *
Chris@0 399 * @param Command $command A Command object
Chris@0 400 *
Chris@0 401 * @return Command|null The registered command if enabled or null
Chris@0 402 */
Chris@0 403 public function add(Command $command)
Chris@0 404 {
Chris@0 405 $command->setApplication($this);
Chris@0 406
Chris@0 407 if (!$command->isEnabled()) {
Chris@0 408 $command->setApplication(null);
Chris@0 409
Chris@0 410 return;
Chris@0 411 }
Chris@0 412
Chris@0 413 if (null === $command->getDefinition()) {
Chris@0 414 throw new LogicException(sprintf('Command class "%s" is not correctly initialized. You probably forgot to call the parent constructor.', get_class($command)));
Chris@0 415 }
Chris@0 416
Chris@0 417 $this->commands[$command->getName()] = $command;
Chris@0 418
Chris@0 419 foreach ($command->getAliases() as $alias) {
Chris@0 420 $this->commands[$alias] = $command;
Chris@0 421 }
Chris@0 422
Chris@0 423 return $command;
Chris@0 424 }
Chris@0 425
Chris@0 426 /**
Chris@0 427 * Returns a registered command by name or alias.
Chris@0 428 *
Chris@0 429 * @param string $name The command name or alias
Chris@0 430 *
Chris@0 431 * @return Command A Command object
Chris@0 432 *
Chris@0 433 * @throws CommandNotFoundException When given command name does not exist
Chris@0 434 */
Chris@0 435 public function get($name)
Chris@0 436 {
Chris@0 437 if (!isset($this->commands[$name])) {
Chris@0 438 throw new CommandNotFoundException(sprintf('The command "%s" does not exist.', $name));
Chris@0 439 }
Chris@0 440
Chris@0 441 $command = $this->commands[$name];
Chris@0 442
Chris@0 443 if ($this->wantHelps) {
Chris@0 444 $this->wantHelps = false;
Chris@0 445
Chris@0 446 $helpCommand = $this->get('help');
Chris@0 447 $helpCommand->setCommand($command);
Chris@0 448
Chris@0 449 return $helpCommand;
Chris@0 450 }
Chris@0 451
Chris@0 452 return $command;
Chris@0 453 }
Chris@0 454
Chris@0 455 /**
Chris@0 456 * Returns true if the command exists, false otherwise.
Chris@0 457 *
Chris@0 458 * @param string $name The command name or alias
Chris@0 459 *
Chris@0 460 * @return bool true if the command exists, false otherwise
Chris@0 461 */
Chris@0 462 public function has($name)
Chris@0 463 {
Chris@0 464 return isset($this->commands[$name]);
Chris@0 465 }
Chris@0 466
Chris@0 467 /**
Chris@0 468 * Returns an array of all unique namespaces used by currently registered commands.
Chris@0 469 *
Chris@0 470 * It does not return the global namespace which always exists.
Chris@0 471 *
Chris@0 472 * @return string[] An array of namespaces
Chris@0 473 */
Chris@0 474 public function getNamespaces()
Chris@0 475 {
Chris@0 476 $namespaces = array();
Chris@0 477 foreach ($this->all() as $command) {
Chris@0 478 $namespaces = array_merge($namespaces, $this->extractAllNamespaces($command->getName()));
Chris@0 479
Chris@0 480 foreach ($command->getAliases() as $alias) {
Chris@0 481 $namespaces = array_merge($namespaces, $this->extractAllNamespaces($alias));
Chris@0 482 }
Chris@0 483 }
Chris@0 484
Chris@0 485 return array_values(array_unique(array_filter($namespaces)));
Chris@0 486 }
Chris@0 487
Chris@0 488 /**
Chris@0 489 * Finds a registered namespace by a name or an abbreviation.
Chris@0 490 *
Chris@0 491 * @param string $namespace A namespace or abbreviation to search for
Chris@0 492 *
Chris@0 493 * @return string A registered namespace
Chris@0 494 *
Chris@0 495 * @throws CommandNotFoundException When namespace is incorrect or ambiguous
Chris@0 496 */
Chris@0 497 public function findNamespace($namespace)
Chris@0 498 {
Chris@0 499 $allNamespaces = $this->getNamespaces();
Chris@0 500 $expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]).'[^:]*'; }, $namespace);
Chris@0 501 $namespaces = preg_grep('{^'.$expr.'}', $allNamespaces);
Chris@0 502
Chris@0 503 if (empty($namespaces)) {
Chris@0 504 $message = sprintf('There are no commands defined in the "%s" namespace.', $namespace);
Chris@0 505
Chris@0 506 if ($alternatives = $this->findAlternatives($namespace, $allNamespaces)) {
Chris@0 507 if (1 == count($alternatives)) {
Chris@0 508 $message .= "\n\nDid you mean this?\n ";
Chris@0 509 } else {
Chris@0 510 $message .= "\n\nDid you mean one of these?\n ";
Chris@0 511 }
Chris@0 512
Chris@0 513 $message .= implode("\n ", $alternatives);
Chris@0 514 }
Chris@0 515
Chris@0 516 throw new CommandNotFoundException($message, $alternatives);
Chris@0 517 }
Chris@0 518
Chris@0 519 $exact = in_array($namespace, $namespaces, true);
Chris@0 520 if (count($namespaces) > 1 && !$exact) {
Chris@0 521 throw new CommandNotFoundException(sprintf('The namespace "%s" is ambiguous (%s).', $namespace, $this->getAbbreviationSuggestions(array_values($namespaces))), array_values($namespaces));
Chris@0 522 }
Chris@0 523
Chris@0 524 return $exact ? $namespace : reset($namespaces);
Chris@0 525 }
Chris@0 526
Chris@0 527 /**
Chris@0 528 * Finds a command by name or alias.
Chris@0 529 *
Chris@0 530 * Contrary to get, this command tries to find the best
Chris@0 531 * match if you give it an abbreviation of a name or alias.
Chris@0 532 *
Chris@0 533 * @param string $name A command name or a command alias
Chris@0 534 *
Chris@0 535 * @return Command A Command instance
Chris@0 536 *
Chris@0 537 * @throws CommandNotFoundException When command name is incorrect or ambiguous
Chris@0 538 */
Chris@0 539 public function find($name)
Chris@0 540 {
Chris@0 541 $allCommands = array_keys($this->commands);
Chris@0 542 $expr = preg_replace_callback('{([^:]+|)}', function ($matches) { return preg_quote($matches[1]).'[^:]*'; }, $name);
Chris@0 543 $commands = preg_grep('{^'.$expr.'}', $allCommands);
Chris@0 544
Chris@0 545 if (empty($commands) || count(preg_grep('{^'.$expr.'$}', $commands)) < 1) {
Chris@0 546 if (false !== $pos = strrpos($name, ':')) {
Chris@0 547 // check if a namespace exists and contains commands
Chris@0 548 $this->findNamespace(substr($name, 0, $pos));
Chris@0 549 }
Chris@0 550
Chris@0 551 $message = sprintf('Command "%s" is not defined.', $name);
Chris@0 552
Chris@0 553 if ($alternatives = $this->findAlternatives($name, $allCommands)) {
Chris@0 554 if (1 == count($alternatives)) {
Chris@0 555 $message .= "\n\nDid you mean this?\n ";
Chris@0 556 } else {
Chris@0 557 $message .= "\n\nDid you mean one of these?\n ";
Chris@0 558 }
Chris@0 559 $message .= implode("\n ", $alternatives);
Chris@0 560 }
Chris@0 561
Chris@0 562 throw new CommandNotFoundException($message, $alternatives);
Chris@0 563 }
Chris@0 564
Chris@0 565 // filter out aliases for commands which are already on the list
Chris@0 566 if (count($commands) > 1) {
Chris@0 567 $commandList = $this->commands;
Chris@0 568 $commands = array_filter($commands, function ($nameOrAlias) use ($commandList, $commands) {
Chris@0 569 $commandName = $commandList[$nameOrAlias]->getName();
Chris@0 570
Chris@0 571 return $commandName === $nameOrAlias || !in_array($commandName, $commands);
Chris@0 572 });
Chris@0 573 }
Chris@0 574
Chris@0 575 $exact = in_array($name, $commands, true);
Chris@0 576 if (count($commands) > 1 && !$exact) {
Chris@0 577 $suggestions = $this->getAbbreviationSuggestions(array_values($commands));
Chris@0 578
Chris@0 579 throw new CommandNotFoundException(sprintf('Command "%s" is ambiguous (%s).', $name, $suggestions), array_values($commands));
Chris@0 580 }
Chris@0 581
Chris@0 582 return $this->get($exact ? $name : reset($commands));
Chris@0 583 }
Chris@0 584
Chris@0 585 /**
Chris@0 586 * Gets the commands (registered in the given namespace if provided).
Chris@0 587 *
Chris@0 588 * The array keys are the full names and the values the command instances.
Chris@0 589 *
Chris@0 590 * @param string $namespace A namespace name
Chris@0 591 *
Chris@0 592 * @return Command[] An array of Command instances
Chris@0 593 */
Chris@0 594 public function all($namespace = null)
Chris@0 595 {
Chris@0 596 if (null === $namespace) {
Chris@0 597 return $this->commands;
Chris@0 598 }
Chris@0 599
Chris@0 600 $commands = array();
Chris@0 601 foreach ($this->commands as $name => $command) {
Chris@0 602 if ($namespace === $this->extractNamespace($name, substr_count($namespace, ':') + 1)) {
Chris@0 603 $commands[$name] = $command;
Chris@0 604 }
Chris@0 605 }
Chris@0 606
Chris@0 607 return $commands;
Chris@0 608 }
Chris@0 609
Chris@0 610 /**
Chris@0 611 * Returns an array of possible abbreviations given a set of names.
Chris@0 612 *
Chris@0 613 * @param array $names An array of names
Chris@0 614 *
Chris@0 615 * @return array An array of abbreviations
Chris@0 616 */
Chris@0 617 public static function getAbbreviations($names)
Chris@0 618 {
Chris@0 619 $abbrevs = array();
Chris@0 620 foreach ($names as $name) {
Chris@0 621 for ($len = strlen($name); $len > 0; --$len) {
Chris@0 622 $abbrev = substr($name, 0, $len);
Chris@0 623 $abbrevs[$abbrev][] = $name;
Chris@0 624 }
Chris@0 625 }
Chris@0 626
Chris@0 627 return $abbrevs;
Chris@0 628 }
Chris@0 629
Chris@0 630 /**
Chris@0 631 * Renders a caught exception.
Chris@0 632 *
Chris@0 633 * @param \Exception $e An exception instance
Chris@0 634 * @param OutputInterface $output An OutputInterface instance
Chris@0 635 */
Chris@0 636 public function renderException(\Exception $e, OutputInterface $output)
Chris@0 637 {
Chris@0 638 $output->writeln('', OutputInterface::VERBOSITY_QUIET);
Chris@0 639
Chris@0 640 do {
Chris@0 641 $title = sprintf(
Chris@0 642 ' [%s%s] ',
Chris@0 643 get_class($e),
Chris@0 644 $output->isVerbose() && 0 !== ($code = $e->getCode()) ? ' ('.$code.')' : ''
Chris@0 645 );
Chris@0 646
Chris@12 647 $len = Helper::strlen($title);
Chris@0 648
Chris@0 649 $width = $this->terminal->getWidth() ? $this->terminal->getWidth() - 1 : PHP_INT_MAX;
Chris@0 650 // HHVM only accepts 32 bits integer in str_split, even when PHP_INT_MAX is a 64 bit integer: https://github.com/facebook/hhvm/issues/1327
Chris@0 651 if (defined('HHVM_VERSION') && $width > 1 << 31) {
Chris@0 652 $width = 1 << 31;
Chris@0 653 }
Chris@0 654 $lines = array();
Chris@0 655 foreach (preg_split('/\r?\n/', $e->getMessage()) as $line) {
Chris@0 656 foreach ($this->splitStringByWidth($line, $width - 4) as $line) {
Chris@0 657 // pre-format lines to get the right string length
Chris@12 658 $lineLength = Helper::strlen($line) + 4;
Chris@0 659 $lines[] = array($line, $lineLength);
Chris@0 660
Chris@0 661 $len = max($lineLength, $len);
Chris@0 662 }
Chris@0 663 }
Chris@0 664
Chris@0 665 $messages = array();
Chris@0 666 $messages[] = $emptyLine = sprintf('<error>%s</error>', str_repeat(' ', $len));
Chris@12 667 $messages[] = sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - Helper::strlen($title))));
Chris@0 668 foreach ($lines as $line) {
Chris@0 669 $messages[] = sprintf('<error> %s %s</error>', OutputFormatter::escape($line[0]), str_repeat(' ', $len - $line[1]));
Chris@0 670 }
Chris@0 671 $messages[] = $emptyLine;
Chris@0 672 $messages[] = '';
Chris@0 673
Chris@0 674 $output->writeln($messages, OutputInterface::VERBOSITY_QUIET);
Chris@0 675
Chris@0 676 if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
Chris@0 677 $output->writeln('<comment>Exception trace:</comment>', OutputInterface::VERBOSITY_QUIET);
Chris@0 678
Chris@0 679 // exception related properties
Chris@0 680 $trace = $e->getTrace();
Chris@0 681 array_unshift($trace, array(
Chris@0 682 'function' => '',
Chris@0 683 'file' => $e->getFile() !== null ? $e->getFile() : 'n/a',
Chris@0 684 'line' => $e->getLine() !== null ? $e->getLine() : 'n/a',
Chris@0 685 'args' => array(),
Chris@0 686 ));
Chris@0 687
Chris@0 688 for ($i = 0, $count = count($trace); $i < $count; ++$i) {
Chris@0 689 $class = isset($trace[$i]['class']) ? $trace[$i]['class'] : '';
Chris@0 690 $type = isset($trace[$i]['type']) ? $trace[$i]['type'] : '';
Chris@0 691 $function = $trace[$i]['function'];
Chris@0 692 $file = isset($trace[$i]['file']) ? $trace[$i]['file'] : 'n/a';
Chris@0 693 $line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a';
Chris@0 694
Chris@0 695 $output->writeln(sprintf(' %s%s%s() at <info>%s:%s</info>', $class, $type, $function, $file, $line), OutputInterface::VERBOSITY_QUIET);
Chris@0 696 }
Chris@0 697
Chris@0 698 $output->writeln('', OutputInterface::VERBOSITY_QUIET);
Chris@0 699 }
Chris@0 700 } while ($e = $e->getPrevious());
Chris@0 701
Chris@0 702 if (null !== $this->runningCommand) {
Chris@0 703 $output->writeln(sprintf('<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName())), OutputInterface::VERBOSITY_QUIET);
Chris@0 704 $output->writeln('', OutputInterface::VERBOSITY_QUIET);
Chris@0 705 }
Chris@0 706 }
Chris@0 707
Chris@0 708 /**
Chris@0 709 * Tries to figure out the terminal width in which this application runs.
Chris@0 710 *
Chris@0 711 * @return int|null
Chris@0 712 *
Chris@0 713 * @deprecated since version 3.2, to be removed in 4.0. Create a Terminal instance instead.
Chris@0 714 */
Chris@0 715 protected function getTerminalWidth()
Chris@0 716 {
Chris@0 717 @trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Create a Terminal instance instead.', __METHOD__), E_USER_DEPRECATED);
Chris@0 718
Chris@0 719 return $this->terminal->getWidth();
Chris@0 720 }
Chris@0 721
Chris@0 722 /**
Chris@0 723 * Tries to figure out the terminal height in which this application runs.
Chris@0 724 *
Chris@0 725 * @return int|null
Chris@0 726 *
Chris@0 727 * @deprecated since version 3.2, to be removed in 4.0. Create a Terminal instance instead.
Chris@0 728 */
Chris@0 729 protected function getTerminalHeight()
Chris@0 730 {
Chris@0 731 @trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Create a Terminal instance instead.', __METHOD__), E_USER_DEPRECATED);
Chris@0 732
Chris@0 733 return $this->terminal->getHeight();
Chris@0 734 }
Chris@0 735
Chris@0 736 /**
Chris@0 737 * Tries to figure out the terminal dimensions based on the current environment.
Chris@0 738 *
Chris@0 739 * @return array Array containing width and height
Chris@0 740 *
Chris@0 741 * @deprecated since version 3.2, to be removed in 4.0. Create a Terminal instance instead.
Chris@0 742 */
Chris@0 743 public function getTerminalDimensions()
Chris@0 744 {
Chris@0 745 @trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Create a Terminal instance instead.', __METHOD__), E_USER_DEPRECATED);
Chris@0 746
Chris@0 747 return array($this->terminal->getWidth(), $this->terminal->getHeight());
Chris@0 748 }
Chris@0 749
Chris@0 750 /**
Chris@0 751 * Sets terminal dimensions.
Chris@0 752 *
Chris@0 753 * Can be useful to force terminal dimensions for functional tests.
Chris@0 754 *
Chris@0 755 * @param int $width The width
Chris@0 756 * @param int $height The height
Chris@0 757 *
Chris@0 758 * @return $this
Chris@0 759 *
Chris@0 760 * @deprecated since version 3.2, to be removed in 4.0. Set the COLUMNS and LINES env vars instead.
Chris@0 761 */
Chris@0 762 public function setTerminalDimensions($width, $height)
Chris@0 763 {
Chris@0 764 @trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Set the COLUMNS and LINES env vars instead.', __METHOD__), E_USER_DEPRECATED);
Chris@0 765
Chris@0 766 putenv('COLUMNS='.$width);
Chris@0 767 putenv('LINES='.$height);
Chris@0 768
Chris@0 769 return $this;
Chris@0 770 }
Chris@0 771
Chris@0 772 /**
Chris@0 773 * Configures the input and output instances based on the user arguments and options.
Chris@0 774 *
Chris@0 775 * @param InputInterface $input An InputInterface instance
Chris@0 776 * @param OutputInterface $output An OutputInterface instance
Chris@0 777 */
Chris@0 778 protected function configureIO(InputInterface $input, OutputInterface $output)
Chris@0 779 {
Chris@0 780 if (true === $input->hasParameterOption(array('--ansi'), true)) {
Chris@0 781 $output->setDecorated(true);
Chris@0 782 } elseif (true === $input->hasParameterOption(array('--no-ansi'), true)) {
Chris@0 783 $output->setDecorated(false);
Chris@0 784 }
Chris@0 785
Chris@0 786 if (true === $input->hasParameterOption(array('--no-interaction', '-n'), true)) {
Chris@0 787 $input->setInteractive(false);
Chris@0 788 } elseif (function_exists('posix_isatty')) {
Chris@0 789 $inputStream = null;
Chris@0 790
Chris@0 791 if ($input instanceof StreamableInputInterface) {
Chris@0 792 $inputStream = $input->getStream();
Chris@0 793 }
Chris@0 794
Chris@0 795 // This check ensures that calling QuestionHelper::setInputStream() works
Chris@0 796 // To be removed in 4.0 (in the same time as QuestionHelper::setInputStream)
Chris@0 797 if (!$inputStream && $this->getHelperSet()->has('question')) {
Chris@0 798 $inputStream = $this->getHelperSet()->get('question')->getInputStream(false);
Chris@0 799 }
Chris@0 800
Chris@0 801 if (!@posix_isatty($inputStream) && false === getenv('SHELL_INTERACTIVE')) {
Chris@0 802 $input->setInteractive(false);
Chris@0 803 }
Chris@0 804 }
Chris@0 805
Chris@0 806 if (true === $input->hasParameterOption(array('--quiet', '-q'), true)) {
Chris@0 807 $output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
Chris@0 808 $input->setInteractive(false);
Chris@0 809 } else {
Chris@0 810 if ($input->hasParameterOption('-vvv', true) || $input->hasParameterOption('--verbose=3', true) || $input->getParameterOption('--verbose', false, true) === 3) {
Chris@0 811 $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG);
Chris@0 812 } elseif ($input->hasParameterOption('-vv', true) || $input->hasParameterOption('--verbose=2', true) || $input->getParameterOption('--verbose', false, true) === 2) {
Chris@0 813 $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE);
Chris@0 814 } elseif ($input->hasParameterOption('-v', true) || $input->hasParameterOption('--verbose=1', true) || $input->hasParameterOption('--verbose', true) || $input->getParameterOption('--verbose', false, true)) {
Chris@0 815 $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
Chris@0 816 }
Chris@0 817 }
Chris@0 818 }
Chris@0 819
Chris@0 820 /**
Chris@0 821 * Runs the current command.
Chris@0 822 *
Chris@0 823 * If an event dispatcher has been attached to the application,
Chris@0 824 * events are also dispatched during the life-cycle of the command.
Chris@0 825 *
Chris@0 826 * @param Command $command A Command instance
Chris@0 827 * @param InputInterface $input An Input instance
Chris@0 828 * @param OutputInterface $output An Output instance
Chris@0 829 *
Chris@0 830 * @return int 0 if everything went fine, or an error code
Chris@0 831 */
Chris@0 832 protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output)
Chris@0 833 {
Chris@0 834 foreach ($command->getHelperSet() as $helper) {
Chris@0 835 if ($helper instanceof InputAwareInterface) {
Chris@0 836 $helper->setInput($input);
Chris@0 837 }
Chris@0 838 }
Chris@0 839
Chris@0 840 if (null === $this->dispatcher) {
Chris@0 841 return $command->run($input, $output);
Chris@0 842 }
Chris@0 843
Chris@0 844 // bind before the console.command event, so the listeners have access to input options/arguments
Chris@0 845 try {
Chris@0 846 $command->mergeApplicationDefinition();
Chris@0 847 $input->bind($command->getDefinition());
Chris@0 848 } catch (ExceptionInterface $e) {
Chris@0 849 // ignore invalid options/arguments for now, to allow the event listeners to customize the InputDefinition
Chris@0 850 }
Chris@0 851
Chris@0 852 $event = new ConsoleCommandEvent($command, $input, $output);
Chris@0 853 $e = null;
Chris@0 854
Chris@0 855 try {
Chris@0 856 $this->dispatcher->dispatch(ConsoleEvents::COMMAND, $event);
Chris@0 857
Chris@0 858 if ($event->commandShouldRun()) {
Chris@0 859 $exitCode = $command->run($input, $output);
Chris@0 860 } else {
Chris@0 861 $exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED;
Chris@0 862 }
Chris@0 863 } catch (\Exception $e) {
Chris@0 864 } catch (\Throwable $e) {
Chris@0 865 }
Chris@0 866 if (null !== $e) {
Chris@0 867 $x = $e instanceof \Exception ? $e : new FatalThrowableError($e);
Chris@0 868 $event = new ConsoleExceptionEvent($command, $input, $output, $x, $x->getCode());
Chris@0 869 $this->dispatcher->dispatch(ConsoleEvents::EXCEPTION, $event);
Chris@0 870
Chris@0 871 if ($x !== $event->getException()) {
Chris@0 872 $e = $event->getException();
Chris@0 873 }
Chris@0 874 $exitCode = $e->getCode();
Chris@0 875 }
Chris@0 876
Chris@0 877 $event = new ConsoleTerminateEvent($command, $input, $output, $exitCode);
Chris@0 878 $this->dispatcher->dispatch(ConsoleEvents::TERMINATE, $event);
Chris@0 879
Chris@0 880 if (null !== $e) {
Chris@0 881 throw $e;
Chris@0 882 }
Chris@0 883
Chris@0 884 return $event->getExitCode();
Chris@0 885 }
Chris@0 886
Chris@0 887 /**
Chris@0 888 * Gets the name of the command based on input.
Chris@0 889 *
Chris@0 890 * @param InputInterface $input The input interface
Chris@0 891 *
Chris@0 892 * @return string The command name
Chris@0 893 */
Chris@0 894 protected function getCommandName(InputInterface $input)
Chris@0 895 {
Chris@0 896 return $this->singleCommand ? $this->defaultCommand : $input->getFirstArgument();
Chris@0 897 }
Chris@0 898
Chris@0 899 /**
Chris@0 900 * Gets the default input definition.
Chris@0 901 *
Chris@0 902 * @return InputDefinition An InputDefinition instance
Chris@0 903 */
Chris@0 904 protected function getDefaultInputDefinition()
Chris@0 905 {
Chris@0 906 return new InputDefinition(array(
Chris@0 907 new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'),
Chris@0 908
Chris@0 909 new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display this help message'),
Chris@0 910 new InputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Do not output any message'),
Chris@0 911 new InputOption('--verbose', '-v|vv|vvv', InputOption::VALUE_NONE, 'Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug'),
Chris@0 912 new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display this application version'),
Chris@0 913 new InputOption('--ansi', '', InputOption::VALUE_NONE, 'Force ANSI output'),
Chris@0 914 new InputOption('--no-ansi', '', InputOption::VALUE_NONE, 'Disable ANSI output'),
Chris@0 915 new InputOption('--no-interaction', '-n', InputOption::VALUE_NONE, 'Do not ask any interactive question'),
Chris@0 916 ));
Chris@0 917 }
Chris@0 918
Chris@0 919 /**
Chris@0 920 * Gets the default commands that should always be available.
Chris@0 921 *
Chris@0 922 * @return Command[] An array of default Command instances
Chris@0 923 */
Chris@0 924 protected function getDefaultCommands()
Chris@0 925 {
Chris@0 926 return array(new HelpCommand(), new ListCommand());
Chris@0 927 }
Chris@0 928
Chris@0 929 /**
Chris@0 930 * Gets the default helper set with the helpers that should always be available.
Chris@0 931 *
Chris@0 932 * @return HelperSet A HelperSet instance
Chris@0 933 */
Chris@0 934 protected function getDefaultHelperSet()
Chris@0 935 {
Chris@0 936 return new HelperSet(array(
Chris@0 937 new FormatterHelper(),
Chris@0 938 new DebugFormatterHelper(),
Chris@0 939 new ProcessHelper(),
Chris@0 940 new QuestionHelper(),
Chris@0 941 ));
Chris@0 942 }
Chris@0 943
Chris@0 944 /**
Chris@0 945 * Returns abbreviated suggestions in string format.
Chris@0 946 *
Chris@0 947 * @param array $abbrevs Abbreviated suggestions to convert
Chris@0 948 *
Chris@0 949 * @return string A formatted string of abbreviated suggestions
Chris@0 950 */
Chris@0 951 private function getAbbreviationSuggestions($abbrevs)
Chris@0 952 {
Chris@0 953 return sprintf('%s, %s%s', $abbrevs[0], $abbrevs[1], count($abbrevs) > 2 ? sprintf(' and %d more', count($abbrevs) - 2) : '');
Chris@0 954 }
Chris@0 955
Chris@0 956 /**
Chris@0 957 * Returns the namespace part of the command name.
Chris@0 958 *
Chris@0 959 * This method is not part of public API and should not be used directly.
Chris@0 960 *
Chris@0 961 * @param string $name The full name of the command
Chris@0 962 * @param string $limit The maximum number of parts of the namespace
Chris@0 963 *
Chris@0 964 * @return string The namespace of the command
Chris@0 965 */
Chris@0 966 public function extractNamespace($name, $limit = null)
Chris@0 967 {
Chris@0 968 $parts = explode(':', $name);
Chris@0 969 array_pop($parts);
Chris@0 970
Chris@0 971 return implode(':', null === $limit ? $parts : array_slice($parts, 0, $limit));
Chris@0 972 }
Chris@0 973
Chris@0 974 /**
Chris@0 975 * Finds alternative of $name among $collection,
Chris@0 976 * if nothing is found in $collection, try in $abbrevs.
Chris@0 977 *
Chris@0 978 * @param string $name The string
Chris@0 979 * @param array|\Traversable $collection The collection
Chris@0 980 *
Chris@0 981 * @return string[] A sorted array of similar string
Chris@0 982 */
Chris@0 983 private function findAlternatives($name, $collection)
Chris@0 984 {
Chris@0 985 $threshold = 1e3;
Chris@0 986 $alternatives = array();
Chris@0 987
Chris@0 988 $collectionParts = array();
Chris@0 989 foreach ($collection as $item) {
Chris@0 990 $collectionParts[$item] = explode(':', $item);
Chris@0 991 }
Chris@0 992
Chris@0 993 foreach (explode(':', $name) as $i => $subname) {
Chris@0 994 foreach ($collectionParts as $collectionName => $parts) {
Chris@0 995 $exists = isset($alternatives[$collectionName]);
Chris@0 996 if (!isset($parts[$i]) && $exists) {
Chris@0 997 $alternatives[$collectionName] += $threshold;
Chris@0 998 continue;
Chris@0 999 } elseif (!isset($parts[$i])) {
Chris@0 1000 continue;
Chris@0 1001 }
Chris@0 1002
Chris@0 1003 $lev = levenshtein($subname, $parts[$i]);
Chris@0 1004 if ($lev <= strlen($subname) / 3 || '' !== $subname && false !== strpos($parts[$i], $subname)) {
Chris@0 1005 $alternatives[$collectionName] = $exists ? $alternatives[$collectionName] + $lev : $lev;
Chris@0 1006 } elseif ($exists) {
Chris@0 1007 $alternatives[$collectionName] += $threshold;
Chris@0 1008 }
Chris@0 1009 }
Chris@0 1010 }
Chris@0 1011
Chris@0 1012 foreach ($collection as $item) {
Chris@0 1013 $lev = levenshtein($name, $item);
Chris@0 1014 if ($lev <= strlen($name) / 3 || false !== strpos($item, $name)) {
Chris@0 1015 $alternatives[$item] = isset($alternatives[$item]) ? $alternatives[$item] - $lev : $lev;
Chris@0 1016 }
Chris@0 1017 }
Chris@0 1018
Chris@0 1019 $alternatives = array_filter($alternatives, function ($lev) use ($threshold) { return $lev < 2 * $threshold; });
Chris@0 1020 asort($alternatives);
Chris@0 1021
Chris@0 1022 return array_keys($alternatives);
Chris@0 1023 }
Chris@0 1024
Chris@0 1025 /**
Chris@0 1026 * Sets the default Command name.
Chris@0 1027 *
Chris@0 1028 * @param string $commandName The Command name
Chris@0 1029 * @param bool $isSingleCommand Set to true if there is only one command in this application
Chris@0 1030 *
Chris@0 1031 * @return self
Chris@0 1032 */
Chris@0 1033 public function setDefaultCommand($commandName, $isSingleCommand = false)
Chris@0 1034 {
Chris@0 1035 $this->defaultCommand = $commandName;
Chris@0 1036
Chris@0 1037 if ($isSingleCommand) {
Chris@0 1038 // Ensure the command exist
Chris@0 1039 $this->find($commandName);
Chris@0 1040
Chris@0 1041 $this->singleCommand = true;
Chris@0 1042 }
Chris@0 1043
Chris@0 1044 return $this;
Chris@0 1045 }
Chris@0 1046
Chris@0 1047 private function splitStringByWidth($string, $width)
Chris@0 1048 {
Chris@0 1049 // str_split is not suitable for multi-byte characters, we should use preg_split to get char array properly.
Chris@0 1050 // additionally, array_slice() is not enough as some character has doubled width.
Chris@0 1051 // we need a function to split string not by character count but by string width
Chris@0 1052 if (false === $encoding = mb_detect_encoding($string, null, true)) {
Chris@0 1053 return str_split($string, $width);
Chris@0 1054 }
Chris@0 1055
Chris@0 1056 $utf8String = mb_convert_encoding($string, 'utf8', $encoding);
Chris@0 1057 $lines = array();
Chris@0 1058 $line = '';
Chris@0 1059 foreach (preg_split('//u', $utf8String) as $char) {
Chris@0 1060 // test if $char could be appended to current line
Chris@0 1061 if (mb_strwidth($line.$char, 'utf8') <= $width) {
Chris@0 1062 $line .= $char;
Chris@0 1063 continue;
Chris@0 1064 }
Chris@0 1065 // if not, push current line to array and make new line
Chris@0 1066 $lines[] = str_pad($line, $width);
Chris@0 1067 $line = $char;
Chris@0 1068 }
Chris@0 1069 if ('' !== $line) {
Chris@0 1070 $lines[] = count($lines) ? str_pad($line, $width) : $line;
Chris@0 1071 }
Chris@0 1072
Chris@0 1073 mb_convert_variables($encoding, 'utf8', $lines);
Chris@0 1074
Chris@0 1075 return $lines;
Chris@0 1076 }
Chris@0 1077
Chris@0 1078 /**
Chris@0 1079 * Returns all namespaces of the command name.
Chris@0 1080 *
Chris@0 1081 * @param string $name The full name of the command
Chris@0 1082 *
Chris@0 1083 * @return string[] The namespaces of the command
Chris@0 1084 */
Chris@0 1085 private function extractAllNamespaces($name)
Chris@0 1086 {
Chris@0 1087 // -1 as third argument is needed to skip the command short name when exploding
Chris@0 1088 $parts = explode(':', $name, -1);
Chris@0 1089 $namespaces = array();
Chris@0 1090
Chris@0 1091 foreach ($parts as $part) {
Chris@0 1092 if (count($namespaces)) {
Chris@0 1093 $namespaces[] = end($namespaces).':'.$part;
Chris@0 1094 } else {
Chris@0 1095 $namespaces[] = $part;
Chris@0 1096 }
Chris@0 1097 }
Chris@0 1098
Chris@0 1099 return $namespaces;
Chris@0 1100 }
Chris@0 1101 }