1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19:
20: 21: 22: 23: 24:
25: class ShellDispatcher {
26:
27: 28: 29: 30: 31:
32: public $params = array();
33:
34: 35: 36: 37: 38:
39: public $args = array();
40:
41: 42: 43: 44: 45: 46: 47: 48: 49:
50: public function __construct($args = array(), $bootstrap = true) {
51: set_time_limit(0);
52: $this->parseParams($args);
53:
54: if ($bootstrap) {
55: $this->_initConstants();
56: $this->_initEnvironment();
57: }
58: }
59:
60: 61: 62: 63: 64: 65:
66: public static function run($argv) {
67: $dispatcher = new ShellDispatcher($argv);
68: return $dispatcher->_stop($dispatcher->dispatch() === false ? 1 : 0);
69: }
70:
71: 72: 73: 74: 75:
76: protected function _initConstants() {
77: if (function_exists('ini_set')) {
78: ini_set('html_errors', false);
79: ini_set('implicit_flush', true);
80: ini_set('max_execution_time', 0);
81: }
82:
83: if (!defined('CAKE_CORE_INCLUDE_PATH')) {
84: define('DS', DIRECTORY_SEPARATOR);
85: define('CAKE_CORE_INCLUDE_PATH', dirname(dirname(dirname(__FILE__))));
86: define('CAKEPHP_SHELL', true);
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: 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: define('FULL_BASE_URL', 'http://localhost');
149: }
150:
151: return true;
152: }
153:
154: 155: 156: 157: 158: 159: 160: 161:
162: public function setErrorHandlers() {
163: App::uses('ConsoleErrorHandler', 'Console');
164: $error = Configure::read('Error');
165: $exception = Configure::read('Exception');
166:
167: $errorHandler = new ConsoleErrorHandler();
168: if (empty($error['consoleHandler'])) {
169: $error['consoleHandler'] = array($errorHandler, 'handleError');
170: Configure::write('Error', $error);
171: }
172: if (empty($exception['consoleHandler'])) {
173: $exception['consoleHandler'] = array($errorHandler, 'handleException');
174: Configure::write('Exception', $exception);
175: }
176: set_exception_handler($exception['consoleHandler']);
177: set_error_handler($error['consoleHandler'], Configure::read('Error.level'));
178: }
179:
180: 181: 182: 183: 184: 185:
186: public function dispatch() {
187: $shell = $this->shiftArgs();
188:
189: if (!$shell) {
190: $this->help();
191: return false;
192: }
193: if (in_array($shell, array('help', '--help', '-h'))) {
194: $this->help();
195: return true;
196: }
197:
198: $Shell = $this->_getShell($shell);
199:
200: $command = null;
201: if (isset($this->args[0])) {
202: $command = $this->args[0];
203: }
204:
205: if ($Shell instanceof Shell) {
206: $Shell->initialize();
207: $Shell->loadTasks();
208: return $Shell->runCommand($command, $this->args);
209: }
210: $methods = array_diff(get_class_methods($Shell), get_class_methods('Shell'));
211: $added = in_array($command, $methods);
212: $private = $command[0] === '_' && method_exists($Shell, $command);
213:
214: if (!$private) {
215: if ($added) {
216: $this->shiftArgs();
217: $Shell->startup();
218: return $Shell->{$command}();
219: }
220: if (method_exists($Shell, 'main')) {
221: $Shell->startup();
222: return $Shell->main();
223: }
224: }
225:
226: throw new MissingShellMethodException(array('shell' => $shell, 'method' => $command));
227: }
228:
229: 230: 231: 232: 233: 234: 235: 236: 237:
238: protected function _getShell($shell) {
239: list($plugin, $shell) = pluginSplit($shell, true);
240:
241: $plugin = Inflector::camelize($plugin);
242: $class = Inflector::camelize($shell) . 'Shell';
243:
244: App::uses('Shell', 'Console');
245: App::uses('AppShell', 'Console/Command');
246: App::uses($class, $plugin . 'Console/Command');
247:
248: if (!class_exists($class)) {
249: throw new MissingShellException(array(
250: 'class' => $class
251: ));
252: }
253: $Shell = new $class();
254: $Shell->plugin = trim($plugin, '.');
255: return $Shell;
256: }
257:
258: 259: 260: 261: 262: 263:
264: public function parseParams($args) {
265: $this->_parsePaths($args);
266:
267: $defaults = array(
268: 'app' => 'app',
269: 'root' => dirname(dirname(dirname(dirname(__FILE__)))),
270: 'working' => null,
271: 'webroot' => 'webroot'
272: );
273: $params = array_merge($defaults, array_intersect_key($this->params, $defaults));
274: $isWin = false;
275: foreach ($defaults as $default => $value) {
276: if (strpos($params[$default], '\\') !== false) {
277: $isWin = true;
278: break;
279: }
280: }
281: $params = str_replace('\\', '/', $params);
282:
283: if (isset($params['working'])) {
284: $params['working'] = trim($params['working']);
285: }
286:
287: if (!empty($params['working']) && (!isset($this->args[0]) || isset($this->args[0]) && $this->args[0][0] !== '.')) {
288: if ($params['working'][0] === '.') {
289: $params['working'] = realpath($params['working']);
290: }
291: if (empty($this->params['app']) && $params['working'] != $params['root']) {
292: $params['root'] = dirname($params['working']);
293: $params['app'] = basename($params['working']);
294: } else {
295: $params['root'] = $params['working'];
296: }
297: }
298:
299: if ($params['app'][0] === '/' || preg_match('/([a-z])(:)/i', $params['app'], $matches)) {
300: $params['root'] = dirname($params['app']);
301: } elseif (strpos($params['app'], '/')) {
302: $params['root'] .= '/' . dirname($params['app']);
303: }
304:
305: $params['app'] = basename($params['app']);
306: $params['working'] = rtrim($params['root'], '/');
307: if (!$isWin || !preg_match('/^[A-Z]:$/i', $params['app'])) {
308: $params['working'] .= '/' . $params['app'];
309: }
310:
311: if (!empty($matches[0]) || !empty($isWin)) {
312: $params = str_replace('/', '\\', $params);
313: }
314:
315: $this->params = array_merge($this->params, $params);
316: }
317:
318: 319: 320: 321: 322: 323:
324: protected function _parsePaths($args) {
325: $parsed = array();
326: $keys = array('-working', '--working', '-app', '--app', '-root', '--root');
327: foreach ($keys as $key) {
328: while (($index = array_search($key, $args)) !== false) {
329: $keyname = str_replace('-', '', $key);
330: $valueIndex = $index + 1;
331: $parsed[$keyname] = $args[$valueIndex];
332: array_splice($args, $index, 2);
333: }
334: }
335: $this->args = $args;
336: $this->params = $parsed;
337: }
338:
339: 340: 341: 342: 343:
344: public function shiftArgs() {
345: return array_shift($this->args);
346: }
347:
348: 349: 350: 351: 352:
353: public function help() {
354: $this->args = array_merge(array('command_list'), $this->args);
355: $this->dispatch();
356: }
357:
358: 359: 360: 361: 362: 363:
364: protected function _stop($status = 0) {
365: exit($status);
366: }
367:
368: }
369: