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