CommandRunner.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 RuntimeException;
  22. /**
  23. * Run CLI commands for the provided application.
  24. */
  25. class CommandRunner
  26. {
  27. /**
  28. * @var \Cake\Http\BaseApplication
  29. */
  30. protected $app;
  31. /**
  32. * @var string
  33. */
  34. protected $root;
  35. /**
  36. * Constructor
  37. *
  38. * @param \Cake\Http\BaseApplication $app The application to run CLI commands for.
  39. * @param string $root The root command name to be removed from argv.
  40. */
  41. public function __construct(BaseApplication $app, $root = 'cake')
  42. {
  43. $this->app = $app;
  44. $this->root = $root;
  45. }
  46. /**
  47. * Run the command contained in $argv.
  48. *
  49. * @param array $argv The arguments from the CLI environment.
  50. * @param \Cake\Console\ConsoleIo $io The ConsoleIo instance. Used primarily for testing.
  51. * @return int The exit code of the command.
  52. * @throws \RuntimeException
  53. */
  54. public function run(array $argv, ConsoleIo $io = null)
  55. {
  56. $this->app->bootstrap();
  57. $commands = $this->app->console(new CommandCollection());
  58. if (!($commands instanceof CommandCollection)) {
  59. $type = is_object($commands) ? get_class($commands) : gettype($commands);
  60. throw new RuntimeException(
  61. "The application's `console` method did not return a CommandCollection." .
  62. " Got '{$type}' instead."
  63. );
  64. }
  65. if (empty($argv) || $argv[0] !== $this->root) {
  66. $command = empty($argv) ? '' : " `{$argv[0]}`";
  67. throw new RuntimeException(
  68. "Unknown root command{$command}. Was expecting `{$this->root}`."
  69. );
  70. }
  71. // Remove the root executable segment
  72. array_shift($argv);
  73. $io = $io ?: new ConsoleIo();
  74. $shell = $this->getShell($io, $commands, array_shift($argv));
  75. try {
  76. $shell->initialize();
  77. $result = $shell->runCommand($argv, true);
  78. } catch (StopException $e) {
  79. return $e->getCode();
  80. }
  81. if ($result === null || $result === true) {
  82. return Shell::CODE_SUCCESS;
  83. }
  84. if (is_int($result)) {
  85. return $result;
  86. }
  87. return Shell::CODE_ERROR;
  88. }
  89. /**
  90. * Get the shell instance for a given command name
  91. *
  92. * @param \Cake\Console\ConsoleIo $io The io wrapper for the created shell class.
  93. * @param \Cake\Console\CommandCollection $commands The command collection to find the shell in.
  94. * @param string $name The command name to find
  95. * @return \Cake\Console\Shell
  96. */
  97. protected function getShell(ConsoleIo $io, CommandCollection $commands, $name)
  98. {
  99. if (!$commands->has($name)) {
  100. throw new RuntimeException(
  101. "Unknown command `{$this->root} {$name}`." .
  102. " Run `{$this->root} --help` to get the list of valid commands."
  103. );
  104. }
  105. $classOrInstance = $commands->get($name);
  106. if (is_string($classOrInstance)) {
  107. return new $classOrInstance($io);
  108. }
  109. return $classOrInstance;
  110. }
  111. }