2007-05-14 12:24:40 +00:00
< ? php
/* SVN FILE: $Id$ */
/**
* The ControllerTask handles creating and updating controller files .
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP ( tm ) : Rapid Development Framework < http :// www . cakephp . org />
* Copyright 2005 - 2007 , Cake Software Foundation , Inc .
* 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
* @ copyright Copyright 2005 - 2007 , Cake Software Foundation , Inc .
* @ link http :// www . cakefoundation . org / projects / info / cakephp CakePHP ( tm ) Project
* @ package cake
* @ subpackage cake . cake . console . libs . tasks
* @ since CakePHP ( tm ) v 1.2
* @ version $Revision $
* @ modifiedby $LastChangedBy $
* @ lastmodified $Date $
* @ license http :// www . opensource . org / licenses / mit - license . php The MIT License
*/
/**
* Task class for creating and updating controller files .
*
* @ package cake
* @ subpackage cake . cake . console . libs . tasks
*/
class ControllerTask extends Shell {
2007-06-03 23:56:33 +00:00
/**
* Tasks to be loaded by this Task
*
* @ var array
2007-10-22 05:52:20 +00:00
* @ access public
2007-06-03 23:56:33 +00:00
*/
2007-06-05 15:37:06 +00:00
var $tasks = array ( 'Project' );
2007-06-03 23:56:33 +00:00
/**
* Override initialize
*
2007-10-22 05:52:20 +00:00
* @ access public
2007-06-03 23:56:33 +00:00
*/
function initialize () {}
/**
* Execution method always used for tasks
*
2007-10-22 05:52:20 +00:00
* @ access public
2007-06-03 23:56:33 +00:00
*/
2007-05-14 12:24:40 +00:00
function execute () {
2007-06-20 06:15:35 +00:00
if ( empty ( $this -> args )) {
2007-05-14 12:24:40 +00:00
$this -> __interactive ();
}
2007-06-03 23:56:33 +00:00
2007-06-20 06:15:35 +00:00
if ( isset ( $this -> args [ 0 ])) {
2007-06-03 23:56:33 +00:00
$controller = Inflector :: camelize ( $this -> args [ 0 ]);
$actions = null ;
2007-06-20 06:15:35 +00:00
if ( isset ( $this -> args [ 1 ]) && $this -> args [ 1 ] == 'scaffold' ) {
2007-06-03 23:56:33 +00:00
$this -> out ( 'Baking scaffold for ' . $controller );
$actions = $this -> __bakeActions ( $controller );
} else {
$actions = 'scaffold' ;
}
2007-08-05 02:03:18 +00:00
if (( isset ( $this -> args [ 1 ]) && $this -> args [ 1 ] == 'admin' ) || ( isset ( $this -> args [ 2 ]) && $this -> args [ 2 ] == 'admin' )) {
2007-06-20 06:15:35 +00:00
if ( $admin = $this -> getAdmin ()) {
2007-08-25 15:29:44 +00:00
$this -> out ( 'Adding ' . Configure :: read ( 'Routing.admin' ) . ' methods' );
2007-06-05 17:06:00 +00:00
if ( $actions == 'scaffold' ) {
$actions = $this -> __bakeActions ( $controller , $admin );
} else {
$actions .= $this -> __bakeActions ( $controller , $admin );
}
2007-06-03 23:56:33 +00:00
}
}
$baked = $this -> __bake ( $controller , $actions );
$this -> __bakeTest ( $controller );
}
2007-05-14 12:24:40 +00:00
}
2007-06-03 23:56:33 +00:00
/**
* Interactive
*
2007-10-22 05:52:20 +00:00
* @ access private
2007-06-03 23:56:33 +00:00
*/
2007-05-14 12:24:40 +00:00
function __interactive () {
2007-06-21 14:38:46 +00:00
$this -> interactive = false ;
2007-05-14 12:24:40 +00:00
$this -> hr ();
$this -> out ( 'Controller Bake:' );
$this -> hr ();
2007-05-16 09:26:33 +00:00
$actions = '' ;
2007-05-14 12:24:40 +00:00
$uses = array ();
$helpers = array ();
$components = array ();
$wannaUseSession = 'y' ;
$wannaDoAdmin = 'n' ;
$wannaUseScaffold = 'n' ;
$wannaDoScaffolding = 'y' ;
2007-06-03 23:56:33 +00:00
$controllerName = $this -> getName ();
2007-05-14 12:24:40 +00:00
$controllerPath = low ( Inflector :: underscore ( $controllerName ));
$doItInteractive = $this -> in ( " Would you like bake to build your controller interactively? \n Warning: Choosing no will overwrite { $controllerName } controller if it exist. " , array ( 'y' , 'n' ), 'y' );
if ( low ( $doItInteractive ) == 'y' || low ( $doItInteractive ) == 'yes' ) {
$this -> interactive = true ;
$wannaUseScaffold = $this -> in ( " Would you like to use scaffolding? " , array ( 'y' , 'n' ), 'y' );
if ( low ( $wannaUseScaffold ) == 'n' || low ( $wannaUseScaffold ) == 'no' ) {
$wannaDoScaffolding = $this -> in ( " Would you like to include some basic class methods (index(), add(), view(), edit())? " , array ( 'y' , 'n' ), 'n' );
if ( low ( $wannaDoScaffolding ) == 'y' || low ( $wannaDoScaffolding ) == 'yes' ) {
$wannaDoAdmin = $this -> in ( " Would you like to create the methods for admin routing? " , array ( 'y' , 'n' ), 'n' );
}
$wannaDoUses = $this -> in ( " Would you like this controller to use other models besides ' " . $this -> _modelName ( $controllerName ) . " '? " , array ( 'y' , 'n' ), 'n' );
if ( low ( $wannaDoUses ) == 'y' || low ( $wannaDoUses ) == 'yes' ) {
$usesList = $this -> in ( " Please provide a comma separated list of the classnames of other models you'd like to use. \n Example: 'Author, Article, Book' " );
$usesListTrimmed = str_replace ( ' ' , '' , $usesList );
$uses = explode ( ',' , $usesListTrimmed );
}
$wannaDoHelpers = $this -> in ( " Would you like this controller to use other helpers besides HtmlHelper and FormHelper? " , array ( 'y' , 'n' ), 'n' );
if ( low ( $wannaDoHelpers ) == 'y' || low ( $wannaDoHelpers ) == 'yes' ) {
$helpersList = $this -> in ( " Please provide a comma separated list of the other helper names you'd like to use. \n Example: 'Ajax, Javascript, Time' " );
$helpersListTrimmed = str_replace ( ' ' , '' , $helpersList );
$helpers = explode ( ',' , $helpersListTrimmed );
}
$wannaDoComponents = $this -> in ( " Would you like this controller to use any components? " , array ( 'y' , 'n' ), 'n' );
if ( low ( $wannaDoComponents ) == 'y' || low ( $wannaDoComponents ) == 'yes' ) {
$componentsList = $this -> in ( " Please provide a comma separated list of the component names you'd like to use. \n Example: 'Acl, MyNiftyHelper' " );
$componentsListTrimmed = str_replace ( ' ' , '' , $componentsList );
$components = explode ( ',' , $componentsListTrimmed );
}
$wannaUseSession = $this -> in ( " Would you like to use Sessions? " , array ( 'y' , 'n' ), 'y' );
} else {
$wannaDoScaffolding = 'n' ;
}
} else {
$wannaDoScaffolding = $this -> in ( " Would you like to include some basic class methods (index(), add(), view(), edit())? " , array ( 'y' , 'n' ), 'y' );
if ( low ( $wannaDoScaffolding ) == 'y' || low ( $wannaDoScaffolding ) == 'yes' ) {
$wannaDoAdmin = $this -> in ( " Would you like to create the methods for admin routing? " , array ( 'y' , 'n' ), 'y' );
}
}
2007-06-21 14:38:46 +00:00
$admin = false ;
2007-05-14 12:24:40 +00:00
if (( low ( $wannaDoAdmin ) == 'y' || low ( $wannaDoAdmin ) == 'yes' )) {
2007-06-03 23:56:33 +00:00
$admin = $this -> getAdmin ();
2007-05-14 12:24:40 +00:00
}
if ( low ( $wannaDoScaffolding ) == 'y' || low ( $wannaDoScaffolding ) == 'yes' ) {
2007-10-22 05:52:20 +00:00
$actions = $this -> __bakeActions ( $controllerName , null , in_array ( low ( $wannaUseSession ), array ( 'y' , 'yes' )));
2007-06-20 06:15:35 +00:00
if ( $admin ) {
2007-10-22 05:52:20 +00:00
$actions .= $this -> __bakeActions ( $controllerName , $admin , in_array ( low ( $wannaUseSession ), array ( 'y' , 'yes' )));
2007-05-14 12:24:40 +00:00
}
}
2007-06-20 06:15:35 +00:00
if ( $this -> interactive === true ) {
2007-05-14 12:24:40 +00:00
$this -> out ( '' );
$this -> hr ();
$this -> out ( 'The following controller will be created:' );
$this -> hr ();
$this -> out ( " Controller Name: $controllerName " );
2007-06-03 23:56:33 +00:00
2007-05-14 12:24:40 +00:00
if ( low ( $wannaUseScaffold ) == 'y' || low ( $wannaUseScaffold ) == 'yes' ) {
$this -> out ( " var \$ scaffold; " );
2007-06-03 23:56:33 +00:00
$actions = 'scaffold' ;
2007-05-14 12:24:40 +00:00
}
2007-06-20 06:15:35 +00:00
if ( count ( $uses )) {
2007-05-14 12:24:40 +00:00
$this -> out ( " Uses: " , false );
2007-06-20 06:15:35 +00:00
foreach ( $uses as $use ) {
2007-05-14 12:24:40 +00:00
if ( $use != $uses [ count ( $uses ) - 1 ]) {
$this -> out ( ucfirst ( $use ) . " , " , false );
} else {
$this -> out ( ucfirst ( $use ));
}
}
}
2007-06-20 06:15:35 +00:00
if ( count ( $helpers )) {
2007-05-14 12:24:40 +00:00
$this -> out ( " Helpers: " , false );
2007-06-20 06:15:35 +00:00
foreach ( $helpers as $help ) {
2007-05-14 12:24:40 +00:00
if ( $help != $helpers [ count ( $helpers ) - 1 ]) {
$this -> out ( ucfirst ( $help ) . " , " , false );
} else {
$this -> out ( ucfirst ( $help ));
}
}
}
2007-06-20 06:15:35 +00:00
if ( count ( $components )) {
2007-05-14 12:24:40 +00:00
$this -> out ( " Components: " , false );
2007-06-20 06:15:35 +00:00
foreach ( $components as $comp ) {
2007-05-14 12:24:40 +00:00
if ( $comp != $components [ count ( $components ) - 1 ]) {
$this -> out ( ucfirst ( $comp ) . " , " , false );
} else {
$this -> out ( ucfirst ( $comp ));
}
}
}
$this -> hr ();
$looksGood = $this -> in ( 'Look okay?' , array ( 'y' , 'n' ), 'y' );
if ( low ( $looksGood ) == 'y' || low ( $looksGood ) == 'yes' ) {
2007-06-03 23:56:33 +00:00
$baked = $this -> __bake ( $controllerName , $actions , $helpers , $components , $uses );
2007-05-14 12:24:40 +00:00
if ( $baked && $this -> _checkUnitTest ()) {
$this -> __bakeTest ( $controllerName );
}
} else {
$this -> out ( 'Bake Aborted.' );
}
} else {
2007-06-03 23:56:33 +00:00
$baked = $this -> __bake ( $controllerName , $actions , $helpers , $components , $uses );
2007-05-14 12:24:40 +00:00
if ( $baked && $this -> _checkUnitTest ()) {
$this -> __bakeTest ( $controllerName );
}
}
}
2007-10-22 05:52:20 +00:00
/**
* Bake scaffold actions
*
* @ param string $controllerName Controller name
* @ param string $admin Admin route to use
2007-10-22 16:54:36 +00:00
* @ param boolean $wannaUseSession Set to true to use sessions , false otherwise
2007-10-22 05:52:20 +00:00
* @ return string Baked actions
* @ access private
*/
function __bakeActions ( $controllerName , $admin = null , $wannaUseSession = true ) {
2007-05-14 12:24:40 +00:00
$currentModelName = $this -> _modelName ( $controllerName );
2007-06-20 06:15:35 +00:00
if ( ! loadModel ( $currentModelName )) {
2007-11-02 06:43:17 +00:00
$this -> err ( 'You must have a model for this class to build scaffold methods. Please try again.' );
2007-05-14 12:24:40 +00:00
exit ;
}
$actions = null ;
$modelObj =& new $currentModelName ();
$controllerPath = $this -> _controllerPath ( $controllerName );
$pluralName = $this -> _pluralName ( $currentModelName );
2007-11-02 06:43:17 +00:00
$singularName = Inflector :: variable ( $currentModelName );
$singularHumanName = Inflector :: humanize ( $currentModelName );
$pluralHumanName = Inflector :: humanize ( $controllerName );
2007-05-14 12:24:40 +00:00
$actions .= " \n " ;
$actions .= " \t function { $admin } index() { \n " ;
$actions .= " \t \t \$ this-> { $currentModelName } ->recursive = 0; \n " ;
$actions .= " \t \t \$ this->set(' { $pluralName } ', \$ this->paginate()); \n " ;
$actions .= " \t } \n " ;
$actions .= " \n " ;
$actions .= " \t function { $admin } view( \$ id = null) { \n " ;
2007-06-20 06:15:35 +00:00
$actions .= " \t \t if (! \$ id) { \n " ;
2007-10-22 05:52:20 +00:00
if ( $wannaUseSession ) {
2007-05-26 12:13:00 +00:00
$actions .= " \t \t \t \$ this->Session->setFlash('Invalid { $singularHumanName } .'); \n " ;
$actions .= " \t \t \t \$ this->redirect(array('action'=>'index'), null, true); \n " ;
2007-05-14 12:24:40 +00:00
} else {
2007-05-26 12:13:00 +00:00
$actions .= " \t \t \t \$ this->flash('Invalid { $singularHumanName } ', array('action'=>'index')); \n " ;
2007-05-14 12:24:40 +00:00
}
$actions .= " \t \t } \n " ;
$actions .= " \t \t \$ this->set(' " . $singularName . " ', \$ this-> { $currentModelName } ->read(null, \$ id)); \n " ;
$actions .= " \t } \n " ;
$actions .= " \n " ;
/* ADD ACTION */
$compact = array ();
$actions .= " \t function { $admin } add() { \n " ;
2007-06-20 06:15:35 +00:00
$actions .= " \t \t if (!empty( \$ this->data)) { \n " ;
2007-05-14 12:24:40 +00:00
$actions .= " \t \t \t \$ this->cleanUpFields(); \n " ;
$actions .= " \t \t \t \$ this-> { $currentModelName } ->create(); \n " ;
2007-06-20 06:15:35 +00:00
$actions .= " \t \t \t if ( \$ this-> { $currentModelName } ->save( \$ this->data)) { \n " ;
2007-10-22 05:52:20 +00:00
if ( $wannaUseSession ) {
2007-05-26 12:13:00 +00:00
$actions .= " \t \t \t \t \$ this->Session->setFlash('The " . $singularHumanName . " has been saved'); \n " ;
$actions .= " \t \t \t \t \$ this->redirect(array('action'=>'index'), null, true); \n " ;
2007-05-14 12:24:40 +00:00
} else {
2007-05-26 12:13:00 +00:00
$actions .= " \t \t \t \t \$ this->flash(' { $currentModelName } saved.', array('action'=>'index')); \n " ;
$actions .= " \t \t \t \t exit(); \n " ;
2007-05-14 12:24:40 +00:00
}
$actions .= " \t \t \t } else { \n " ;
2007-10-22 05:52:20 +00:00
if ( $wannaUseSession ) {
2007-05-14 12:24:40 +00:00
$actions .= " \t \t \t \t \$ this->Session->setFlash('The { $singularHumanName } could not be saved. Please, try again.'); \n " ;
}
$actions .= " \t \t \t } \n " ;
$actions .= " \t \t } \n " ;
2007-06-20 06:15:35 +00:00
foreach ( $modelObj -> hasAndBelongsToMany as $associationName => $relation ) {
if ( ! empty ( $associationName )) {
2007-05-14 12:24:40 +00:00
$habtmModelName = $this -> _modelName ( $associationName );
$habtmSingularName = $this -> _singularName ( $associationName );
$habtmPluralName = $this -> _pluralName ( $associationName );
$actions .= " \t \t \$ { $habtmPluralName } = \$ this-> { $currentModelName } -> { $habtmModelName } ->generateList(); \n " ;
$compact [] = " ' { $habtmPluralName } ' " ;
}
}
2007-06-20 06:15:35 +00:00
foreach ( $modelObj -> belongsTo as $associationName => $relation ) {
if ( ! empty ( $associationName )) {
2007-05-14 12:24:40 +00:00
$belongsToModelName = $this -> _modelName ( $associationName );
$belongsToPluralName = $this -> _pluralName ( $associationName );
$actions .= " \t \t \$ { $belongsToPluralName } = \$ this-> { $currentModelName } -> { $belongsToModelName } ->generateList(); \n " ;
$compact [] = " ' { $belongsToPluralName } ' " ;
}
}
2007-06-20 06:15:35 +00:00
if ( ! empty ( $compact )) {
2007-05-14 12:24:40 +00:00
$actions .= " \t \t \$ this->set(compact( " . join ( ', ' , $compact ) . " )); \n " ;
}
$actions .= " \t } \n " ;
$actions .= " \n " ;
/* EDIT ACTION */
$compact = array ();
$actions .= " \t function { $admin } edit( \$ id = null) { \n " ;
2007-06-20 06:15:35 +00:00
$actions .= " \t \t if (! \$ id && empty( \$ this->data)) { \n " ;
2007-10-22 05:52:20 +00:00
if ( $wannaUseSession ) {
2007-05-14 12:24:40 +00:00
$actions .= " \t \t \t \$ this->Session->setFlash('Invalid { $singularHumanName } '); \n " ;
$actions .= " \t \t \t \$ this->redirect(array('action'=>'index'), null, true); \n " ;
} else {
$actions .= " \t \t \t \$ this->flash('Invalid { $singularHumanName } ', array('action'=>'index')); \n " ;
$actions .= " \t \t \t exit(); \n " ;
}
$actions .= " \t \t } \n " ;
2007-06-20 06:15:35 +00:00
$actions .= " \t \t if (!empty( \$ this->data)) { \n " ;
2007-05-14 12:24:40 +00:00
$actions .= " \t \t \t \$ this->cleanUpFields(); \n " ;
2007-06-20 06:15:35 +00:00
$actions .= " \t \t \t if ( \$ this-> { $currentModelName } ->save( \$ this->data)) { \n " ;
2007-10-22 05:52:20 +00:00
if ( $wannaUseSession ) {
2007-08-19 16:53:25 +00:00
$actions .= " \t \t \t \t \$ this->Session->setFlash('The " . $singularHumanName . " has been saved'); \n " ;
2007-05-14 12:24:40 +00:00
$actions .= " \t \t \t \t \$ this->redirect(array('action'=>'index'), null, true); \n " ;
} else {
2007-08-19 16:53:25 +00:00
$actions .= " \t \t \t \t \$ this->flash('The " . $singularHumanName . " has been saved.', array('action'=>'index')); \n " ;
2007-05-14 12:24:40 +00:00
$actions .= " \t \t \t \t exit(); \n " ;
}
$actions .= " \t \t \t } else { \n " ;
2007-10-22 05:52:20 +00:00
if ( $wannaUseSession ) {
2007-05-14 12:24:40 +00:00
$actions .= " \t \t \t \t \$ this->Session->setFlash('The { $singularHumanName } could not be saved. Please, try again.'); \n " ;
}
$actions .= " \t \t \t } \n " ;
$actions .= " \t \t } \n " ;
2007-06-20 06:15:35 +00:00
$actions .= " \t \t if (empty( \$ this->data)) { \n " ;
2007-05-14 12:24:40 +00:00
$actions .= " \t \t \t \$ this->data = \$ this-> { $currentModelName } ->read(null, \$ id); \n " ;
$actions .= " \t \t } \n " ;
2007-06-20 06:15:35 +00:00
foreach ( $modelObj -> hasAndBelongsToMany as $associationName => $relation ) {
if ( ! empty ( $associationName )) {
2007-05-14 12:24:40 +00:00
$habtmModelName = $this -> _modelName ( $associationName );
$habtmSingularName = $this -> _singularName ( $associationName );
$habtmPluralName = $this -> _pluralName ( $associationName );
$actions .= " \t \t \$ { $habtmPluralName } = \$ this-> { $currentModelName } -> { $habtmModelName } ->generateList(); \n " ;
$compact [] = " ' { $habtmPluralName } ' " ;
}
}
2007-06-20 06:15:35 +00:00
foreach ( $modelObj -> belongsTo as $associationName => $relation ) {
if ( ! empty ( $associationName )) {
2007-05-14 12:24:40 +00:00
$belongsToModelName = $this -> _modelName ( $associationName );
$belongsToPluralName = $this -> _pluralName ( $associationName );
$actions .= " \t \t \$ { $belongsToPluralName } = \$ this-> { $currentModelName } -> { $belongsToModelName } ->generateList(); \n " ;
$compact [] = " ' { $belongsToPluralName } ' " ;
}
}
2007-06-20 06:15:35 +00:00
if ( ! empty ( $compact )) {
2007-05-14 12:24:40 +00:00
$actions .= " \t \t \$ this->set(compact( " . join ( ',' , $compact ) . " )); \n " ;
}
$actions .= " \t } \n " ;
$actions .= " \n " ;
$actions .= " \t function { $admin } delete( \$ id = null) { \n " ;
2007-06-20 06:15:35 +00:00
$actions .= " \t \t if (! \$ id) { \n " ;
2007-10-22 05:52:20 +00:00
if ( $wannaUseSession ) {
2007-05-26 12:13:00 +00:00
$actions .= " \t \t \t \$ this->Session->setFlash('Invalid id for { $singularHumanName } '); \n " ;
$actions .= " \t \t \t \$ this->redirect(array('action'=>'index'), null, true); \n " ;
2007-05-14 12:24:40 +00:00
} else {
2007-05-26 12:13:00 +00:00
$actions .= " \t \t \t \$ this->flash('Invalid { $singularHumanName } ', array('action'=>'index')); \n " ;
2007-05-14 12:24:40 +00:00
}
$actions .= " \t \t } \n " ;
2007-06-20 06:15:35 +00:00
$actions .= " \t \t if ( \$ this-> { $currentModelName } ->del( \$ id)) { \n " ;
2007-10-22 05:52:20 +00:00
if ( $wannaUseSession ) {
2007-05-14 12:24:40 +00:00
$actions .= " \t \t \t \$ this->Session->setFlash(' " . $singularHumanName . " #'. \$ id.' deleted'); \n " ;
$actions .= " \t \t \t \$ this->redirect(array('action'=>'index'), null, true); \n " ;
} else {
$actions .= " \t \t \t \$ this->flash(' " . $singularHumanName . " #'. \$ id.' deleted', array('action'=>'index')); \n " ;
}
$actions .= " \t \t } \n " ;
$actions .= " \t } \n " ;
$actions .= " \n " ;
return $actions ;
2007-06-03 23:56:33 +00:00
}
2007-05-14 12:24:40 +00:00
/**
2007-10-22 05:52:20 +00:00
* Assembles and writes a Controller file
2007-05-14 12:24:40 +00:00
*
2007-10-22 05:52:20 +00:00
* @ param string $controllerName Controller name
* @ param string $actions Actions to add , or set the whole controller to use $scaffold ( set $actions to 'scaffold' )
* @ param array $helpers Helpers to use in controller
* @ param array $components Components to use in controller
* @ param array $uses Models to use in controller
* @ return string Baked controller
* @ access private
2007-05-14 12:24:40 +00:00
*/
2007-06-03 23:56:33 +00:00
function __bake ( $controllerName , $actions = '' , $helpers = null , $components = null , $uses = null ) {
2007-05-14 12:24:40 +00:00
$out = " <?php \n " ;
$out .= " class $controllerName " . " Controller extends AppController { \n \n " ;
$out .= " \t var \$ name = ' $controllerName '; \n " ;
2007-05-26 12:13:00 +00:00
2007-06-20 06:15:35 +00:00
if ( low ( $actions ) == 'scaffold' ) {
2007-05-26 12:13:00 +00:00
$out .= " \t var \$ scaffold; \n " ;
2007-05-14 12:24:40 +00:00
} else {
if ( count ( $uses )) {
$out .= " \t var \$ uses = array(' " . $this -> _modelName ( $controllerName ) . " ', " ;
2007-06-20 06:15:35 +00:00
foreach ( $uses as $use ) {
2007-05-14 12:24:40 +00:00
if ( $use != $uses [ count ( $uses ) - 1 ]) {
$out .= " ' " . $this -> _modelName ( $use ) . " ', " ;
} else {
$out .= " ' " . $this -> _modelName ( $use ) . " ' " ;
}
}
$out .= " ); \n " ;
}
2007-05-26 12:13:00 +00:00
$out .= " \t var \$ helpers = array('Html', 'Form' " ;
if ( count ( $helpers )) {
2007-06-20 06:15:35 +00:00
foreach ( $helpers as $help ) {
2007-05-26 12:13:00 +00:00
if ( $help != $helpers [ count ( $helpers ) - 1 ]) {
2007-08-11 17:10:04 +00:00
$out .= " , ' " . Inflector :: camelize ( $help ) . " ', " ;
2007-05-26 12:13:00 +00:00
} else {
$out .= " , ' " . Inflector :: camelize ( $help ) . " ' " ;
2007-05-14 12:24:40 +00:00
}
}
2007-05-26 12:13:00 +00:00
}
$out .= " ); \n " ;
2007-05-14 12:24:40 +00:00
if ( count ( $components )) {
$out .= " \t var \$ components = array( " ;
2007-06-20 06:15:35 +00:00
foreach ( $components as $comp ) {
2007-05-14 12:24:40 +00:00
if ( $comp != $components [ count ( $components ) - 1 ]) {
$out .= " ' " . Inflector :: camelize ( $comp ) . " ', " ;
} else {
$out .= " ' " . Inflector :: camelize ( $comp ) . " ' " ;
}
}
$out .= " ); \n " ;
}
2007-06-03 23:56:33 +00:00
$out .= $actions ;
2007-05-14 12:24:40 +00:00
}
$out .= " } \n " ;
$out .= " ?> " ;
$filename = CONTROLLERS . $this -> _controllerPath ( $controllerName ) . '_controller.php' ;
return $this -> createFile ( $filename , $out );
}
/**
2007-10-22 05:52:20 +00:00
* Assembles and writes a unit test file
2007-05-14 12:24:40 +00:00
*
2007-10-22 05:52:20 +00:00
* @ param string $className Controller class name
* @ return string Baked test
* @ access private
2007-05-14 12:24:40 +00:00
*/
function __bakeTest ( $className ) {
$out = '<?php ' . " \n \n " ;
$out .= " loadController(' $className '); \n \n " ;
2007-06-03 23:56:33 +00:00
$out .= " class { $className } ControllerTestCase extends CakeTestCase { \n " ;
2007-05-14 12:24:40 +00:00
$out .= " \t var \$ TestObject = null; \n \n " ;
$out .= " \t function setUp() { \n \t \t \$ this->TestObject = new { $className } Controller(); \n " ;
$out .= " \t } \n \n \t function tearDown() { \n \t \t unset( \$ this->TestObject); \n \t } \n " ;
$out .= " \n \t /* \n \t function testMe() { \n " ;
$out .= " \t \t \$ result = \$ this->TestObject->index(); \n " ;
$out .= " \t \t \$ expected = 1; \n " ;
$out .= " \t \t \$ this->assertEqual( \$ result, \$ expected); \n \t } \n \t */ \n } " ;
$out .= " \n ?> " ;
$path = CONTROLLER_TESTS ;
$filename = $this -> _pluralName ( $className ) . '_controller.test.php' ;
$this -> out ( " Baking unit test for $className ... " );
$Folder =& new Folder ( $path , true );
2007-06-20 06:15:35 +00:00
if ( $path = $Folder -> cd ( $path )) {
2007-05-14 12:24:40 +00:00
$path = $Folder -> slashTerm ( $path );
return $this -> createFile ( $path . $filename , $out );
}
return false ;
}
/**
2007-10-22 05:52:20 +00:00
* Outputs and gets the list of possible models or controllers from database
2007-05-14 12:24:40 +00:00
*
2007-10-22 05:52:20 +00:00
* @ param string $useDbConfig Database configuration name
* @ return array Set of controllers
* @ access public
2007-05-14 12:24:40 +00:00
*/
2007-06-01 17:34:42 +00:00
function listAll ( $useDbConfig = 'default' ) {
2007-05-14 12:24:40 +00:00
$db =& ConnectionManager :: getDataSource ( $useDbConfig );
$usePrefix = empty ( $db -> config [ 'prefix' ]) ? '' : $db -> config [ 'prefix' ];
if ( $usePrefix ) {
$tables = array ();
foreach ( $db -> listSources () as $table ) {
if ( ! strncmp ( $table , $usePrefix , strlen ( $usePrefix ))) {
$tables [] = substr ( $table , strlen ( $usePrefix ));
}
}
} else {
$tables = $db -> listSources ();
}
$this -> __tables = $tables ;
$this -> out ( 'Possible Models based on your current database:' );
$this -> _controllerNames = array ();
$count = count ( $tables );
for ( $i = 0 ; $i < $count ; $i ++ ) {
$this -> _controllerNames [] = $this -> _controllerName ( $this -> _modelName ( $tables [ $i ]));
$this -> out ( $i + 1 . " . " . $this -> _controllerNames [ $i ]);
}
2007-06-03 23:56:33 +00:00
return $this -> _controllerNames ;
2007-05-26 09:00:54 +00:00
}
/**
* Forces the user to specify the controller he wants to bake , and returns the selected controller name .
*
2007-10-22 05:52:20 +00:00
* @ return string Controller name
* @ access public
2007-05-26 09:00:54 +00:00
*/
2007-06-03 23:56:33 +00:00
function getName () {
2007-05-26 09:00:54 +00:00
$useDbConfig = 'default' ;
2007-06-03 23:56:33 +00:00
$controllers = $this -> listAll ( $useDbConfig , 'Controllers' );
2007-05-26 09:00:54 +00:00
$enteredController = '' ;
while ( $enteredController == '' ) {
$enteredController = $this -> in ( 'Enter a number from the list above, or type in the name of another controller.' );
2007-06-03 23:56:33 +00:00
if ( $enteredController == '' || intval ( $enteredController ) > count ( $controllers )) {
2007-05-26 09:00:54 +00:00
$this -> out ( 'Error:' );
$this -> out ( " The Controller name you supplied was empty, or the number \n you selected was not an option. Please try again. " );
$enteredController = '' ;
}
}
2007-06-03 23:56:33 +00:00
if ( intval ( $enteredController ) > 0 && intval ( $enteredController ) <= count ( $controllers ) ) {
$controllerName = $controllers [ intval ( $enteredController ) - 1 ];
2007-05-26 09:00:54 +00:00
} else {
$controllerName = Inflector :: camelize ( $enteredController );
}
2007-06-03 23:56:33 +00:00
2007-05-26 09:00:54 +00:00
return $controllerName ;
}
2007-06-08 08:44:34 +00:00
/**
* Displays help contents
*
2007-10-22 05:52:20 +00:00
* @ access public
2007-06-08 08:44:34 +00:00
*/
function help () {
$this -> hr ();
$this -> out ( " Usage: cake bake controller <arg1> <arg2>... " );
$this -> hr ();
$this -> out ( 'Commands:' );
$this -> out ( " \n \t controller <name> \n \t \t bakes controller with var \$ scaffold " );
$this -> out ( " \n \t controller <name> scaffold \n \t \t bakes controller with scaffold actions. \n \t \t (index, view, add, edit, delete) " );
2007-08-25 15:29:44 +00:00
$this -> out ( " \n \t controller <name> scaffold admin \n \t \t bakes a controller with scaffold actions for both public and Configure::read('Routing.admin') " );
$this -> out ( " \n \t controller <name> admin \n \t \t bakes a controller with scaffold actions only for Configure::read('Routing.admin') " );
2007-06-08 08:44:34 +00:00
$this -> out ( " " );
exit ();
2007-06-21 14:38:46 +00:00
}
}
2007-08-25 18:00:19 +00:00
?>