00001 #!/usr/bin/php -q
00002 <?php
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class ShellDispatcher {
00035
00036
00037
00038
00039
00040
00041 var $stdin;
00042
00043
00044
00045
00046
00047
00048 var $stdout;
00049
00050
00051
00052
00053
00054
00055 var $stderr;
00056
00057
00058
00059
00060
00061
00062 var $params = array();
00063
00064
00065
00066
00067
00068
00069 var $args = array();
00070
00071
00072
00073
00074
00075
00076 var $shell = null;
00077
00078
00079
00080
00081
00082
00083 var $shellClass = null;
00084
00085
00086
00087
00088
00089
00090 var $shellCommand = null;
00091
00092
00093
00094
00095
00096
00097 var $shellPaths = array();
00098
00099
00100
00101
00102
00103
00104 var $shellPath = null;
00105
00106
00107
00108
00109
00110
00111 var $shellName = null;
00112
00113
00114
00115
00116
00117 function ShellDispatcher($args = array()) {
00118 $this->__construct($args);
00119 }
00120
00121
00122
00123
00124
00125 function __construct($args = array()) {
00126 set_time_limit(0);
00127 $this->__initConstants();
00128 $this->parseParams($args);
00129 $this->_initEnvironment();
00130 $this->__buildPaths();
00131 $this->_stop($this->dispatch());
00132 }
00133
00134
00135
00136
00137
00138 function __initConstants() {
00139 if (function_exists('ini_set')) {
00140 ini_set('display_errors', '1');
00141 ini_set('error_reporting', E_ALL);
00142 ini_set('html_errors', false);
00143 ini_set('implicit_flush', true);
00144 ini_set('max_execution_time', 0);
00145 }
00146
00147 if (!defined('CAKE_CORE_INCLUDE_PATH')) {
00148 define('PHP5', (PHP_VERSION >= 5));
00149 define('DS', DIRECTORY_SEPARATOR);
00150 define('CAKE_CORE_INCLUDE_PATH', dirname(dirname(dirname(__FILE__))));
00151 define('CORE_PATH', CAKE_CORE_INCLUDE_PATH . DS);
00152 define('DISABLE_DEFAULT_ERROR_HANDLING', false);
00153 define('CAKEPHP_SHELL', true);
00154 }
00155 require_once(CORE_PATH . 'cake' . DS . 'basics.php');
00156 }
00157
00158
00159
00160
00161
00162 function _initEnvironment() {
00163 $this->stdin = fopen('php:
00164 $this->stdout = fopen('php:
00165 $this->stderr = fopen('php:
00166
00167 if (!$this->__bootstrap()) {
00168 $this->stderr("\nCakePHP Console: ");
00169 $this->stderr("\nUnable to load Cake core:");
00170 $this->stderr("\tMake sure " . DS . 'cake' . DS . 'libs exists in ' . CAKE_CORE_INCLUDE_PATH);
00171 $this->_stop();
00172 }
00173
00174 if (!isset($this->args[0]) || !isset($this->params['working'])) {
00175 $this->stderr("\nCakePHP Console: ");
00176 $this->stderr('This file has been loaded incorrectly and cannot continue.');
00177 $this->stderr('Please make sure that ' . DIRECTORY_SEPARATOR . 'cake' . DIRECTORY_SEPARATOR . 'console is in your system path,');
00178 $this->stderr('and check the manual for the correct usage of this command.');
00179 $this->stderr('(http:
00180 $this->_stop();
00181 }
00182
00183 if (basename(__FILE__) != basename($this->args[0])) {
00184 $this->stderr("\nCakePHP Console: ");
00185 $this->stderr('Warning: the dispatcher may have been loaded incorrectly, which could lead to unexpected results...');
00186 if ($this->getInput('Continue anyway?', array('y', 'n'), 'y') == 'n') {
00187 $this->_stop();
00188 }
00189 }
00190
00191 $this->shiftArgs();
00192 }
00193
00194
00195
00196
00197
00198
00199 function __buildPaths() {
00200 $paths = array();
00201 $pluginPaths = Configure::read('pluginPaths');
00202 if (!class_exists('Folder')) {
00203 require LIBS . 'folder.php';
00204 }
00205
00206 foreach ($pluginPaths as $pluginPath) {
00207 $Folder =& new Folder($pluginPath);
00208 list($plugins,) = $Folder->read(false, true);
00209 foreach ((array)$plugins as $plugin) {
00210 $path = $pluginPath . Inflector::underscore($plugin) . DS . 'vendors' . DS . 'shells' . DS;
00211 if (file_exists($path)) {
00212 $paths[] = $path;
00213 }
00214 }
00215 }
00216
00217 $vendorPaths = array_values(Configure::read('vendorPaths'));
00218 foreach ($vendorPaths as $vendorPath) {
00219 $path = rtrim($vendorPath, DS) . DS . 'shells' . DS;
00220 if (file_exists($path)) {
00221 $paths[] = $path;
00222 }
00223 }
00224
00225 $this->shellPaths = array_values(array_unique(array_merge($paths, Configure::read('shellPaths'))));
00226 }
00227
00228
00229
00230
00231
00232
00233 function __bootstrap() {
00234
00235 define('ROOT', $this->params['root']);
00236 define('APP_DIR', $this->params['app']);
00237 define('APP_PATH', $this->params['working'] . DS);
00238 define('WWW_ROOT', APP_PATH . $this->params['webroot'] . DS);
00239
00240 $includes = array(
00241 CORE_PATH . 'cake' . DS . 'config' . DS . 'paths.php',
00242 CORE_PATH . 'cake' . DS . 'libs' . DS . 'object.php',
00243 CORE_PATH . 'cake' . DS . 'libs' . DS . 'inflector.php',
00244 CORE_PATH . 'cake' . DS . 'libs' . DS . 'configure.php',
00245 CORE_PATH . 'cake' . DS . 'libs' . DS . 'file.php',
00246 CORE_PATH . 'cake' . DS . 'libs' . DS . 'cache.php',
00247 CORE_PATH . 'cake' . DS . 'libs' . DS . 'string.php',
00248 CORE_PATH . 'cake' . DS . 'libs' . DS . 'class_registry.php',
00249 CORE_PATH . 'cake' . DS . 'console' . DS . 'error.php'
00250 );
00251
00252 foreach ($includes as $inc) {
00253 if (!require($inc)) {
00254 $this->stderr("Failed to load Cake core file {$inc}");
00255 return false;
00256 }
00257 }
00258
00259 Configure::getInstance(file_exists(CONFIGS . 'bootstrap.php'));
00260
00261 if (!file_exists(APP_PATH . 'config' . DS . 'core.php')) {
00262 include_once CORE_PATH . 'cake' . DS . 'console' . DS . 'libs' . DS . 'templates' . DS . 'skel' . DS . 'config' . DS . 'core.php';
00263 Configure::buildPaths(array());
00264 }
00265
00266 Configure::write('debug', 1);
00267 return true;
00268 }
00269
00270
00271
00272
00273
00274 function dispatch() {
00275 if (isset($this->args[0])) {
00276 $plugin = null;
00277 $shell = $this->args[0];
00278 if (strpos($shell, '.') !== false) {
00279 list($plugin, $shell) = explode('.', $this->args[0]);
00280 }
00281
00282 $this->shell = $shell;
00283 $this->shiftArgs();
00284 $this->shellName = Inflector::camelize($this->shell);
00285 $this->shellClass = $this->shellName . 'Shell';
00286
00287 if ($this->shell === 'help') {
00288 $this->help();
00289 } else {
00290 $loaded = false;
00291 foreach ($this->shellPaths as $path) {
00292 $this->shellPath = $path . $this->shell . '.php';
00293
00294 $isPlugin = ($plugin && strpos($path, DS . $plugin . DS . 'vendors' . DS . 'shells' . DS) !== false);
00295 if (($isPlugin && file_exists($this->shellPath)) || (!$plugin && file_exists($this->shellPath))) {
00296 $loaded = true;
00297 break;
00298 }
00299 }
00300
00301 if ($loaded) {
00302 if (!class_exists('Shell')) {
00303 require CONSOLE_LIBS . 'shell.php';
00304 }
00305 require $this->shellPath;
00306 if (class_exists($this->shellClass)) {
00307 $command = null;
00308 if (isset($this->args[0])) {
00309 $command = $this->args[0];
00310 }
00311 $this->shellCommand = Inflector::variable($command);
00312 $shell = new $this->shellClass($this);
00313
00314 if (strtolower(get_parent_class($shell)) == 'shell') {
00315 $shell->initialize();
00316 $shell->loadTasks();
00317
00318 foreach ($shell->taskNames as $task) {
00319 if (strtolower(get_parent_class($shell)) == 'shell') {
00320 $shell->{$task}->initialize();
00321 $shell->{$task}->loadTasks();
00322 }
00323 }
00324
00325 $task = Inflector::camelize($command);
00326 if (in_array($task, $shell->taskNames)) {
00327 $this->shiftArgs();
00328 $shell->{$task}->startup();
00329 if (isset($this->args[0]) && $this->args[0] == 'help') {
00330 if (method_exists($shell->{$task}, 'help')) {
00331 $shell->{$task}->help();
00332 $this->_stop();
00333 } else {
00334 $this->help();
00335 }
00336 }
00337 return $shell->{$task}->execute();
00338 }
00339 }
00340
00341 $classMethods = get_class_methods($shell);
00342
00343 $privateMethod = $missingCommand = false;
00344 if ((in_array($command, $classMethods) || in_array(strtolower($command), $classMethods)) && strpos($command, '_', 0) === 0) {
00345 $privateMethod = true;
00346 }
00347
00348 if (!in_array($command, $classMethods) && !in_array(strtolower($command), $classMethods)) {
00349 $missingCommand = true;
00350 }
00351
00352 $protectedCommands = array(
00353 'initialize','in','out','err','hr',
00354 'createfile', 'isdir','copydir','object','tostring',
00355 'requestaction','log','cakeerror', 'shelldispatcher',
00356 '__initconstants','__initenvironment','__construct',
00357 'dispatch','__bootstrap','getinput','stdout','stderr','parseparams','shiftargs'
00358 );
00359
00360 if (in_array(strtolower($command), $protectedCommands)) {
00361 $missingCommand = true;
00362 }
00363
00364 if ($missingCommand && method_exists($shell, 'main')) {
00365 $shell->startup();
00366 return $shell->main();
00367 } elseif (!$privateMethod && method_exists($shell, $command)) {
00368 $this->shiftArgs();
00369 $shell->startup();
00370 return $shell->{$command}();
00371 } else {
00372 $this->stderr("Unknown {$this->shellName} command '$command'.\nFor usage, try 'cake {$this->shell} help'.\n\n");
00373 }
00374 } else {
00375 $this->stderr('Class '.$this->shellClass.' could not be loaded');
00376 }
00377 } else {
00378 $this->help();
00379 }
00380 }
00381 } else {
00382 $this->help();
00383 }
00384 }
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394 function getInput($prompt, $options = null, $default = null) {
00395 if (!is_array($options)) {
00396 $printOptions = '';
00397 } else {
00398 $printOptions = '(' . implode('/', $options) . ')';
00399 }
00400
00401 if ($default == null) {
00402 $this->stdout($prompt . " $printOptions \n" . '> ', false);
00403 } else {
00404 $this->stdout($prompt . " $printOptions \n" . "[$default] > ", false);
00405 }
00406 $result = fgets($this->stdin);
00407
00408 if ($result === false) {
00409 exit;
00410 }
00411 $result = trim($result);
00412
00413 if ($default != null && empty($result)) {
00414 return $default;
00415 }
00416 return $result;
00417 }
00418
00419
00420
00421
00422
00423
00424
00425 function stdout($string, $newline = true) {
00426 if ($newline) {
00427 fwrite($this->stdout, $string . "\n");
00428 } else {
00429 fwrite($this->stdout, $string);
00430 }
00431 }
00432
00433
00434
00435
00436
00437
00438 function stderr($string) {
00439 fwrite($this->stderr, 'Error: '. $string);
00440 }
00441
00442
00443
00444
00445
00446
00447 function parseParams($params) {
00448 $this->__parseParams($params);
00449 $defaults = array('app' => 'app', 'root' => dirname(dirname(dirname(__FILE__))), 'working' => null, 'webroot' => 'webroot');
00450 $params = array_merge($defaults, array_intersect_key($this->params, $defaults));
00451 $isWin = false;
00452 foreach ($defaults as $default => $value) {
00453 if (strpos($params[$default], '\\') !== false) {
00454 $isWin = true;
00455 break;
00456 }
00457 }
00458 $params = str_replace('\\', '/', $params);
00459
00460 if (!empty($params['working']) && (!isset($this->args[0]) || isset($this->args[0]) && $this->args[0]{0} !== '.')) {
00461 if (empty($this->params['app']) && $params['working'] != $params['root']) {
00462 $params['root'] = dirname($params['working']);
00463 $params['app'] = basename($params['working']);
00464 } else {
00465 $params['root'] = $params['working'];
00466 }
00467 }
00468
00469 if ($params['app'][0] == '/' || preg_match('/([a-z])(:)/i', $params['app'], $matches)) {
00470 $params['root'] = dirname($params['app']);
00471 } elseif (strpos($params['app'], '/')) {
00472 $params['root'] .= '/' . dirname($params['app']);
00473 }
00474
00475 $params['app'] = basename($params['app']);
00476 $params['working'] = rtrim($params['root'], '/') . '/' . $params['app'];
00477
00478 if (!empty($matches[0]) || !empty($isWin)) {
00479 $params = str_replace('/', '\\', $params);
00480 }
00481
00482 $this->params = array_merge($this->params, $params);
00483 }
00484
00485
00486
00487
00488
00489
00490 function __parseParams($params) {
00491 $count = count($params);
00492 for ($i = 0; $i < $count; $i++) {
00493 if (isset($params[$i])) {
00494 if ($params[$i]{0} === '-') {
00495 $key = substr($params[$i], 1);
00496 $this->params[$key] = true;
00497 unset($params[$i]);
00498 if (isset($params[++$i])) {
00499 if ($params[$i]{0} !== '-') {
00500 $this->params[$key] = str_replace('"', '', $params[$i]);
00501 unset($params[$i]);
00502 } else {
00503 $i--;
00504 $this->__parseParams($params);
00505 }
00506 }
00507 } else {
00508 $this->args[] = $params[$i];
00509 unset($params[$i]);
00510 }
00511
00512 }
00513 }
00514 }
00515
00516
00517
00518
00519
00520
00521 function shiftArgs() {
00522 if (empty($this->args)) {
00523 return false;
00524 }
00525 unset($this->args[0]);
00526 $this->args = array_values($this->args);
00527 return true;
00528 }
00529
00530
00531
00532
00533
00534 function help() {
00535 $this->stdout("\nWelcome to CakePHP v" . Configure::version() . " Console");
00536 $this->stdout("---------------------------------------------------------------");
00537 $this->stdout("Current Paths:");
00538 $this->stdout(" -app: ". $this->params['app']);
00539 $this->stdout(" -working: " . rtrim($this->params['working'], DS));
00540 $this->stdout(" -root: " . rtrim($this->params['root'], DS));
00541 $this->stdout(" -core: " . rtrim(CORE_PATH, DS));
00542 $this->stdout("");
00543 $this->stdout("Changing Paths:");
00544 $this->stdout("your working path should be the same as your application path");
00545 $this->stdout("to change your path use the '-app' param.");
00546 $this->stdout("Example: -app relative/path/to/myapp or -app /absolute/path/to/myapp");
00547
00548 $this->stdout("\nAvailable Shells:");
00549 $_shells = array();
00550
00551 foreach ($this->shellPaths as $path) {
00552 if (is_dir($path)) {
00553 $shells = Configure::listObjects('file', $path);
00554 $path = str_replace(CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS, 'CORE' . DS, $path);
00555 $path = str_replace(APP, 'APP' . DS, $path);
00556 $path = str_replace(ROOT, 'ROOT', $path);
00557 $path = rtrim($path, DS);
00558 $this->stdout("\n " . $path . ":");
00559 if (empty($shells)) {
00560 $this->stdout("\t - none");
00561 } else {
00562 sort($shells);
00563 foreach ($shells as $shell) {
00564 if ($shell !== 'shell.php') {
00565 $this->stdout("\t " . str_replace('.php', '', $shell));
00566 }
00567 }
00568 }
00569 }
00570 }
00571 $this->stdout("\nTo run a command, type 'cake shell_name [args]'");
00572 $this->stdout("To get help on a specific command, type 'cake shell_name help'");
00573 $this->_stop();
00574 }
00575
00576
00577
00578
00579
00580
00581
00582 function _stop($status = 0) {
00583 exit($status);
00584 }
00585 }
00586 if (!defined('DISABLE_AUTO_DISPATCH')) {
00587 $dispatcher = new ShellDispatcher($argv);
00588 }
00589 ?>