CommandRunner.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.5.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Console;
  16. use Cake\Console\CommandCollection;
  17. use Cake\Console\ConsoleIo;
  18. use Cake\Console\Exception\StopException;
  19. use Cake\Console\Shell;
  20. use Cake\Http\BaseApplication;
  21. use Cake\Shell\VersionShell;
  22. use RuntimeException;
  23. /**
  24. * Run CLI commands for the provided application.
  25. */
  26. class CommandRunner
  27. {
  28. /**
  29. * The application console commands are being run for.
  30. *
  31. * @var \Cake\Http\BaseApplication
  32. */
  33. protected $app;
  34. /**
  35. * The root command name. Defaults to `cake`.
  36. *
  37. * @var string
  38. */
  39. protected $root;
  40. /**
  41. * Alias mappings.
  42. *
  43. * @var array
  44. */
  45. protected $aliases = [];
  46. /**
  47. * Constructor
  48. *
  49. * @param \Cake\Http\BaseApplication $app The application to run CLI commands for.
  50. * @param string $root The root command name to be removed from argv.
  51. */
  52. public function __construct(BaseApplication $app, $root = 'cake')
  53. {
  54. $this->app = $app;
  55. $this->root = $root;
  56. $this->aliases = [
  57. '--version' => 'version'
  58. ];
  59. }
  60. /**
  61. * Replace the entire alias map for a runner.
  62. *
  63. * Aliases allow you to define alternate names for commands
  64. * in the collection. This can be useful to add top level switches
  65. * like `--version` or `-h`
  66. *
  67. * ### Usage
  68. *
  69. * ```
  70. * $runner->setAliases(['--version' => 'version']);
  71. * ```
  72. *
  73. * @param array $aliases The map of aliases to replace.
  74. * @return $this
  75. */
  76. public function setAliases(array $aliases)
  77. {
  78. $this->aliases = $aliases;
  79. return $this;
  80. }
  81. /**
  82. * Run the command contained in $argv.
  83. *
  84. * @param array $argv The arguments from the CLI environment.
  85. * @param \Cake\Console\ConsoleIo $io The ConsoleIo instance. Used primarily for testing.
  86. * @return int The exit code of the command.
  87. * @throws \RuntimeException
  88. */
  89. public function run(array $argv, ConsoleIo $io = null)
  90. {
  91. $this->app->bootstrap();
  92. $commands = new CommandCollection([
  93. 'version' => VersionShell::class,
  94. ]);
  95. $commands = $this->app->console($commands);
  96. if (!($commands instanceof CommandCollection)) {
  97. $type = is_object($commands) ? get_class($commands) : gettype($commands);
  98. throw new RuntimeException(
  99. "The application's `console` method did not return a CommandCollection." .
  100. " Got '{$type}' instead."
  101. );
  102. }
  103. if (empty($argv) || $argv[0] !== $this->root) {
  104. $command = empty($argv) ? '' : " `{$argv[0]}`";
  105. throw new RuntimeException(
  106. "Unknown root command{$command}. Was expecting `{$this->root}`."
  107. );
  108. }
  109. // Remove the root executable segment
  110. array_shift($argv);
  111. $io = $io ?: new ConsoleIo();
  112. $shell = $this->getShell($io, $commands, array_shift($argv));
  113. try {
  114. $shell->initialize();
  115. $result = $shell->runCommand($argv, true);
  116. } catch (StopException $e) {
  117. return $e->getCode();
  118. }
  119. if ($result === null || $result === true) {
  120. return Shell::CODE_SUCCESS;
  121. }
  122. if (is_int($result)) {
  123. return $result;
  124. }
  125. return Shell::CODE_ERROR;
  126. }
  127. /**
  128. * Get the shell instance for a given command name
  129. *
  130. * @param \Cake\Console\ConsoleIo $io The io wrapper for the created shell class.
  131. * @param \Cake\Console\CommandCollection $commands The command collection to find the shell in.
  132. * @param string $name The command name to find
  133. * @return \Cake\Console\Shell
  134. */
  135. protected function getShell(ConsoleIo $io, CommandCollection $commands, $name)
  136. {
  137. if (isset($this->aliases[$name])) {
  138. $name = $this->aliases[$name];
  139. }
  140. if (!$commands->has($name)) {
  141. throw new RuntimeException(
  142. "Unknown command `{$this->root} {$name}`." .
  143. " Run `{$this->root} --help` to get the list of valid commands."
  144. );
  145. }
  146. $classOrInstance = $commands->get($name);
  147. if (is_string($classOrInstance)) {
  148. return new $classOrInstance($io);
  149. }
  150. return $classOrInstance;
  151. }
  152. }