2007-05-14 12:24:40 +00:00
< ? php
/* SVN FILE: $Id$ */
/**
* The Project Task handles creating the base application
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP ( tm ) : Rapid Development Framework < http :// www . cakephp . org />
2008-01-01 22:18:17 +00:00
* Copyright 2005 - 2008 , Cake Software Foundation , Inc .
2007-05-14 12:24:40 +00:00
* 1785 E . Sahara Avenue , Suite 490 - 204
* Las Vegas , Nevada 89104
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice .
*
* @ filesource
2008-01-01 22:18:17 +00:00
* @ copyright Copyright 2005 - 2008 , Cake Software Foundation , Inc .
2007-05-14 12:24:40 +00:00
* @ link http :// www . cakefoundation . org / projects / info / cakephp CakePHP ( tm ) Project
* @ package cake
* @ subpackage cake . cake . scripts . bake
* @ since CakePHP ( tm ) v 1.2
* @ version $Revision $
* @ modifiedby $LastChangedBy $
* @ lastmodified $Date $
* @ license http :// www . opensource . org / licenses / mit - license . php The MIT License
*/
2007-06-20 06:15:35 +00:00
if ( ! class_exists ( 'File' )) {
2007-05-14 12:24:40 +00:00
uses ( 'file' );
}
2007-06-04 07:21:17 +00:00
/**
* Task class for creating new project apps and plugins
*
* @ package cake
* @ subpackage cake . cake . console . libs . tasks
*/
2007-05-14 12:24:40 +00:00
class ProjectTask extends Shell {
2007-06-03 23:56:33 +00:00
/**
* Override
*
2007-10-22 05:52:20 +00:00
* @ access public
2007-06-03 23:56:33 +00:00
*/
2007-10-22 05:52:20 +00:00
function initialize () {
}
2007-07-18 01:54:47 +00:00
/**
* Override
*
2007-10-22 05:52:20 +00:00
* @ access public
2007-07-18 01:54:47 +00:00
*/
2007-10-22 05:52:20 +00:00
function startup () {
}
2007-05-14 12:24:40 +00:00
/**
* Checks that given project path does not already exist , and
2007-12-21 02:54:45 +00:00
* finds the app directory in it . Then it calls bake () with that information .
2007-05-14 12:24:40 +00:00
*
2007-10-22 05:52:20 +00:00
* @ param string $project Project path
* @ access public
2007-05-14 12:24:40 +00:00
*/
function execute ( $project = null ) {
2007-06-20 06:15:35 +00:00
if ( $project === null ) {
if ( isset ( $this -> args [ 0 ])) {
2007-05-27 16:13:42 +00:00
$project = $this -> args [ 0 ];
2007-05-14 12:24:40 +00:00
$this -> Dispatch -> shiftArgs ();
}
}
2007-09-11 19:03:34 +00:00
if ( $project ) {
if ( $project { 0 } == '/' || $project { 0 } == DS ) {
$this -> Dispatch -> parseParams ( array ( '-working' , $project ));
} else {
$this -> Dispatch -> parseParams ( array ( '-app' , $project ));
}
}
$project = $this -> params [ 'working' ];
2007-07-25 04:38:28 +00:00
if ( empty ( $this -> params [ 'skel' ])) {
2007-07-18 01:54:47 +00:00
$this -> params [ 'skel' ] = '' ;
if ( is_dir ( CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'console' . DS . 'libs' . DS . 'templates' . DS . 'skel' ) === true ) {
$this -> params [ 'skel' ] = CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'console' . DS . 'libs' . DS . 'templates' . DS . 'skel' ;
}
}
2007-05-27 16:13:42 +00:00
2007-09-11 19:03:34 +00:00
if ( $project ) {
2007-05-15 03:41:50 +00:00
$response = false ;
2007-07-08 19:33:20 +00:00
while ( $response == false && is_dir ( $project ) === true && config ( 'core' ) === true ) {
2007-05-14 12:24:40 +00:00
$response = $this -> in ( 'A project already exists in this location: ' . $project . ' Overwrite?' , array ( 'y' , 'n' ), 'n' );
2007-06-20 06:15:35 +00:00
if ( low ( $response ) === 'n' ) {
2007-10-20 05:55:37 +00:00
$response = false ;
while ( ! $response ) {
$response = $this -> in ( " What is the full path for this app including the app directory name? \n Example: " . $this -> params [ 'root' ] . DS . " myapp \n [Q]uit " , null , 'Q' );
if ( strtoupper ( $response ) === 'Q' ) {
$this -> out ( 'Bake Aborted' );
exit ();
}
$this -> params [ 'working' ] = null ;
$this -> params [ 'app' ] = null ;
$this -> execute ( $response );
exit ();
}
2007-05-14 12:24:40 +00:00
}
}
}
2007-05-27 16:13:42 +00:00
2007-05-14 12:24:40 +00:00
while ( ! $project ) {
2007-07-18 01:54:47 +00:00
$project = $this -> in ( " What is the full path for this app including the app directory name? \n Example: " . $this -> params [ 'root' ] . DS . " myapp " , null , $this -> params [ 'root' ] . DS . 'myapp' );
2007-05-14 12:24:40 +00:00
$this -> execute ( $project );
exit ();
}
2007-07-18 01:54:47 +00:00
if ( ! is_dir ( $this -> params [ 'root' ])) {
2007-12-21 02:54:45 +00:00
$this -> err ( __ ( 'The directory path you supplied was not found. Please try again.' , true ));
2007-05-14 12:24:40 +00:00
}
2007-12-21 02:54:45 +00:00
if ( $this -> bake ( $project )) {
$path = Folder :: slashTerm ( $project );
if ( $this -> createHome ( $path )) {
$this -> out ( __ ( 'Welcome page created' , true ));
} else {
$this -> out ( __ ( 'The Welcome page was NOT created' , true ));
}
if ( $this -> securitySalt ( $path ) === true ) {
$this -> out ( __ ( 'Random hash key created for \'Security.salt\'' , true ));
} else {
$this -> err ( sprintf ( __ ( 'Unable to generate random hash for \'Security.salt\', you should change it in %s' , true ), CONFIGS . 'core.php' ));
}
$corePath = $this -> corePath ( $path );
if ( $corePath === true ) {
2008-01-05 19:31:29 +00:00
$this -> out ( sprintf ( __ ( 'CAKE_CORE_INCLUDE_PATH set to %s in webroot/index.php' , true ), CAKE_CORE_INCLUDE_PATH ));
$this -> out ( __ ( 'Remember to check this value after moving to production server' , true ));
2007-12-21 02:54:45 +00:00
} elseif ( $corePath === false ) {
$this -> err ( sprintf ( __ ( 'Unable to set CAKE_CORE_INCLUDE_PATH, you should change it in %s' , true ), $path . 'webroot' . DS . 'index.php' ));
}
$Folder = new Folder ( $path );
if ( ! $Folder -> chmod ( $path . 'tmp' , 0777 )) {
$this -> err ( sprintf ( __ ( 'Could not set permissions on %s' , true ), $path . DS . 'tmp' ));
$this -> out ( sprintf ( __ ( 'chmod -R 0777 %s' , true ), $path . DS . 'tmp' ));
}
}
2007-05-14 12:24:40 +00:00
exit ();
}
/**
* Looks for a skeleton template of a Cake application ,
* and if not found asks the user for a path . When there is a path
* this method will make a deep copy of the skeleton to the project directory .
* A default home page will be added , and the tmp file storage will be chmod ' ed to 0777.
*
2007-10-22 05:52:20 +00:00
* @ param string $path Project path
2007-12-21 02:54:45 +00:00
* @ param string $skel Path to copy from
* @ param string $skip array of directories to skip when copying
2007-10-22 05:52:20 +00:00
* @ access private
2007-05-14 12:24:40 +00:00
*/
2007-12-21 02:54:45 +00:00
function bake ( $path , $skel = null , $skip = array ( 'empty' )) {
if ( ! $skel ) {
$skel = $this -> params [ 'skel' ];
}
while ( ! $skel ) {
$skel = $this -> in ( sprintf ( __ ( " What is the path to the directory layout you wish to copy? \n Example: %s " ), APP , null , ROOT . DS . 'myapp' . DS ));
2007-07-18 01:54:47 +00:00
if ( $skel == '' ) {
2007-12-21 02:54:45 +00:00
$this -> out ( __ ( 'The directory path you supplied was empty. Please try again.' , true ));
2007-07-18 01:54:47 +00:00
} else {
while ( is_dir ( $skel ) === false ) {
2007-12-21 02:54:45 +00:00
$skel = $this -> in ( __ ( 'Directory path does not exist please choose another:' , true ));
2007-05-14 12:24:40 +00:00
}
}
}
2007-07-18 01:54:47 +00:00
2007-05-14 12:24:40 +00:00
$app = basename ( $path );
2007-10-27 20:00:29 +00:00
2007-07-18 01:54:47 +00:00
$this -> out ( 'Bake Project' );
2007-05-14 12:24:40 +00:00
$this -> out ( " Skel Directory: $skel " );
2007-05-27 16:13:42 +00:00
$this -> out ( " Will be copied to: { $path } " );
2007-05-14 12:24:40 +00:00
$this -> hr ();
2007-12-21 02:54:45 +00:00
2007-05-14 12:24:40 +00:00
$looksGood = $this -> in ( 'Look okay?' , array ( 'y' , 'n' , 'q' ), 'y' );
if ( low ( $looksGood ) == 'y' || low ( $looksGood ) == 'yes' ) {
2007-12-21 02:54:45 +00:00
$verbose = $this -> in ( __ ( 'Do you want verbose output?' , true ), array ( 'y' , 'n' ), 'n' );
2007-05-14 12:24:40 +00:00
$Folder = new Folder ( $skel );
2007-12-21 02:54:45 +00:00
if ( $Folder -> copy ( array ( 'to' => $path , 'skip' => $skip ))) {
2007-05-14 12:24:40 +00:00
$this -> hr ();
2007-09-18 04:16:04 +00:00
$this -> out ( sprintf ( __ ( " Created: %s in %s " , true ), $app , $path ));
2007-05-14 12:24:40 +00:00
$this -> hr ();
} else {
$this -> err ( " ' " . $app . " ' could not be created properly " );
2007-12-21 02:54:45 +00:00
return false ;
2007-05-14 12:24:40 +00:00
}
2007-12-21 02:54:45 +00:00
if ( low ( $verbose ) == 'y' || low ( $verbose ) == 'yes' ) {
2007-06-20 06:15:35 +00:00
foreach ( $Folder -> messages () as $message ) {
2007-05-14 12:24:40 +00:00
$this -> out ( $message );
}
}
2007-12-21 02:54:45 +00:00
return true ;
2007-05-14 12:24:40 +00:00
} elseif ( low ( $looksGood ) == 'q' || low ( $looksGood ) == 'quit' ) {
$this -> out ( 'Bake Aborted.' );
} else {
2007-10-20 05:55:37 +00:00
$this -> params [ 'working' ] = null ;
$this -> params [ 'app' ] = null ;
2007-05-14 12:24:40 +00:00
$this -> execute ( false );
}
}
/**
* Writes a file with a default home page to the project .
*
2007-10-22 05:52:20 +00:00
* @ param string $dir Path to project
2007-10-22 16:09:35 +00:00
* @ return boolean Success
2007-10-22 05:52:20 +00:00
* @ access public
2007-05-14 12:24:40 +00:00
*/
2007-10-22 05:52:20 +00:00
function createHome ( $dir ) {
2007-10-27 20:00:29 +00:00
$app = basename ( $dir );
2007-05-14 12:24:40 +00:00
$path = $dir . 'views' . DS . 'pages' . DS ;
include ( CAKE_CORE_INCLUDE_PATH . DS . 'cake' . DS . 'console' . DS . 'libs' . DS . 'templates' . DS . 'views' . DS . 'home.ctp' );
2007-05-15 03:51:40 +00:00
return $this -> createFile ( $path . 'home.ctp' , $output );
2007-05-14 12:24:40 +00:00
}
/**
2007-10-22 05:52:20 +00:00
* Generates and writes 'Security.salt'
2007-05-14 12:24:40 +00:00
*
2007-10-22 05:52:20 +00:00
* @ param string $path Project path
2007-10-22 16:09:35 +00:00
* @ return boolean Success
2007-10-22 05:52:20 +00:00
* @ access public
2007-05-14 12:24:40 +00:00
*/
2007-10-16 09:05:25 +00:00
function securitySalt ( $path ) {
2007-05-14 12:24:40 +00:00
$File =& new File ( $path . 'config' . DS . 'core.php' );
$contents = $File -> read ();
2007-10-16 09:05:25 +00:00
if ( preg_match ( '/([\\t\\x20]*Configure::write\\(\\\'Security.salt\\\',[\\t\\x20\'A-z0-9]*\\);)/' , $contents , $match )) {
2007-05-14 12:24:40 +00:00
uses ( 'Security' );
$string = Security :: generateAuthKey ();
2007-10-16 09:05:25 +00:00
$result = str_replace ( $match [ 0 ], " \t " . 'Configure::write(\'Security.salt\', \'' . $string . '\');' , $contents );
2007-06-20 07:51:52 +00:00
if ( $File -> write ( $result )) {
2007-05-14 12:24:40 +00:00
return true ;
} else {
return false ;
}
} else {
return false ;
}
}
/**
2007-10-22 05:52:20 +00:00
* Generates and writes CAKE_CORE_INCLUDE_PATH
2007-05-14 12:24:40 +00:00
*
2007-10-22 05:52:20 +00:00
* @ param string $path Project path
2007-10-22 16:09:35 +00:00
* @ return boolean Success
2007-10-22 05:52:20 +00:00
* @ access public
2007-05-14 12:24:40 +00:00
*/
2007-06-20 07:51:52 +00:00
function corePath ( $path ) {
2007-06-20 06:15:35 +00:00
if ( dirname ( $path ) !== CAKE_CORE_INCLUDE_PATH ) {
2007-05-14 12:24:40 +00:00
$File =& new File ( $path . 'webroot' . DS . 'index.php' );
$contents = $File -> read ();
if ( preg_match ( '/([\\t\\x20]*define\\(\\\'CAKE_CORE_INCLUDE_PATH\\\',[\\t\\x20\'A-z0-9]*\\);)/' , $contents , $match )) {
$result = str_replace ( $match [ 0 ], " \t \t define('CAKE_CORE_INCLUDE_PATH', ' " . CAKE_CORE_INCLUDE_PATH . " '); " , $contents );
2007-06-20 07:51:52 +00:00
if ( $File -> write ( $result )) {
2007-05-14 12:24:40 +00:00
return true ;
} else {
return false ;
}
} else {
return false ;
}
2007-12-21 02:54:45 +00:00
$File =& new File ( $path . 'webroot' . DS . 'test.php' );
$contents = $File -> read ();
if ( preg_match ( '/([\\t\\x20]*define\\(\\\'CAKE_CORE_INCLUDE_PATH\\\',[\\t\\x20\'A-z0-9]*\\);)/' , $contents , $match )) {
$result = str_replace ( $match [ 0 ], " \t \t define('CAKE_CORE_INCLUDE_PATH', ' " . CAKE_CORE_INCLUDE_PATH . " '); " , $contents );
if ( $File -> write ( $result )) {
return true ;
} else {
return false ;
}
} else {
return false ;
}
2007-05-14 12:24:40 +00:00
}
}
2007-06-03 23:56:33 +00:00
/**
2007-08-25 15:29:44 +00:00
* Enables Configure :: read ( 'Routing.admin' ) in / app / config / core . php
2007-06-03 23:56:33 +00:00
*
2007-10-22 05:52:20 +00:00
* @ param string $name Name to use as admin routing
2007-10-22 16:09:35 +00:00
* @ return boolean Success
2007-10-22 05:52:20 +00:00
* @ access public
2007-06-21 14:38:46 +00:00
*/
2007-06-20 07:51:52 +00:00
function cakeAdmin ( $name ) {
2007-06-21 14:38:46 +00:00
$File =& new File ( CONFIGS . 'core.php' );
2007-06-10 06:20:39 +00:00
$contents = $File -> read ();
2007-08-25 15:38:26 +00:00
if ( preg_match ( '%([/\\t\\x20]*Configure::write\(\'Routing.admin\',[\\t\\x20\'a-z]*\\);)%' , $contents , $match )) {
2007-10-16 09:05:25 +00:00
$result = str_replace ( $match [ 0 ], " \t " . 'Configure::write(\'Routing.admin\', \'' . $name . '\');' , $contents );
2007-06-20 07:51:52 +00:00
if ( $File -> write ( $result )) {
2007-08-25 15:29:44 +00:00
Configure :: write ( 'Routing.admin' , $name );
2007-06-03 23:56:33 +00:00
return true ;
} else {
return false ;
}
} else {
return false ;
}
}
2007-12-21 02:54:45 +00:00
/**
* Help
*
* @ return void
* @ access public
*/
function help () {
$this -> hr ();
$this -> out ( " Usage: cake bake project <arg1> " );
$this -> hr ();
$this -> out ( 'Commands:' );
$this -> out ( " \n \t project <name> \n \t \t bakes app directory structure. \n \t \t if <name> begins with '/' path is absolute. " );
$this -> out ( " " );
exit ();
}
2007-05-14 12:24:40 +00:00
}
?>