1: <?php
  2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16: 
 17: 
 18:  19:  20:  21:  22: 
 23: class ShellDispatcher {
 24: 
 25:  26:  27:  28:  29: 
 30:     public $params = array();
 31: 
 32:  33:  34:  35:  36: 
 37:     public $args = array();
 38: 
 39:  40:  41:  42:  43:  44:  45:  46:  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:  60:  61:  62:  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:  71:  72:  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:  95:  96:  97:  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:                 "(https://book.cakephp.org/)";
110:             throw new CakeException($message);
111:         }
112: 
113:         $this->shiftArgs();
114:     }
115: 
116: 117: 118: 119: 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:             if (!$this->_isAbsolutePath($this->params['webroot'])) {
133:                 $webroot = realpath(APP . $this->params['webroot']);
134:             } else {
135:                 $webroot = $this->params['webroot'];
136:             }
137:             define('WWW_ROOT', $webroot . DS);
138:         }
139:         if (!defined('TMP') && !is_dir(APP . 'tmp')) {
140:             define('TMP', CAKE_CORE_INCLUDE_PATH . DS . 'Cake' . DS . 'Console' . DS . 'Templates' . DS . 'skel' . DS . 'tmp' . DS);
141:         }
142: 
143:         if (!defined('CONFIG')) {
144:             define('CONFIG', ROOT . DS . APP_DIR . DS . 'Config' . DS);
145:         }
146:         
147:         $boot = file_exists(CONFIG . 'bootstrap.php');
148:         require CORE_PATH . 'Cake' . DS . 'bootstrap.php';
149: 
150:         if (!file_exists(CONFIG . 'core.php')) {
151:             include_once CAKE_CORE_INCLUDE_PATH . DS . 'Cake' . DS . 'Console' . DS . 'Templates' . DS . 'skel' . DS . 'Config' . DS . 'core.php';
152:             App::build();
153:         }
154: 
155:         $this->setErrorHandlers();
156: 
157:         if (!defined('FULL_BASE_URL')) {
158:             $url = Configure::read('App.fullBaseUrl');
159:             define('FULL_BASE_URL', $url ? $url : 'http://localhost');
160:             Configure::write('App.fullBaseUrl', FULL_BASE_URL);
161:         }
162: 
163:         return true;
164:     }
165: 
166: 167: 168: 169: 170: 171: 172: 173: 
174:     public function setErrorHandlers() {
175:         App::uses('ConsoleErrorHandler', 'Console');
176:         $error = Configure::read('Error');
177:         $exception = Configure::read('Exception');
178: 
179:         $errorHandler = new ConsoleErrorHandler();
180:         if (empty($error['consoleHandler'])) {
181:             $error['consoleHandler'] = array($errorHandler, 'handleError');
182:             Configure::write('Error', $error);
183:         }
184:         if (empty($exception['consoleHandler'])) {
185:             $exception['consoleHandler'] = array($errorHandler, 'handleException');
186:             Configure::write('Exception', $exception);
187:         }
188:         set_exception_handler($exception['consoleHandler']);
189:         set_error_handler($error['consoleHandler'], Configure::read('Error.level'));
190: 
191:         App::uses('Debugger', 'Utility');
192:         Debugger::getInstance()->output('txt');
193:     }
194: 
195: 196: 197: 198: 199: 200: 
201:     public function dispatch() {
202:         $shell = $this->shiftArgs();
203: 
204:         if (!$shell) {
205:             $this->help();
206:             return false;
207:         }
208:         if (in_array($shell, array('help', '--help', '-h'))) {
209:             $this->help();
210:             return true;
211:         }
212: 
213:         $Shell = $this->_getShell($shell);
214: 
215:         $command = null;
216:         if (isset($this->args[0])) {
217:             $command = $this->args[0];
218:         }
219: 
220:         if ($Shell instanceof Shell) {
221:             $Shell->initialize();
222:             return $Shell->runCommand($command, $this->args);
223:         }
224:         $methods = array_diff(get_class_methods($Shell), get_class_methods('Shell'));
225:         $added = in_array($command, $methods);
226:         $private = substr($command, 0, 1) === '_' && method_exists($Shell, $command);
227: 
228:         if (!$private) {
229:             if ($added) {
230:                 $this->shiftArgs();
231:                 $Shell->startup();
232:                 return $Shell->{$command}();
233:             }
234:             if (method_exists($Shell, 'main')) {
235:                 $Shell->startup();
236:                 return $Shell->main();
237:             }
238:         }
239: 
240:         throw new MissingShellMethodException(array('shell' => $shell, 'method' => $command));
241:     }
242: 
243: 244: 245: 246: 247: 248: 249: 250: 251: 
252:     protected function _getShell($shell) {
253:         list($plugin, $shell) = pluginSplit($shell, true);
254: 
255:         $plugin = Inflector::camelize($plugin);
256:         $class = Inflector::camelize($shell) . 'Shell';
257: 
258:         App::uses('Shell', 'Console');
259:         App::uses('AppShell', 'Console/Command');
260:         App::uses($class, $plugin . 'Console/Command');
261: 
262:         if (!class_exists($class)) {
263:             $plugin = Inflector::camelize($shell) . '.';
264:             App::uses($class, $plugin . 'Console/Command');
265:         }
266: 
267:         if (!class_exists($class)) {
268:             throw new MissingShellException(array(
269:                 'class' => $class
270:             ));
271:         }
272:         $Shell = new $class();
273:         $Shell->plugin = trim($plugin, '.');
274:         return $Shell;
275:     }
276: 
277: 278: 279: 280: 281: 282: 
283:     public function parseParams($args) {
284:         $this->_parsePaths($args);
285: 
286:         $defaults = array(
287:             'app' => 'app',
288:             'root' => dirname(dirname(dirname(dirname(__FILE__)))),
289:             'working' => null,
290:             'webroot' => 'webroot'
291:         );
292:         $params = array_merge($defaults, array_intersect_key($this->params, $defaults));
293:         $isWin = false;
294:         foreach ($defaults as $default => $value) {
295:             if (strpos($params[$default], '\\') !== false) {
296:                 $isWin = true;
297:                 break;
298:             }
299:         }
300:         $params = str_replace('\\', '/', $params);
301: 
302:         if (isset($params['working'])) {
303:             $params['working'] = trim($params['working']);
304:         }
305: 
306:         if (!empty($params['working']) && (!isset($this->args[0]) || isset($this->args[0]) && $this->args[0][0] !== '.')) {
307:             if ($params['working'][0] === '.') {
308:                 $params['working'] = realpath($params['working']);
309:             }
310:             if (empty($this->params['app']) && $params['working'] != $params['root']) {
311:                 $params['root'] = dirname($params['working']);
312:                 $params['app'] = basename($params['working']);
313:             } else {
314:                 $params['root'] = $params['working'];
315:             }
316:         }
317: 
318:         if ($this->_isAbsolutePath($params['app'])) {
319:             $params['root'] = dirname($params['app']);
320:         } elseif (strpos($params['app'], '/')) {
321:             $params['root'] .= '/' . dirname($params['app']);
322:         }
323:         $isWindowsAppPath = $this->_isWindowsPath($params['app']);
324:         $params['app'] = basename($params['app']);
325:         $params['working'] = rtrim($params['root'], '/');
326:         if (!$isWin || !preg_match('/^[A-Z]:$/i', $params['app'])) {
327:             $params['working'] .= '/' . $params['app'];
328:         }
329: 
330:         if ($isWindowsAppPath || !empty($isWin)) {
331:             $params = str_replace('/', '\\', $params);
332:         }
333: 
334:         $this->params = $params + $this->params;
335:     }
336: 
337: 338: 339: 340: 341: 342: 
343:     protected function _isAbsolutePath($path) {
344:         return $path[0] === '/' || $this->_isWindowsPath($path);
345:     }
346: 
347: 348: 349: 350: 351: 352: 
353:     protected function _isWindowsPath($path) {
354:         return preg_match('/([a-z])(:)/i', $path) == 1;
355:     }
356: 
357: 358: 359: 360: 361: 362: 
363:     protected function _parsePaths($args) {
364:         $parsed = array();
365:         $keys = array('-working', '--working', '-app', '--app', '-root', '--root', '-webroot', '--webroot');
366:         $args = (array)$args;
367:         foreach ($keys as $key) {
368:             while (($index = array_search($key, $args)) !== false) {
369:                 $keyname = str_replace('-', '', $key);
370:                 $valueIndex = $index + 1;
371:                 $parsed[$keyname] = $args[$valueIndex];
372:                 array_splice($args, $index, 2);
373:             }
374:         }
375:         $this->args = $args;
376:         $this->params = $parsed;
377:     }
378: 
379: 380: 381: 382: 383: 
384:     public function shiftArgs() {
385:         return array_shift($this->args);
386:     }
387: 
388: 389: 390: 391: 392: 
393:     public function help() {
394:         $this->args = array_merge(array('command_list'), $this->args);
395:         $this->dispatch();
396:     }
397: 
398: 399: 400: 401: 402: 403: 
404:     protected function _stop($status = 0) {
405:         exit($status);
406:     }
407: 
408: }
409: