cake.php

Go to the documentation of this file.
00001 #!/usr/bin/php -q
00002 <?php
00003 /* SVN FILE: $Id: cake.php 8257 2009-07-27 16:57:58Z gwoo $ */
00004 /**
00005  * Command-line code generation utility to automate programmer chores.
00006  *
00007  * Shell dispatcher class
00008  *
00009  * PHP versions 4 and 5
00010  *
00011  * CakePHP(tm) :  Rapid Development Framework (http://www.cakephp.org)
00012  * Copyright 2005-2008, Cake Software Foundation, Inc.
00013  *
00014  * Licensed under The MIT License
00015  * Redistributions of files must retain the above copyright notice.
00016  *
00017  * @filesource
00018  * @copyright     Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
00019  * @link          http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00020  * @package       cake
00021  * @subpackage    cake.cake.console
00022  * @since         CakePHP(tm) v 1.2.0.5012
00023  * @version       $Revision: 8257 $
00024  * @modifiedby    $LastChangedBy: gwoo $
00025  * @lastmodified  $Date: 2009-07-27 12:57:58 -0400 (Mon, 27 Jul 2009) $
00026  * @license       http://www.opensource.org/licenses/mit-license.php The MIT License
00027  */
00028 /**
00029  * Shell dispatcher
00030  *
00031  * @package       cake
00032  * @subpackage    cake.cake.console
00033  */
00034 class ShellDispatcher {
00035 /**
00036  * Standard input stream.
00037  *
00038  * @var filehandle
00039  * @access public
00040  */
00041     var $stdin;
00042 /**
00043  * Standard output stream.
00044  *
00045  * @var filehandle
00046  * @access public
00047  */
00048     var $stdout;
00049 /**
00050  * Standard error stream.
00051  *
00052  * @var filehandle
00053  * @access public
00054  */
00055     var $stderr;
00056 /**
00057  * Contains command switches parsed from the command line.
00058  *
00059  * @var array
00060  * @access public
00061  */
00062     var $params = array();
00063 /**
00064  * Contains arguments parsed from the command line.
00065  *
00066  * @var array
00067  * @access public
00068  */
00069     var $args = array();
00070 /**
00071  * The file name of the shell that was invoked.
00072  *
00073  * @var string
00074  * @access public
00075  */
00076     var $shell = null;
00077 /**
00078  * The class name of the shell that was invoked.
00079  *
00080  * @var string
00081  * @access public
00082  */
00083     var $shellClass = null;
00084 /**
00085  * The command called if public methods are available.
00086  *
00087  * @var string
00088  * @access public
00089  */
00090     var $shellCommand = null;
00091 /**
00092  * The path locations of shells.
00093  *
00094  * @var array
00095  * @access public
00096  */
00097     var $shellPaths = array();
00098 /**
00099  * The path to the current shell location.
00100  *
00101  * @var string
00102  * @access public
00103  */
00104     var $shellPath = null;
00105 /**
00106  * The name of the shell in camelized.
00107  *
00108  * @var string
00109  * @access public
00110  */
00111     var $shellName = null;
00112 /**
00113  * Constructs this ShellDispatcher instance.
00114  *
00115  * @param array $args the argv.
00116  */
00117     function ShellDispatcher($args = array()) {
00118         $this->__construct($args);
00119     }
00120 /**
00121  * Constructor
00122  *
00123  * @param array $args the argv.
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  * Defines core configuration.
00135  *
00136  * @access private
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  * Defines current working environment.
00159  *
00160  * @access protected
00161  */
00162     function _initEnvironment() {
00163         $this->stdin = fopen('php://stdin', 'r');
00164         $this->stdout = fopen('php://stdout', 'w');
00165         $this->stderr = fopen('php://stderr', 'w');
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://manual.cakephp.org/)');
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  * Builds the shell paths.
00195  *
00196  * @access private
00197  * @return void
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  * Initializes the environment and loads the Cake core.
00229  *
00230  * @return boolean Success.
00231  * @access private
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  * Dispatches a CLI request
00271  *
00272  * @access public
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  * Prompts the user for input, and returns it.
00387  *
00388  * @param string $prompt Prompt text.
00389  * @param mixed $options Array or string of options.
00390  * @param string $default Default input value.
00391  * @return Either the default value, or the user-provided input.
00392  * @access public
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  * Outputs to the stdout filehandle.
00420  *
00421  * @param string $string String to output.
00422  * @param boolean $newline If true, the outputs gets an added newline.
00423  * @access public
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  * Outputs to the stderr filehandle.
00434  *
00435  * @param string $string Error text to output.
00436  * @access public
00437  */
00438     function stderr($string) {
00439         fwrite($this->stderr, 'Error: '. $string);
00440     }
00441 /**
00442  * Parses command line options
00443  *
00444  * @param array $params Parameters to parse
00445  * @access public
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  * Helper for recursively paraing params
00486  *
00487  * @return array params
00488  * @access private
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  * Removes first argument and shifts other arguments up
00517  *
00518  * @return boolean False if there are no arguments
00519  * @access public
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  * Shows console help
00531  *
00532  * @access public
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  * Stop execution of the current script
00577  *
00578  * @param $status see http://php.net/exit for values
00579  * @return void
00580  * @access protected
00581  */
00582     function _stop($status = 0) {
00583         exit($status);
00584     }
00585 }
00586 if (!defined('DISABLE_AUTO_DISPATCH')) {
00587     $dispatcher = new ShellDispatcher($argv);
00588 }
00589 ?>

Generated on Sun Nov 22 00:30:51 2009 for CakePHP 1.2.x.x (v1.2.4.8284) by doxygen 1.4.7