ShellDispatcher.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 2.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Console;
  16. use Cake\Core\App;
  17. use Cake\Core\Configure;
  18. use Cake\Error\Exception;
  19. use Cake\Utility\Inflector;
  20. /**
  21. * Shell dispatcher handles dispatching cli commands.
  22. *
  23. * Consult https://github.com/cakephp/app/tree/master/App/Console/cake.php
  24. * for how this class is used in practice.
  25. */
  26. class ShellDispatcher {
  27. /**
  28. * Contains arguments parsed from the command line.
  29. *
  30. * @var array
  31. */
  32. public $args = [];
  33. /**
  34. * List of connected aliases.
  35. *
  36. * @var array
  37. */
  38. protected static $_aliases = [];
  39. /**
  40. * Constructor
  41. *
  42. * The execution of the script is stopped after dispatching the request with
  43. * a status code of either 0 or 1 according to the result of the dispatch.
  44. *
  45. * @param array $args the argv from PHP
  46. * @param bool $bootstrap Should the environment be bootstrapped.
  47. */
  48. public function __construct($args = [], $bootstrap = true) {
  49. set_time_limit(0);
  50. $this->args = $args;
  51. if ($bootstrap) {
  52. $this->_initEnvironment();
  53. }
  54. }
  55. /**
  56. * Add an alias for a shell command.
  57. *
  58. * Aliases allow you to call shells by alternate names. This is most
  59. * useful when dealing with plugin shells that you want to have shorter
  60. * names for.
  61. *
  62. * If you re-use an alias the last alias set will be the one available.
  63. *
  64. * @param string $short The new short name for the shell.
  65. * @param string $original The original full name for the shell.
  66. * @return void
  67. */
  68. public static function alias($short, $original) {
  69. static::$_aliases[$short] = $original;
  70. }
  71. /**
  72. * Clear any aliases that have been set.
  73. *
  74. * @return void
  75. */
  76. public static function resetAliases() {
  77. static::$_aliases = [];
  78. }
  79. /**
  80. * Run the dispatcher
  81. *
  82. * @param array $argv The argv from PHP
  83. * @return int The exit code of the shell process.
  84. */
  85. public static function run($argv) {
  86. $dispatcher = new ShellDispatcher($argv);
  87. return $dispatcher->dispatch();
  88. }
  89. /**
  90. * Defines current working environment.
  91. *
  92. * @return void
  93. * @throws \Cake\Error\Exception
  94. */
  95. protected function _initEnvironment() {
  96. if (!$this->_bootstrap()) {
  97. $message = "Unable to load CakePHP core.\nMake sure Cake exists in " . CAKE_CORE_INCLUDE_PATH;
  98. throw new Exception($message);
  99. }
  100. if (function_exists('ini_set')) {
  101. ini_set('html_errors', false);
  102. ini_set('implicit_flush', true);
  103. ini_set('max_execution_time', 0);
  104. }
  105. $this->shiftArgs();
  106. }
  107. /**
  108. * Initializes the environment and loads the CakePHP core.
  109. *
  110. * @return bool Success.
  111. */
  112. protected function _bootstrap() {
  113. if (!Configure::read('App.fullBaseUrl')) {
  114. Configure::write('App.fullBaseUrl', 'http://localhost');
  115. }
  116. return true;
  117. }
  118. /**
  119. * Dispatches a CLI request
  120. *
  121. * @return int The cli command exit code. 0 is success.
  122. */
  123. public function dispatch() {
  124. return $this->_dispatch() === true ? 0 : 1;
  125. }
  126. /**
  127. * Dispatch a request.
  128. *
  129. * @return bool
  130. * @throws \Cake\Console\Error\MissingShellMethodException
  131. */
  132. protected function _dispatch() {
  133. $shell = $this->shiftArgs();
  134. if (!$shell) {
  135. $this->help();
  136. return false;
  137. }
  138. if (in_array($shell, ['help', '--help', '-h'])) {
  139. $this->help();
  140. return true;
  141. }
  142. $Shell = $this->findShell($shell);
  143. $Shell->initialize();
  144. return $Shell->runCommand($this->args, true);
  145. }
  146. /**
  147. * Get shell to use, either plugin shell or application shell
  148. *
  149. * All paths in the loaded shell paths are searched.
  150. *
  151. * @param string $shell Optionally the name of a plugin
  152. * @return \Cake\Console\Shell A shell instance.
  153. * @throws \Cake\Console\Error\MissingShellException when errors are encountered.
  154. */
  155. public function findShell($shell) {
  156. $classname = $this->_shellExists($shell);
  157. if (!$classname && isset(static::$_aliases[$shell])) {
  158. $shell = static::$_aliases[$shell];
  159. $classname = $this->_shellExists($shell);
  160. }
  161. if ($classname) {
  162. list($plugin) = pluginSplit($shell);
  163. $instance = new $classname();
  164. $instance->plugin = Inflector::camelize(trim($plugin, '.'));
  165. return $instance;
  166. }
  167. throw new Error\MissingShellException([
  168. 'class' => $shell,
  169. ]);
  170. }
  171. /**
  172. * Check if a shell class exists for the given name.
  173. *
  174. * @param string $shell The shell name to look for.
  175. * @return string|boolean Either the classname or false.
  176. */
  177. protected function _shellExists($shell) {
  178. $class = array_map('Cake\Utility\Inflector::camelize', explode('.', $shell));
  179. $class = implode('.', $class);
  180. $class = App::classname($class, 'Console/Command', 'Shell');
  181. if (class_exists($class)) {
  182. return $class;
  183. }
  184. return false;
  185. }
  186. /**
  187. * Removes first argument and shifts other arguments up
  188. *
  189. * @return mixed Null if there are no arguments otherwise the shifted argument
  190. */
  191. public function shiftArgs() {
  192. return array_shift($this->args);
  193. }
  194. /**
  195. * Shows console help. Performs an internal dispatch to the CommandList Shell
  196. *
  197. * @return void
  198. */
  199. public function help() {
  200. $this->args = array_merge(['command_list'], $this->args);
  201. $this->dispatch();
  202. }
  203. /**
  204. * Stop execution of the current script
  205. *
  206. * @param int|string $status see http://php.net/exit for values
  207. * @return void
  208. */
  209. protected function _stop($status = 0) {
  210. exit($status);
  211. }
  212. }