2008-05-30 11:40:08 +00:00
< ? php
/* SVN FILE: $Id$ */
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Base class for Shells
*
* Long description for file
*
* PHP versions 4 and 5
*
2008-10-30 17:30:26 +00:00
* CakePHP ( tm ) : Rapid Development Framework ( http :// www . cakephp . org )
* Copyright 2005 - 2008 , Cake Software Foundation , Inc . ( http :// www . cakefoundation . org )
2008-05-30 11:40:08 +00:00
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice .
*
* @ filesource
2008-10-30 17:30:26 +00:00
* @ copyright Copyright 2005 - 2008 , Cake Software Foundation , Inc . ( http :// www . cakefoundation . org )
* @ link http :// www . cakefoundation . org / projects / info / cakephp CakePHP ( tm ) Project
* @ package cake
* @ subpackage cake . cake . console . libs
* @ since CakePHP ( tm ) v 1.2 . 0.5012
* @ version $Revision $
* @ modifiedby $LastChangedBy $
* @ lastmodified $Date $
* @ license http :// www . opensource . org / licenses / mit - license . php The MIT License
2008-05-30 11:40:08 +00:00
*/
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Base class for command - line utilities for automating programmer chores .
*
2008-10-30 17:30:26 +00:00
* @ package cake
* @ subpackage cake . cake . console . libs
2008-05-30 11:40:08 +00:00
*/
class Shell extends Object {
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* An instance of the ShellDispatcher object that loaded this script
*
2009-03-19 21:10:13 +00:00
* @ var ShellDispatcher
2008-05-30 11:40:08 +00:00
* @ access public
*/
var $Dispatch = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* If true , the script will ask for permission to perform actions .
*
* @ var boolean
* @ access public
*/
var $interactive = true ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Holds the DATABASE_CONFIG object for the app . Null if database . php could not be found ,
* or the app does not exist .
*
2009-03-19 21:10:13 +00:00
* @ var DATABASE_CONFIG
2008-05-30 11:40:08 +00:00
* @ access public
*/
var $DbConfig = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Contains command switches parsed from the command line .
*
* @ var array
* @ access public
*/
var $params = array ();
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Contains arguments parsed from the command line .
*
* @ var array
* @ access public
*/
var $args = array ();
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* The file name of the shell that was invoked .
*
* @ var string
* @ access public
*/
var $shell = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* The class name of the shell that was invoked .
*
* @ var string
* @ access public
*/
var $className = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* The command called if public methods are available .
*
* @ var string
* @ access public
*/
var $command = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* The name of the shell in camelized .
*
* @ var string
* @ access public
*/
var $name = null ;
2009-07-24 19:18:37 +00:00
2008-10-29 19:25:49 +00:00
/**
* An alias for the shell
*
* @ var string
* @ access public
*/
var $alias = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Contains tasks to load and instantiate
*
* @ var array
* @ access public
*/
var $tasks = array ();
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Contains the loaded tasks
*
* @ var array
* @ access public
*/
var $taskNames = array ();
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Contains models to load and instantiate
*
* @ var array
* @ access public
*/
var $uses = array ();
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Constructs this Shell instance .
*
*/
function __construct ( & $dispatch ) {
2008-10-29 19:25:49 +00:00
$vars = array ( 'params' , 'args' , 'shell' , 'shellCommand' => 'command' );
2008-05-30 11:40:08 +00:00
foreach ( $vars as $key => $var ) {
if ( is_string ( $key )) {
$this -> { $var } =& $dispatch -> { $key };
} else {
$this -> { $var } =& $dispatch -> { $var };
}
}
if ( $this -> name == null ) {
2008-10-29 19:25:49 +00:00
$this -> name = get_class ( $this );
2008-05-30 11:40:08 +00:00
}
2008-10-29 19:25:49 +00:00
if ( $this -> alias == null ) {
$this -> alias = $this -> name ;
}
ClassRegistry :: addObject ( $this -> name , $this );
ClassRegistry :: map ( $this -> name , $this -> alias );
2008-05-30 11:40:08 +00:00
if ( ! PHP5 && isset ( $this -> args [ 0 ])) {
2008-10-29 19:25:49 +00:00
if ( strpos ( $this -> name , low ( Inflector :: camelize ( $this -> args [ 0 ]))) !== false ) {
2008-05-30 11:40:08 +00:00
$dispatch -> shiftArgs ();
}
if ( low ( $this -> command ) == low ( Inflector :: variable ( $this -> args [ 0 ])) && method_exists ( $this , $this -> command )) {
$dispatch -> shiftArgs ();
}
}
$this -> Dispatch =& $dispatch ;
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Initializes the Shell
* acts as constructor for subclasses
* allows configuration of tasks prior to shell execution
*
* @ access public
*/
function initialize () {
$this -> _loadModels ();
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Starts up the the Shell
* allows for checking and configuring prior to command or main execution
* can be overriden in subclasses
*
* @ access public
*/
function startup () {
$this -> _welcome ();
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Displays a header for the shell
*
* @ access protected
*/
function _welcome () {
2009-07-24 20:01:55 +00:00
$this -> Dispatcher -> clear ();
2008-06-03 05:11:04 +00:00
$this -> out ( " \n Welcome to CakePHP v " . Configure :: version () . " Console " );
$this -> out ( " --------------------------------------------------------------- " );
2008-05-30 11:40:08 +00:00
$this -> out ( 'App : ' . $this -> params [ 'app' ]);
$this -> out ( 'Path: ' . $this -> params [ 'working' ]);
$this -> hr ();
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Loads database file and constructs DATABASE_CONFIG class
* makes $this -> DbConfig available to subclasses
*
* @ return bool
* @ access protected
*/
function _loadDbConfig () {
if ( config ( 'database' ) && class_exists ( 'DATABASE_CONFIG' )) {
$this -> DbConfig =& new DATABASE_CONFIG ();
return true ;
}
$this -> err ( 'Database config could not be loaded' );
$this -> out ( 'Run \'bake\' to create the database configuration' );
return false ;
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* if var $uses = true
* Loads AppModel file and constructs AppModel class
* makes $this -> AppModel available to subclasses
* if var $uses is an array of models will load those models
*
* @ return bool
* @ access protected
*/
function _loadModels () {
if ( $this -> uses === null || $this -> uses === false ) {
return ;
}
if ( $this -> uses === true && App :: import ( 'Model' , 'AppModel' )) {
$this -> AppModel =& new AppModel ( false , false , false );
return true ;
}
if ( $this -> uses !== true && ! empty ( $this -> uses )) {
$uses = is_array ( $this -> uses ) ? $this -> uses : array ( $this -> uses );
2008-10-10 02:14:57 +00:00
$modelClassName = $uses [ 0 ];
if ( strpos ( $uses [ 0 ], '.' ) !== false ) {
list ( $plugin , $modelClassName ) = explode ( '.' , $uses [ 0 ]);
}
$this -> modelClass = $modelClassName ;
2008-05-30 11:40:08 +00:00
foreach ( $uses as $modelClass ) {
2008-10-10 02:14:57 +00:00
$plugin = null ;
if ( strpos ( $modelClass , '.' ) !== false ) {
list ( $plugin , $modelClass ) = explode ( '.' , $modelClass );
$plugin = $plugin . '.' ;
}
2008-05-30 11:40:08 +00:00
if ( PHP5 ) {
2008-10-10 02:14:57 +00:00
$this -> { $modelClass } = ClassRegistry :: init ( $plugin . $modelClass );
2008-05-30 11:40:08 +00:00
} else {
2008-10-10 02:14:57 +00:00
$this -> { $modelClass } =& ClassRegistry :: init ( $plugin . $modelClass );
2008-05-30 11:40:08 +00:00
}
}
return true ;
}
return false ;
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Loads tasks defined in var $tasks
*
* @ return bool
* @ access public
*/
function loadTasks () {
2008-10-29 19:25:49 +00:00
if ( $this -> tasks === null || $this -> tasks === false || $this -> tasks === true || empty ( $this -> tasks )) {
return true ;
2008-05-30 11:40:08 +00:00
}
2008-10-29 19:25:49 +00:00
$tasks = $this -> tasks ;
if ( ! is_array ( $tasks )) {
$tasks = array ( $tasks );
}
2008-05-30 11:40:08 +00:00
2008-10-29 19:25:49 +00:00
foreach ( $tasks as $taskName ) {
$task = Inflector :: underscore ( $taskName );
$taskClass = Inflector :: camelize ( $taskName . 'Task' );
2008-05-30 11:40:08 +00:00
2008-10-29 19:25:49 +00:00
if ( ! class_exists ( $taskClass )) {
foreach ( $this -> Dispatch -> shellPaths as $path ) {
$taskPath = $path . 'tasks' . DS . $task . '.php' ;
if ( file_exists ( $taskPath )) {
require_once $taskPath ;
break ;
2008-05-30 11:40:08 +00:00
}
}
2008-10-29 19:25:49 +00:00
}
if ( ClassRegistry :: isKeySet ( $taskClass )) {
$this -> taskNames [] = $taskName ;
if ( ! PHP5 ) {
$this -> { $taskName } =& ClassRegistry :: getObject ( $taskClass );
2008-05-30 11:40:08 +00:00
} else {
2008-10-29 19:25:49 +00:00
$this -> { $taskName } = ClassRegistry :: getObject ( $taskClass );
2008-05-30 11:40:08 +00:00
}
2008-10-29 19:25:49 +00:00
} else {
$this -> taskNames [] = $taskName ;
if ( ! PHP5 ) {
$this -> { $taskName } =& new $taskClass ( $this -> Dispatch );
} else {
$this -> { $taskName } = new $taskClass ( $this -> Dispatch );
2008-05-30 11:40:08 +00:00
}
}
2008-10-29 19:25:49 +00:00
if ( ! isset ( $this -> { $taskName })) {
$this -> err ( " Task ' " . $taskName . " ' could not be loaded " );
$this -> _stop ();
}
2008-05-30 11:40:08 +00:00
}
2008-10-29 19:25:49 +00:00
return true ;
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Prompts the user for input , and returns it .
*
* @ param string $prompt Prompt text .
* @ param mixed $options Array or string of options .
* @ param string $default Default input value .
* @ return Either the default value , or the user - provided input .
* @ access public
*/
function in ( $prompt , $options = null , $default = null ) {
if ( ! $this -> interactive ) {
return $default ;
}
$in = $this -> Dispatch -> getInput ( $prompt , $options , $default );
if ( $options && is_string ( $options )) {
if ( strpos ( $options , ',' )) {
$options = explode ( ',' , $options );
} elseif ( strpos ( $options , '/' )) {
$options = explode ( '/' , $options );
} else {
$options = array ( $options );
}
}
if ( is_array ( $options )) {
while ( $in == '' || ( $in && ( ! in_array ( low ( $in ), $options ) && ! in_array ( up ( $in ), $options )) && ! in_array ( $in , $options ))) {
2008-06-20 20:17:23 +00:00
$in = $this -> Dispatch -> getInput ( $prompt , $options , $default );
2008-05-30 11:40:08 +00:00
}
}
if ( $in ) {
return $in ;
}
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Outputs to the stdout filehandle .
*
* @ param string $string String to output .
* @ param boolean $newline If true , the outputs gets an added newline .
* @ access public
*/
function out ( $string , $newline = true ) {
if ( is_array ( $string )) {
$str = '' ;
2008-10-23 00:10:44 +00:00
foreach ( $string as $message ) {
2008-05-30 11:40:08 +00:00
$str .= $message . " \n " ;
}
$string = $str ;
}
return $this -> Dispatch -> stdout ( $string , $newline );
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Outputs to the stderr filehandle .
*
* @ param string $string Error text to output .
* @ access public
*/
function err ( $string ) {
if ( is_array ( $string )) {
$str = '' ;
2008-10-23 00:10:44 +00:00
foreach ( $string as $message ) {
2008-05-30 11:40:08 +00:00
$str .= $message . " \n " ;
}
$string = $str ;
}
return $this -> Dispatch -> stderr ( $string . " \n " );
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Outputs a series of minus characters to the standard output , acts as a visual separator .
*
* @ param boolean $newline If true , the outputs gets an added newline .
* @ access public
*/
function hr ( $newline = false ) {
if ( $newline ) {
$this -> out ( " \n " );
}
$this -> out ( '---------------------------------------------------------------' );
if ( $newline ) {
$this -> out ( " \n " );
}
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Displays a formatted error message and exits the application
*
* @ param string $title Title of the error message
* @ param string $msg Error message
* @ access public
*/
function error ( $title , $msg ) {
$out = " $title\n " ;
$out .= " $msg\n " ;
$out .= " \n " ;
$this -> err ( $out );
2008-06-04 19:04:58 +00:00
$this -> _stop ();
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Will check the number args matches otherwise throw an error
*
* @ param integer $expectedNum Expected number of paramters
* @ param string $command Command
* @ access protected
*/
function _checkArgs ( $expectedNum , $command = null ) {
if ( ! $command ) {
$command = $this -> command ;
}
if ( count ( $this -> args ) < $expectedNum ) {
$this -> error ( " Wrong number of parameters: " . count ( $this -> args ), " Expected: { $expectedNum } \n Please type 'cake { $this -> shell } help' for help on usage of the { $this -> name } { $command } " );
}
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Creates a file at given path
*
* @ param string $path Where to put the file .
* @ param string $contents Content to put in the file .
* @ return boolean Success
* @ access public
*/
function createFile ( $path , $contents ) {
$path = str_replace ( DS . DS , DS , $path );
$this -> out ( " \n " . sprintf ( __ ( " Creating file %s " , true ), $path ));
if ( is_file ( $path ) && $this -> interactive === true ) {
$key = $this -> in ( __ ( " File exists, overwrite? " , true ) . " { $path } " , array ( 'y' , 'n' , 'q' ), 'n' );
if ( low ( $key ) == 'q' ) {
$this -> out ( __ ( " Quitting. " , true ) . " \n " );
exit ;
} elseif ( low ( $key ) != 'y' ) {
$this -> out ( __ ( " Skip " , true ) . " { $path } \n " );
return false ;
}
}
if ( ! class_exists ( 'File' )) {
uses ( 'file' );
}
if ( $File = new File ( $path , true )) {
$data = $File -> prepare ( $contents );
$File -> write ( $data );
$this -> out ( __ ( " Wrote " , true ) . " { $path } " );
return true ;
} else {
$this -> err ( __ ( " Error! Could not write to " , true ) . " { $path } . \n " );
return false ;
}
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Outputs usage text on the standard output . Implement it in subclasses .
*
* @ access public
*/
function help () {
if ( $this -> command != null ) {
$this -> err ( " Unknown { $this -> name } command ' $this->command '. \n For usage, try 'cake { $this -> shell } help'. \n \n " );
} else {
$this -> Dispatch -> help ();
}
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Action to create a Unit Test
*
* @ return boolean Success
* @ access protected
*/
function _checkUnitTest () {
2008-10-09 17:42:10 +00:00
if ( App :: import ( 'vendor' , 'simpletest' . DS . 'simpletest' )) {
2008-05-30 11:40:08 +00:00
return true ;
}
2009-03-04 01:17:44 +00:00
$unitTest = $this -> in ( 'SimpleTest is not installed. Do you want to bake unit test files anyway?' , array ( 'y' , 'n' ), 'y' );
2008-05-30 11:40:08 +00:00
$result = low ( $unitTest ) == 'y' || low ( $unitTest ) == 'yes' ;
if ( $result ) {
2009-03-04 01:17:44 +00:00
$this -> out ( " \n You can download SimpleTest from http://simpletest.org " , true );
2008-05-30 11:40:08 +00:00
}
return $result ;
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Makes absolute file path easier to read
*
* @ param string $file Absolute file path
* @ return sting short path
* @ access public
*/
function shortPath ( $file ) {
$shortPath = str_replace ( ROOT , null , $file );
$shortPath = str_replace ( '..' . DS , '' , $shortPath );
return r ( DS . DS , DS , $shortPath );
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Creates the proper controller path for the specified controller class name
*
* @ param string $name Controller class name
* @ return string Path to controller
* @ access protected
*/
function _controllerPath ( $name ) {
return low ( Inflector :: underscore ( $name ));
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Creates the proper controller plural name for the specified controller class name
*
* @ param string $name Controller class name
* @ return string Controller plural name
* @ access protected
*/
function _controllerName ( $name ) {
return Inflector :: pluralize ( Inflector :: camelize ( $name ));
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Creates the proper controller camelized name ( singularized ) for the specified name
*
* @ param string $name Name
* @ return string Camelized and singularized controller name
* @ access protected
*/
function _modelName ( $name ) {
return Inflector :: camelize ( Inflector :: singularize ( $name ));
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Creates the proper singular model key for associations
*
* @ param string $name Controller class name
* @ return string Singular model key
* @ access protected
*/
function _modelKey ( $name ) {
return Inflector :: underscore ( Inflector :: singularize ( $name )) . '_id' ;
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Creates the proper model name from a foreign key
*
* @ param string $key Foreign key
* @ return string Model name
* @ access protected
*/
function _modelNameFromKey ( $key ) {
$name = str_replace ( '_id' , '' , $key );
return Inflector :: camelize ( $name );
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* creates the singular name for use in views .
*
* @ param string $name
* @ return string $name
* @ access protected
*/
function _singularName ( $name ) {
return Inflector :: variable ( Inflector :: singularize ( $name ));
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Creates the plural name for views
*
* @ param string $name Name to use
* @ return string Plural name for views
* @ access protected
*/
function _pluralName ( $name ) {
return Inflector :: variable ( Inflector :: pluralize ( $name ));
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Creates the singular human name used in views
*
* @ param string $name Controller name
* @ return string Singular human name
* @ access protected
*/
function _singularHumanName ( $name ) {
return Inflector :: humanize ( Inflector :: underscore ( Inflector :: singularize ( $name )));
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Creates the plural human name used in views
*
* @ param string $name Controller name
* @ return string Plural human name
* @ access protected
*/
function _pluralHumanName ( $name ) {
return Inflector :: humanize ( Inflector :: underscore ( Inflector :: pluralize ( $name )));
}
2009-07-24 19:18:37 +00:00
2009-06-06 23:59:58 +00:00
/**
* Find the correct path for a plugin . Scans $pluginPaths for the plugin you want .
*
* @ param string $pluginName Name of the plugin you want ie . DebugKit
* @ return string $path path to the correct plugin .
**/
function _pluginPath ( $pluginName ) {
2009-07-07 03:11:57 +00:00
$pluginPaths = App :: path ( 'plugins' );
2009-06-06 23:59:58 +00:00
$pluginDirName = Inflector :: underscore ( $pluginName );
foreach ( $pluginPaths as $path ) {
if ( is_dir ( $path . $pluginDirName )) {
return $path . $pluginDirName . DS ;
}
}
return $pluginPaths [ 0 ] . $pluginDirName . DS ;
}
2008-05-30 11:40:08 +00:00
}
?>