pictcode / lib / Cake / Console / ShellDispatcher.php @ b3a58ce1
履歴 | 表示 | アノテート | ダウンロード (9.985 KB)
| 1 | 
      <?php
     | 
  
|---|---|
| 2 | 
      /**
     | 
  
| 3 | 
       * ShellDispatcher file
     | 
  
| 4 | 
       *
     | 
  
| 5 | 
       * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
     | 
  
| 6 | 
       * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
     | 
  
| 7 | 
       *
     | 
  
| 8 | 
       * Licensed under The MIT License
     | 
  
| 9 | 
       * For full copyright and license information, please see the LICENSE.txt
     | 
  
| 10 | 
       * Redistributions of files must retain the above copyright notice.
     | 
  
| 11 | 
       *
     | 
  
| 12 | 
       * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
     | 
  
| 13 | 
       * @link          http://cakephp.org CakePHP(tm) Project
     | 
  
| 14 | 
       * @since         CakePHP(tm) v 2.0
     | 
  
| 15 | 
       * @license       http://www.opensource.org/licenses/mit-license.php MIT License
     | 
  
| 16 | 
       */
     | 
  
| 17 | 
       | 
  
| 18 | 
      /**
     | 
  
| 19 | 
       * Shell dispatcher handles dispatching cli commands.
     | 
  
| 20 | 
       *
     | 
  
| 21 | 
       * @package       Cake.Console
     | 
  
| 22 | 
       */
     | 
  
| 23 | 
      class ShellDispatcher {  | 
  
| 24 | 
       | 
  
| 25 | 
      /**
     | 
  
| 26 | 
       * Contains command switches parsed from the command line.
     | 
  
| 27 | 
       *
     | 
  
| 28 | 
       * @var array
     | 
  
| 29 | 
       */
     | 
  
| 30 | 
      public $params = array();  | 
  
| 31 | 
       | 
  
| 32 | 
      /**
     | 
  
| 33 | 
       * Contains arguments parsed from the command line.
     | 
  
| 34 | 
       *
     | 
  
| 35 | 
       * @var array
     | 
  
| 36 | 
       */
     | 
  
| 37 | 
      public $args = array();  | 
  
| 38 | 
       | 
  
| 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 = array(), $bootstrap = true) {  | 
  
| 49 | 
      set_time_limit(0);  | 
  
| 50 | 
      $this->parseParams($args);  | 
  
| 51 | 
       | 
  
| 52 | 
      if ($bootstrap) {  | 
  
| 53 | 
                              $this->_initConstants();
     | 
  
| 54 | 
                              $this->_initEnvironment();
     | 
  
| 55 | 
      }  | 
  
| 56 | 
      }  | 
  
| 57 | 
       | 
  
| 58 | 
      /**
     | 
  
| 59 | 
       * Run the dispatcher
     | 
  
| 60 | 
       *
     | 
  
| 61 | 
       * @param array $argv The argv from PHP
     | 
  
| 62 | 
       * @return void
     | 
  
| 63 | 
       */
     | 
  
| 64 | 
      public static function run($argv) {  | 
  
| 65 | 
      $dispatcher = new ShellDispatcher($argv);  | 
  
| 66 | 
      return $dispatcher->_stop($dispatcher->dispatch() === false ? 1 : 0);  | 
  
| 67 | 
      }  | 
  
| 68 | 
       | 
  
| 69 | 
      /**
     | 
  
| 70 | 
       * Defines core configuration.
     | 
  
| 71 | 
       *
     | 
  
| 72 | 
       * @return void
     | 
  
| 73 | 
       */
     | 
  
| 74 | 
      protected function _initConstants() {  | 
  
| 75 | 
      if (function_exists('ini_set')) {  | 
  
| 76 | 
      ini_set('html_errors', false);  | 
  
| 77 | 
      ini_set('implicit_flush', true);  | 
  
| 78 | 
      ini_set('max_execution_time', 0);  | 
  
| 79 | 
      }  | 
  
| 80 | 
       | 
  
| 81 | 
      if (!defined('CAKE_CORE_INCLUDE_PATH')) {  | 
  
| 82 | 
      define('CAKE_CORE_INCLUDE_PATH', dirname(dirname(dirname(__FILE__))));  | 
  
| 83 | 
      define('CAKEPHP_SHELL', true);  | 
  
| 84 | 
      if (!defined('DS')) {  | 
  
| 85 | 
      define('DS', DIRECTORY_SEPARATOR);  | 
  
| 86 | 
      }  | 
  
| 87 | 
      if (!defined('CORE_PATH')) {  | 
  
| 88 | 
      define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);  | 
  
| 89 | 
      }  | 
  
| 90 | 
      }  | 
  
| 91 | 
      }  | 
  
| 92 | 
       | 
  
| 93 | 
      /**
     | 
  
| 94 | 
       * Defines current working environment.
     | 
  
| 95 | 
       *
     | 
  
| 96 | 
       * @return void
     | 
  
| 97 | 
       * @throws CakeException
     | 
  
| 98 | 
       */
     | 
  
| 99 | 
      protected function _initEnvironment() {  | 
  
| 100 | 
      if (!$this->_bootstrap()) {  | 
  
| 101 | 
      $message = "Unable to load CakePHP core.\nMake sure " . DS . 'lib' . DS . 'Cake exists in ' . CAKE_CORE_INCLUDE_PATH;  | 
  
| 102 | 
      throw new CakeException($message);  | 
  
| 103 | 
      }  | 
  
| 104 | 
       | 
  
| 105 | 
      if (!isset($this->args[0]) || !isset($this->params['working'])) {  | 
  
| 106 | 
      $message = "This file has been loaded incorrectly and cannot continue.\n" .  | 
  
| 107 | 
      "Please make sure that " . DS . 'lib' . DS . 'Cake' . DS . "Console is in your system path,\n" .  | 
  
| 108 | 
                                      "and check the cookbook for the correct usage of this command.\n" .
     | 
  
| 109 | 
                                      "(http://book.cakephp.org/)";
     | 
  
| 110 | 
      throw new CakeException($message);  | 
  
| 111 | 
      }  | 
  
| 112 | 
       | 
  
| 113 | 
                      $this->shiftArgs();
     | 
  
| 114 | 
      }  | 
  
| 115 | 
       | 
  
| 116 | 
      /**
     | 
  
| 117 | 
       * Initializes the environment and loads the CakePHP core.
     | 
  
| 118 | 
       *
     | 
  
| 119 | 
       * @return bool Success.
     | 
  
| 120 | 
       */
     | 
  
| 121 | 
      protected function _bootstrap() {  | 
  
| 122 | 
      if (!defined('ROOT')) {  | 
  
| 123 | 
      define('ROOT', $this->params['root']);  | 
  
| 124 | 
      }  | 
  
| 125 | 
      if (!defined('APP_DIR')) {  | 
  
| 126 | 
      define('APP_DIR', $this->params['app']);  | 
  
| 127 | 
      }  | 
  
| 128 | 
      if (!defined('APP')) {  | 
  
| 129 | 
      define('APP', $this->params['working'] . DS);  | 
  
| 130 | 
      }  | 
  
| 131 | 
      if (!defined('WWW_ROOT')) {  | 
  
| 132 | 
      define('WWW_ROOT', APP . $this->params['webroot'] . DS);  | 
  
| 133 | 
      }  | 
  
| 134 | 
      if (!defined('TMP') && !is_dir(APP . 'tmp')) {  | 
  
| 135 | 
      define('TMP', CAKE_CORE_INCLUDE_PATH . DS . 'Cake' . DS . 'Console' . DS . 'Templates' . DS . 'skel' . DS . 'tmp' . DS);  | 
  
| 136 | 
      }  | 
  
| 137 | 
      $boot = file_exists(ROOT . DS . APP_DIR . DS . 'Config' . DS . 'bootstrap.php');  | 
  
| 138 | 
      require CORE_PATH . 'Cake' . DS . 'bootstrap.php';  | 
  
| 139 | 
       | 
  
| 140 | 
      if (!file_exists(APP . 'Config' . DS . 'core.php')) {  | 
  
| 141 | 
      include_once CAKE_CORE_INCLUDE_PATH . DS . 'Cake' . DS . 'Console' . DS . 'Templates' . DS . 'skel' . DS . 'Config' . DS . 'core.php';  | 
  
| 142 | 
                              App::build();
     | 
  
| 143 | 
      }  | 
  
| 144 | 
       | 
  
| 145 | 
                      $this->setErrorHandlers();
     | 
  
| 146 | 
       | 
  
| 147 | 
      if (!defined('FULL_BASE_URL')) {  | 
  
| 148 | 
      $url = Configure::read('App.fullBaseUrl');  | 
  
| 149 | 
      define('FULL_BASE_URL', $url ? $url : 'http://localhost');  | 
  
| 150 | 
      Configure::write('App.fullBaseUrl', FULL_BASE_URL);  | 
  
| 151 | 
      }  | 
  
| 152 | 
       | 
  
| 153 | 
      return true;  | 
  
| 154 | 
      }  | 
  
| 155 | 
       | 
  
| 156 | 
      /**
     | 
  
| 157 | 
       * Set the error/exception handlers for the console
     | 
  
| 158 | 
       * based on the `Error.consoleHandler`, and `Exception.consoleHandler` values
     | 
  
| 159 | 
       * if they are set. If they are not set, the default ConsoleErrorHandler will be
     | 
  
| 160 | 
       * used.
     | 
  
| 161 | 
       *
     | 
  
| 162 | 
       * @return void
     | 
  
| 163 | 
       */
     | 
  
| 164 | 
      public function setErrorHandlers() {  | 
  
| 165 | 
      App::uses('ConsoleErrorHandler', 'Console');  | 
  
| 166 | 
      $error = Configure::read('Error');  | 
  
| 167 | 
      $exception = Configure::read('Exception');  | 
  
| 168 | 
       | 
  
| 169 | 
      $errorHandler = new ConsoleErrorHandler();  | 
  
| 170 | 
      if (empty($error['consoleHandler'])) {  | 
  
| 171 | 
      $error['consoleHandler'] = array($errorHandler, 'handleError');  | 
  
| 172 | 
      Configure::write('Error', $error);  | 
  
| 173 | 
      }  | 
  
| 174 | 
      if (empty($exception['consoleHandler'])) {  | 
  
| 175 | 
      $exception['consoleHandler'] = array($errorHandler, 'handleException');  | 
  
| 176 | 
      Configure::write('Exception', $exception);  | 
  
| 177 | 
      }  | 
  
| 178 | 
      set_exception_handler($exception['consoleHandler']);  | 
  
| 179 | 
      set_error_handler($error['consoleHandler'], Configure::read('Error.level'));  | 
  
| 180 | 
       | 
  
| 181 | 
      App::uses('Debugger', 'Utility');  | 
  
| 182 | 
      Debugger::getInstance()->output('txt');  | 
  
| 183 | 
      }  | 
  
| 184 | 
       | 
  
| 185 | 
      /**
     | 
  
| 186 | 
       * Dispatches a CLI request
     | 
  
| 187 | 
       *
     | 
  
| 188 | 
       * @return bool
     | 
  
| 189 | 
       * @throws MissingShellMethodException
     | 
  
| 190 | 
       */
     | 
  
| 191 | 
      public function dispatch() {  | 
  
| 192 | 
      $shell = $this->shiftArgs();  | 
  
| 193 | 
       | 
  
| 194 | 
      if (!$shell) {  | 
  
| 195 | 
                              $this->help();
     | 
  
| 196 | 
      return false;  | 
  
| 197 | 
      }  | 
  
| 198 | 
      if (in_array($shell, array('help', '--help', '-h'))) {  | 
  
| 199 | 
                              $this->help();
     | 
  
| 200 | 
      return true;  | 
  
| 201 | 
      }  | 
  
| 202 | 
       | 
  
| 203 | 
      $Shell = $this->_getShell($shell);  | 
  
| 204 | 
       | 
  
| 205 | 
      $command = null;  | 
  
| 206 | 
      if (isset($this->args[0])) {  | 
  
| 207 | 
      $command = $this->args[0];  | 
  
| 208 | 
      }  | 
  
| 209 | 
       | 
  
| 210 | 
      if ($Shell instanceof Shell) {  | 
  
| 211 | 
                              $Shell->initialize();
     | 
  
| 212 | 
      return $Shell->runCommand($command, $this->args);  | 
  
| 213 | 
      }  | 
  
| 214 | 
      $methods = array_diff(get_class_methods($Shell), get_class_methods('Shell'));  | 
  
| 215 | 
      $added = in_array($command, $methods);  | 
  
| 216 | 
      $private = $command[0] === '_' && method_exists($Shell, $command);  | 
  
| 217 | 
       | 
  
| 218 | 
      if (!$private) {  | 
  
| 219 | 
      if ($added) {  | 
  
| 220 | 
                                      $this->shiftArgs();
     | 
  
| 221 | 
                                      $Shell->startup();
     | 
  
| 222 | 
      return $Shell->{$command}();  | 
  
| 223 | 
      }  | 
  
| 224 | 
      if (method_exists($Shell, 'main')) {  | 
  
| 225 | 
                                      $Shell->startup();
     | 
  
| 226 | 
      return $Shell->main();  | 
  
| 227 | 
      }  | 
  
| 228 | 
      }  | 
  
| 229 | 
       | 
  
| 230 | 
      throw new MissingShellMethodException(array('shell' => $shell, 'method' => $command));  | 
  
| 231 | 
      }  | 
  
| 232 | 
       | 
  
| 233 | 
      /**
     | 
  
| 234 | 
       * Get shell to use, either plugin shell or application shell
     | 
  
| 235 | 
       *
     | 
  
| 236 | 
       * All paths in the loaded shell paths are searched.
     | 
  
| 237 | 
       *
     | 
  
| 238 | 
       * @param string $shell Optionally the name of a plugin
     | 
  
| 239 | 
       * @return mixed An object
     | 
  
| 240 | 
       * @throws MissingShellException when errors are encountered.
     | 
  
| 241 | 
       */
     | 
  
| 242 | 
      protected function _getShell($shell) {  | 
  
| 243 | 
      list($plugin, $shell) = pluginSplit($shell, true);  | 
  
| 244 | 
       | 
  
| 245 | 
      $plugin = Inflector::camelize($plugin);  | 
  
| 246 | 
      $class = Inflector::camelize($shell) . 'Shell';  | 
  
| 247 | 
       | 
  
| 248 | 
      App::uses('Shell', 'Console');  | 
  
| 249 | 
      App::uses('AppShell', 'Console/Command');  | 
  
| 250 | 
      App::uses($class, $plugin . 'Console/Command');  | 
  
| 251 | 
       | 
  
| 252 | 
      if (!class_exists($class)) {  | 
  
| 253 | 
      $plugin = Inflector::camelize($shell) . '.';  | 
  
| 254 | 
      App::uses($class, $plugin . 'Console/Command');  | 
  
| 255 | 
      }  | 
  
| 256 | 
       | 
  
| 257 | 
      if (!class_exists($class)) {  | 
  
| 258 | 
      throw new MissingShellException(array(  | 
  
| 259 | 
      'class' => $class  | 
  
| 260 | 
      ));  | 
  
| 261 | 
      }  | 
  
| 262 | 
      $Shell = new $class();  | 
  
| 263 | 
      $Shell->plugin = trim($plugin, '.');  | 
  
| 264 | 
      return $Shell;  | 
  
| 265 | 
      }  | 
  
| 266 | 
       | 
  
| 267 | 
      /**
     | 
  
| 268 | 
       * Parses command line options and extracts the directory paths from $params
     | 
  
| 269 | 
       *
     | 
  
| 270 | 
       * @param array $args Parameters to parse
     | 
  
| 271 | 
       * @return void
     | 
  
| 272 | 
       */
     | 
  
| 273 | 
      public function parseParams($args) {  | 
  
| 274 | 
      $this->_parsePaths($args);  | 
  
| 275 | 
       | 
  
| 276 | 
      $defaults = array(  | 
  
| 277 | 
      'app' => 'app',  | 
  
| 278 | 
      'root' => dirname(dirname(dirname(dirname(__FILE__)))),  | 
  
| 279 | 
      'working' => null,  | 
  
| 280 | 
      'webroot' => 'webroot'  | 
  
| 281 | 
      );  | 
  
| 282 | 
      $params = array_merge($defaults, array_intersect_key($this->params, $defaults));  | 
  
| 283 | 
      $isWin = false;  | 
  
| 284 | 
      foreach ($defaults as $default => $value) {  | 
  
| 285 | 
      if (strpos($params[$default], '\\') !== false) {  | 
  
| 286 | 
      $isWin = true;  | 
  
| 287 | 
                                      break;
     | 
  
| 288 | 
      }  | 
  
| 289 | 
      }  | 
  
| 290 | 
      $params = str_replace('\\', '/', $params);  | 
  
| 291 | 
       | 
  
| 292 | 
      if (isset($params['working'])) {  | 
  
| 293 | 
      $params['working'] = trim($params['working']);  | 
  
| 294 | 
      }  | 
  
| 295 | 
       | 
  
| 296 | 
      if (!empty($params['working']) && (!isset($this->args[0]) || isset($this->args[0]) && $this->args[0][0] !== '.')) {  | 
  
| 297 | 
      if ($params['working'][0] === '.') {  | 
  
| 298 | 
      $params['working'] = realpath($params['working']);  | 
  
| 299 | 
      }  | 
  
| 300 | 
      if (empty($this->params['app']) && $params['working'] != $params['root']) {  | 
  
| 301 | 
      $params['root'] = dirname($params['working']);  | 
  
| 302 | 
      $params['app'] = basename($params['working']);  | 
  
| 303 | 
                              } else {
     | 
  
| 304 | 
      $params['root'] = $params['working'];  | 
  
| 305 | 
      }  | 
  
| 306 | 
      }  | 
  
| 307 | 
       | 
  
| 308 | 
      if ($params['app'][0] === '/' || preg_match('/([a-z])(:)/i', $params['app'], $matches)) {  | 
  
| 309 | 
      $params['root'] = dirname($params['app']);  | 
  
| 310 | 
      } elseif (strpos($params['app'], '/')) {  | 
  
| 311 | 
      $params['root'] .= '/' . dirname($params['app']);  | 
  
| 312 | 
      }  | 
  
| 313 | 
       | 
  
| 314 | 
      $params['app'] = basename($params['app']);  | 
  
| 315 | 
      $params['working'] = rtrim($params['root'], '/');  | 
  
| 316 | 
      if (!$isWin || !preg_match('/^[A-Z]:$/i', $params['app'])) {  | 
  
| 317 | 
      $params['working'] .= '/' . $params['app'];  | 
  
| 318 | 
      }  | 
  
| 319 | 
       | 
  
| 320 | 
      if (!empty($matches[0]) || !empty($isWin)) {  | 
  
| 321 | 
      $params = str_replace('/', '\\', $params);  | 
  
| 322 | 
      }  | 
  
| 323 | 
       | 
  
| 324 | 
      $this->params = $params + $this->params;  | 
  
| 325 | 
      }  | 
  
| 326 | 
       | 
  
| 327 | 
      /**
     | 
  
| 328 | 
       * Parses out the paths from from the argv
     | 
  
| 329 | 
       *
     | 
  
| 330 | 
       * @param array $args The argv to parse.
     | 
  
| 331 | 
       * @return void
     | 
  
| 332 | 
       */
     | 
  
| 333 | 
      protected function _parsePaths($args) {  | 
  
| 334 | 
      $parsed = array();  | 
  
| 335 | 
      $keys = array('-working', '--working', '-app', '--app', '-root', '--root');  | 
  
| 336 | 
      $args = (array)$args;  | 
  
| 337 | 
      foreach ($keys as $key) {  | 
  
| 338 | 
      while (($index = array_search($key, $args)) !== false) {  | 
  
| 339 | 
      $keyname = str_replace('-', '', $key);  | 
  
| 340 | 
      $valueIndex = $index + 1;  | 
  
| 341 | 
      $parsed[$keyname] = $args[$valueIndex];  | 
  
| 342 | 
      array_splice($args, $index, 2);  | 
  
| 343 | 
      }  | 
  
| 344 | 
      }  | 
  
| 345 | 
      $this->args = $args;  | 
  
| 346 | 
      $this->params = $parsed;  | 
  
| 347 | 
      }  | 
  
| 348 | 
       | 
  
| 349 | 
      /**
     | 
  
| 350 | 
       * Removes first argument and shifts other arguments up
     | 
  
| 351 | 
       *
     | 
  
| 352 | 
       * @return mixed Null if there are no arguments otherwise the shifted argument
     | 
  
| 353 | 
       */
     | 
  
| 354 | 
      public function shiftArgs() {  | 
  
| 355 | 
      return array_shift($this->args);  | 
  
| 356 | 
      }  | 
  
| 357 | 
       | 
  
| 358 | 
      /**
     | 
  
| 359 | 
       * Shows console help. Performs an internal dispatch to the CommandList Shell
     | 
  
| 360 | 
       *
     | 
  
| 361 | 
       * @return void
     | 
  
| 362 | 
       */
     | 
  
| 363 | 
      public function help() {  | 
  
| 364 | 
      $this->args = array_merge(array('command_list'), $this->args);  | 
  
| 365 | 
                      $this->dispatch();
     | 
  
| 366 | 
      }  | 
  
| 367 | 
       | 
  
| 368 | 
      /**
     | 
  
| 369 | 
       * Stop execution of the current script
     | 
  
| 370 | 
       *
     | 
  
| 371 | 
       * @param int|string $status see http://php.net/exit for values
     | 
  
| 372 | 
       * @return void
     | 
  
| 373 | 
       */
     | 
  
| 374 | 
      protected function _stop($status = 0) {  | 
  
| 375 | 
      exit($status);  | 
  
| 376 | 
      }  | 
  
| 377 | 
       | 
  
| 378 | 
      }  |