2008-05-30 11:40:08 +00:00
< ? php
/* SVN FILE: $Id$ */
/**
* The ControllerTask handles creating and updating controller files .
*
* 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 )
2008-05-30 11:40:08 +00:00
* Copyright 2005 - 2008 , Cake Software Foundation , Inc .
*
* 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 . 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
2008-05-30 11:40:08 +00:00
*/
/**
* Task class for creating and updating controller files .
*
2008-10-30 17:30:26 +00:00
* @ package cake
* @ subpackage cake . cake . console . libs . tasks
2008-05-30 11:40:08 +00:00
*/
class ControllerTask extends Shell {
/**
* Name of plugin
*
* @ var string
* @ access public
*/
var $plugin = null ;
/**
* Tasks to be loaded by this Task
*
* @ var array
* @ access public
*/
var $tasks = array ( 'Project' );
/**
* path to CONTROLLERS directory
*
* @ var array
* @ access public
*/
var $path = CONTROLLERS ;
/**
* Override initialize
*
* @ access public
*/
function initialize () {
}
/**
* Execution method always used for tasks
*
* @ access public
*/
function execute () {
if ( empty ( $this -> args )) {
$this -> __interactive ();
}
if ( isset ( $this -> args [ 0 ])) {
$controller = Inflector :: camelize ( $this -> args [ 0 ]);
$actions = null ;
if ( isset ( $this -> args [ 1 ]) && $this -> args [ 1 ] == 'scaffold' ) {
$this -> out ( 'Baking scaffold for ' . $controller );
$actions = $this -> bakeActions ( $controller );
} else {
$actions = 'scaffold' ;
}
if (( isset ( $this -> args [ 1 ]) && $this -> args [ 1 ] == 'admin' ) || ( isset ( $this -> args [ 2 ]) && $this -> args [ 2 ] == 'admin' )) {
if ( $admin = $this -> getAdmin ()) {
$this -> out ( 'Adding ' . Configure :: read ( 'Routing.admin' ) . ' methods' );
if ( $actions == 'scaffold' ) {
$actions = $this -> bakeActions ( $controller , $admin );
} else {
$actions .= $this -> bakeActions ( $controller , $admin );
}
}
}
if ( $this -> bake ( $controller , $actions )) {
if ( $this -> _checkUnitTest ()) {
$this -> bakeTest ( $controller );
}
}
}
}
/**
* Interactive
*
* @ access private
*/
function __interactive ( $controllerName = false ) {
if ( ! $controllerName ) {
$this -> interactive = true ;
$this -> hr ();
$this -> out ( sprintf ( " Bake Controller \n Path: %s " , $this -> path ));
$this -> hr ();
$actions = '' ;
$uses = array ();
$helpers = array ();
$components = array ();
$wannaUseSession = 'y' ;
$wannaDoAdmin = 'n' ;
$wannaUseScaffold = 'n' ;
$wannaDoScaffolding = 'y' ;
$controllerName = $this -> getName ();
}
$this -> hr ();
$this -> out ( " Baking { $controllerName } Controller " );
$this -> hr ();
2009-07-23 21:04:40 +00:00
$controllerFile = strtolower ( Inflector :: underscore ( $controllerName ));
2008-05-30 11:40:08 +00:00
$question [] = __ ( " Would you like to build your controller interactively? " , true );
if ( file_exists ( $this -> path . $controllerFile . '_controller.php' )) {
$question [] = sprintf ( __ ( " Warning: Choosing no will overwrite the %sController. " , true ), $controllerName );
}
2009-11-19 22:13:35 +00:00
$doItInteractive = $this -> in ( implode ( " \n " , $question ), array ( 'y' , 'n' ), 'y' );
2008-05-30 11:40:08 +00:00
2009-07-23 21:04:40 +00:00
if ( strtolower ( $doItInteractive ) == 'y' || strtolower ( $doItInteractive ) == 'yes' ) {
2008-05-30 11:40:08 +00:00
$this -> interactive = true ;
$wannaUseScaffold = $this -> in ( __ ( " Would you like to use scaffolding? " , true ), array ( 'y' , 'n' ), 'n' );
2009-07-23 21:04:40 +00:00
if ( strtolower ( $wannaUseScaffold ) == 'n' || strtolower ( $wannaUseScaffold ) == 'no' ) {
2008-05-30 11:40:08 +00:00
$wannaDoScaffolding = $this -> in ( __ ( " Would you like to include some basic class methods (index(), add(), view(), edit())? " , true ), array ( 'y' , 'n' ), 'n' );
2009-07-23 21:04:40 +00:00
if ( strtolower ( $wannaDoScaffolding ) == 'y' || strtolower ( $wannaDoScaffolding ) == 'yes' ) {
2008-05-30 11:40:08 +00:00
$wannaDoAdmin = $this -> in ( __ ( " Would you like to create the methods for admin routing? " , true ), array ( 'y' , 'n' ), 'n' );
}
$wannaDoHelpers = $this -> in ( __ ( " Would you like this controller to use other helpers besides HtmlHelper and FormHelper? " , true ), array ( 'y' , 'n' ), 'n' );
2009-07-23 21:04:40 +00:00
if ( strtolower ( $wannaDoHelpers ) == 'y' || strtolower ( $wannaDoHelpers ) == 'yes' ) {
2008-05-30 11:40:08 +00:00
$helpersList = $this -> in ( __ ( " Please provide a comma separated list of the other helper names you'd like to use. \n Example: 'Ajax, Javascript, Time' " , true ));
$helpersListTrimmed = str_replace ( ' ' , '' , $helpersList );
$helpers = explode ( ',' , $helpersListTrimmed );
}
$wannaDoComponents = $this -> in ( __ ( " Would you like this controller to use any components? " , true ), array ( 'y' , 'n' ), 'n' );
2009-07-23 21:04:40 +00:00
if ( strtolower ( $wannaDoComponents ) == 'y' || strtolower ( $wannaDoComponents ) == 'yes' ) {
2008-05-30 11:40:08 +00:00
$componentsList = $this -> in ( __ ( " Please provide a comma separated list of the component names you'd like to use. \n Example: 'Acl, Security, RequestHandler' " , true ));
$componentsListTrimmed = str_replace ( ' ' , '' , $componentsList );
$components = explode ( ',' , $componentsListTrimmed );
}
$wannaUseSession = $this -> in ( __ ( " Would you like to use Sessions? " , true ), array ( 'y' , 'n' ), 'y' );
} else {
$wannaDoScaffolding = 'n' ;
}
} else {
$wannaDoScaffolding = $this -> in ( __ ( " Would you like to include some basic class methods (index(), add(), view(), edit())? " , true ), array ( 'y' , 'n' ), 'y' );
2009-07-23 21:04:40 +00:00
if ( strtolower ( $wannaDoScaffolding ) == 'y' || strtolower ( $wannaDoScaffolding ) == 'yes' ) {
2008-05-30 11:40:08 +00:00
$wannaDoAdmin = $this -> in ( __ ( " Would you like to create the methods for admin routing? " , true ), array ( 'y' , 'n' ), 'y' );
}
}
$admin = false ;
2009-07-23 21:04:40 +00:00
if (( strtolower ( $wannaDoAdmin ) == 'y' || strtolower ( $wannaDoAdmin ) == 'yes' )) {
2008-05-30 11:40:08 +00:00
$admin = $this -> getAdmin ();
}
2009-07-23 21:04:40 +00:00
if ( strtolower ( $wannaDoScaffolding ) == 'y' || strtolower ( $wannaDoScaffolding ) == 'yes' ) {
$actions = $this -> bakeActions ( $controllerName , null , in_array ( strtolower ( $wannaUseSession ), array ( 'y' , 'yes' )));
2008-05-30 11:40:08 +00:00
if ( $admin ) {
2009-07-23 21:04:40 +00:00
$actions .= $this -> bakeActions ( $controllerName , $admin , in_array ( strtolower ( $wannaUseSession ), array ( 'y' , 'yes' )));
2008-05-30 11:40:08 +00:00
}
}
if ( $this -> interactive === true ) {
$this -> out ( '' );
$this -> hr ();
$this -> out ( 'The following controller will be created:' );
$this -> hr ();
$this -> out ( " Controller Name: $controllerName " );
2009-07-23 21:04:40 +00:00
if ( strtolower ( $wannaUseScaffold ) == 'y' || strtolower ( $wannaUseScaffold ) == 'yes' ) {
2008-05-30 11:40:08 +00:00
$this -> out ( " var \$ scaffold; " );
$actions = 'scaffold' ;
}
if ( count ( $helpers )) {
$this -> out ( " Helpers: " , false );
foreach ( $helpers as $help ) {
if ( $help != $helpers [ count ( $helpers ) - 1 ]) {
$this -> out ( ucfirst ( $help ) . " , " , false );
} else {
$this -> out ( ucfirst ( $help ));
}
}
}
if ( count ( $components )) {
$this -> out ( " Components: " , false );
foreach ( $components as $comp ) {
if ( $comp != $components [ count ( $components ) - 1 ]) {
$this -> out ( ucfirst ( $comp ) . " , " , false );
} else {
$this -> out ( ucfirst ( $comp ));
}
}
}
$this -> hr ();
$looksGood = $this -> in ( __ ( 'Look okay?' , true ), array ( 'y' , 'n' ), 'y' );
2009-07-23 21:04:40 +00:00
if ( strtolower ( $looksGood ) == 'y' || strtolower ( $looksGood ) == 'yes' ) {
2008-05-30 11:40:08 +00:00
$baked = $this -> bake ( $controllerName , $actions , $helpers , $components , $uses );
if ( $baked && $this -> _checkUnitTest ()) {
$this -> bakeTest ( $controllerName );
}
} else {
$this -> __interactive ( $controllerName );
}
} else {
$baked = $this -> bake ( $controllerName , $actions , $helpers , $components , $uses );
if ( $baked && $this -> _checkUnitTest ()) {
$this -> bakeTest ( $controllerName );
}
}
}
/**
* Bake scaffold actions
*
* @ param string $controllerName Controller name
* @ param string $admin Admin route to use
* @ param boolean $wannaUseSession Set to true to use sessions , false otherwise
* @ return string Baked actions
* @ access private
*/
function bakeActions ( $controllerName , $admin = null , $wannaUseSession = true ) {
2009-06-17 02:17:18 +00:00
$currentModelName = $modelImport = $this -> _modelName ( $controllerName );
if ( $this -> plugin ) {
$modelImport = $this -> plugin . '.' . $modelImport ;
}
if ( ! App :: import ( 'Model' , $modelImport )) {
2008-05-30 11:40:08 +00:00
$this -> err ( __ ( 'You must have a model for this class to build scaffold methods. Please try again.' , true ));
exit ;
}
$actions = null ;
2009-11-12 14:32:45 +00:00
$modelObj =& ClassRegistry :: init ( $currentModelName );
2008-05-30 11:40:08 +00:00
$controllerPath = $this -> _controllerPath ( $controllerName );
$pluralName = $this -> _pluralName ( $currentModelName );
$singularName = Inflector :: variable ( $currentModelName );
$singularHumanName = Inflector :: humanize ( $currentModelName );
$pluralHumanName = Inflector :: humanize ( $controllerName );
$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 " ;
$actions .= " \t \t if (! \$ id) { \n " ;
if ( $wannaUseSession ) {
2009-10-30 20:30:11 +00:00
$actions .= " \t \t \t \$ this->Session->setFlash(__('Invalid { $singularHumanName } ', true)); \n " ;
$actions .= " \t \t \t \$ this->redirect(array('action' => 'index')); \n " ;
2008-05-30 11:40:08 +00:00
} else {
2009-10-30 20:30:11 +00:00
$actions .= " \t \t \t \$ this->flash(__('Invalid { $singularHumanName } ', true), array('action' => 'index')); \n " ;
2008-05-30 11:40:08 +00:00
}
$actions .= " \t \t } \n " ;
2009-07-28 20:01:42 +00:00
$actions .= " \t \t \$ this->set(' " . $singularName . " ', \$ this-> { $currentModelName } ->read(null, \$ id)); \n " ;
2008-05-30 11:40:08 +00:00
$actions .= " \t } \n " ;
$actions .= " \n " ;
/* ADD ACTION */
$compact = array ();
$actions .= " \t function { $admin } add() { \n " ;
$actions .= " \t \t if (!empty( \$ this->data)) { \n " ;
$actions .= " \t \t \t \$ this-> { $currentModelName } ->create(); \n " ;
$actions .= " \t \t \t if ( \$ this-> { $currentModelName } ->save( \$ this->data)) { \n " ;
if ( $wannaUseSession ) {
2009-07-28 20:01:42 +00:00
$actions .= " \t \t \t \t \$ this->Session->setFlash(__('The " . $singularHumanName . " has been saved', true)); \n " ;
2009-10-30 20:30:11 +00:00
$actions .= " \t \t \t \t \$ this->redirect(array('action' => 'index')); \n " ;
2008-05-30 11:40:08 +00:00
} else {
2009-10-30 20:30:11 +00:00
$actions .= " \t \t \t \t \$ this->flash(__(' { $currentModelName } saved.', true), array('action' => 'index')); \n " ;
2008-05-30 11:40:08 +00:00
}
$actions .= " \t \t \t } else { \n " ;
if ( $wannaUseSession ) {
$actions .= " \t \t \t \t \$ this->Session->setFlash(__('The { $singularHumanName } could not be saved. Please, try again.', true)); \n " ;
}
$actions .= " \t \t \t } \n " ;
$actions .= " \t \t } \n " ;
foreach ( $modelObj -> hasAndBelongsToMany as $associationName => $relation ) {
if ( ! empty ( $associationName )) {
$habtmModelName = $this -> _modelName ( $associationName );
$habtmSingularName = $this -> _singularName ( $associationName );
$habtmPluralName = $this -> _pluralName ( $associationName );
$actions .= " \t \t \$ { $habtmPluralName } = \$ this-> { $currentModelName } -> { $habtmModelName } ->find('list'); \n " ;
$compact [] = " ' { $habtmPluralName } ' " ;
}
}
foreach ( $modelObj -> belongsTo as $associationName => $relation ) {
if ( ! empty ( $associationName )) {
$belongsToModelName = $this -> _modelName ( $associationName );
$belongsToPluralName = $this -> _pluralName ( $associationName );
$actions .= " \t \t \$ { $belongsToPluralName } = \$ this-> { $currentModelName } -> { $belongsToModelName } ->find('list'); \n " ;
$compact [] = " ' { $belongsToPluralName } ' " ;
}
}
if ( ! empty ( $compact )) {
2009-11-19 22:13:35 +00:00
$actions .= " \t \t \$ this->set(compact( " . implode ( ', ' , $compact ) . " )); \n " ;
2008-05-30 11:40:08 +00:00
}
$actions .= " \t } \n " ;
$actions .= " \n " ;
/* EDIT ACTION */
$compact = array ();
$actions .= " \t function { $admin } edit( \$ id = null) { \n " ;
$actions .= " \t \t if (! \$ id && empty( \$ this->data)) { \n " ;
if ( $wannaUseSession ) {
$actions .= " \t \t \t \$ this->Session->setFlash(__('Invalid { $singularHumanName } ', true)); \n " ;
2009-10-30 20:30:11 +00:00
$actions .= " \t \t \t \$ this->redirect(array('action' => 'index')); \n " ;
2008-05-30 11:40:08 +00:00
} else {
2009-10-30 20:30:11 +00:00
$actions .= " \t \t \t \$ this->flash(__('Invalid { $singularHumanName } ', true), array('action' => 'index')); \n " ;
2008-05-30 11:40:08 +00:00
}
$actions .= " \t \t } \n " ;
$actions .= " \t \t if (!empty( \$ this->data)) { \n " ;
$actions .= " \t \t \t if ( \$ this-> { $currentModelName } ->save( \$ this->data)) { \n " ;
if ( $wannaUseSession ) {
2009-07-28 20:01:42 +00:00
$actions .= " \t \t \t \t \$ this->Session->setFlash(__('The " . $singularHumanName . " has been saved', true)); \n " ;
2009-10-30 20:30:11 +00:00
$actions .= " \t \t \t \t \$ this->redirect(array('action' => 'index')); \n " ;
2008-05-30 11:40:08 +00:00
} else {
2009-10-30 20:30:11 +00:00
$actions .= " \t \t \t \t \$ this->flash(__('The " . $singularHumanName . " has been saved.', true), array('action' => 'index')); \n " ;
2008-05-30 11:40:08 +00:00
}
$actions .= " \t \t \t } else { \n " ;
if ( $wannaUseSession ) {
$actions .= " \t \t \t \t \$ this->Session->setFlash(__('The { $singularHumanName } could not be saved. Please, try again.', true)); \n " ;
}
$actions .= " \t \t \t } \n " ;
$actions .= " \t \t } \n " ;
$actions .= " \t \t if (empty( \$ this->data)) { \n " ;
$actions .= " \t \t \t \$ this->data = \$ this-> { $currentModelName } ->read(null, \$ id); \n " ;
$actions .= " \t \t } \n " ;
foreach ( $modelObj -> hasAndBelongsToMany as $associationName => $relation ) {
if ( ! empty ( $associationName )) {
$habtmModelName = $this -> _modelName ( $associationName );
$habtmSingularName = $this -> _singularName ( $associationName );
$habtmPluralName = $this -> _pluralName ( $associationName );
$actions .= " \t \t \$ { $habtmPluralName } = \$ this-> { $currentModelName } -> { $habtmModelName } ->find('list'); \n " ;
$compact [] = " ' { $habtmPluralName } ' " ;
}
}
foreach ( $modelObj -> belongsTo as $associationName => $relation ) {
if ( ! empty ( $associationName )) {
$belongsToModelName = $this -> _modelName ( $associationName );
$belongsToPluralName = $this -> _pluralName ( $associationName );
$actions .= " \t \t \$ { $belongsToPluralName } = \$ this-> { $currentModelName } -> { $belongsToModelName } ->find('list'); \n " ;
$compact [] = " ' { $belongsToPluralName } ' " ;
}
}
if ( ! empty ( $compact )) {
2009-11-19 22:13:35 +00:00
$actions .= " \t \t \$ this->set(compact( " . implode ( ',' , $compact ) . " )); \n " ;
2008-05-30 11:40:08 +00:00
}
$actions .= " \t } \n " ;
$actions .= " \n " ;
$actions .= " \t function { $admin } delete( \$ id = null) { \n " ;
$actions .= " \t \t if (! \$ id) { \n " ;
if ( $wannaUseSession ) {
$actions .= " \t \t \t \$ this->Session->setFlash(__('Invalid id for { $singularHumanName } ', true)); \n " ;
2009-10-30 20:30:11 +00:00
$actions .= " \t \t \t \$ this->redirect(array('action' => 'index')); \n " ;
2008-05-30 11:40:08 +00:00
} else {
2009-10-30 20:30:11 +00:00
$actions .= " \t \t \t \$ this->flash(__('Invalid { $singularHumanName } ', true), array('action' => 'index')); \n " ;
2008-05-30 11:40:08 +00:00
}
$actions .= " \t \t } \n " ;
$actions .= " \t \t if ( \$ this-> { $currentModelName } ->del( \$ id)) { \n " ;
if ( $wannaUseSession ) {
$actions .= " \t \t \t \$ this->Session->setFlash(__(' { $singularHumanName } deleted', true)); \n " ;
2009-10-30 20:30:11 +00:00
$actions .= " \t \t \t \$ this->redirect(array('action' => 'index')); \n " ;
2008-05-30 11:40:08 +00:00
} else {
2009-10-30 20:30:11 +00:00
$actions .= " \t \t \t \$ this->flash(__(' { $singularHumanName } deleted', true), array('action' => 'index')); \n " ;
2008-05-30 11:40:08 +00:00
}
$actions .= " \t \t } \n " ;
2010-01-11 15:46:21 +00:00
if ( $wannaUseSession ) {
$actions .= " \t \t \$ this->Session->setFlash(__('The { $singularHumanName } could not be deleted. Please, try again.', true)); \n " ;
$actions .= " \t \t \$ this->redirect(array('action' => 'index')); \n " ;
} else {
$actions .= " \t \t \$ this->flash(__('The { $singularHumanName } could not be deleted. Please, try again.', true), array('action' => 'index')); \n " ;
}
2008-05-30 11:40:08 +00:00
$actions .= " \t } \n " ;
$actions .= " \n " ;
return $actions ;
}
/**
* Assembles and writes a Controller file
*
* @ 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
*/
function bake ( $controllerName , $actions = '' , $helpers = null , $components = null , $uses = null ) {
$out = " <?php \n " ;
$out .= " class $controllerName " . " Controller extends { $this -> plugin } AppController { \n \n " ;
$out .= " \t var \$ name = ' $controllerName '; \n " ;
2009-07-23 21:04:40 +00:00
if ( strtolower ( $actions ) == 'scaffold' ) {
2008-05-30 11:40:08 +00:00
$out .= " \t var \$ scaffold; \n " ;
} else {
if ( count ( $uses )) {
$out .= " \t var \$ uses = array(' " . $this -> _modelName ( $controllerName ) . " ', " ;
foreach ( $uses as $use ) {
if ( $use != $uses [ count ( $uses ) - 1 ]) {
$out .= " ' " . $this -> _modelName ( $use ) . " ', " ;
} else {
$out .= " ' " . $this -> _modelName ( $use ) . " ' " ;
}
}
$out .= " ); \n " ;
}
$out .= " \t var \$ helpers = array('Html', 'Form' " ;
if ( count ( $helpers )) {
foreach ( $helpers as $help ) {
$out .= " , ' " . Inflector :: camelize ( $help ) . " ' " ;
}
}
$out .= " ); \n " ;
if ( count ( $components )) {
$out .= " \t var \$ components = array( " ;
foreach ( $components as $comp ) {
if ( $comp != $components [ count ( $components ) - 1 ]) {
$out .= " ' " . Inflector :: camelize ( $comp ) . " ', " ;
} else {
$out .= " ' " . Inflector :: camelize ( $comp ) . " ' " ;
}
}
$out .= " ); \n " ;
}
$out .= $actions ;
}
$out .= " } \n " ;
$out .= " ?> " ;
$filename = $this -> path . $this -> _controllerPath ( $controllerName ) . '_controller.php' ;
return $this -> createFile ( $filename , $out );
}
/**
* Assembles and writes a unit test file
*
* @ param string $className Controller class name
* @ return string Baked test
* @ access private
*/
function bakeTest ( $className ) {
$import = $className ;
if ( $this -> plugin ) {
$import = $this -> plugin . '.' . $className ;
}
$out = " App::import('Controller', ' $import '); \n \n " ;
$out .= " class Test { $className } extends { $className } Controller { \n " ;
$out .= " \t var \$ autoRender = false; \n } \n \n " ;
$out .= " class { $className } ControllerTest extends CakeTestCase { \n " ;
$out .= " \t var \$ { $className } = null; \n \n " ;
2009-04-14 23:13:43 +00:00
$out .= " \t function startTest() { \n \t \t \$ this-> { $className } = new Test { $className } (); " ;
2008-06-30 12:36:20 +00:00
$out .= " \n \t \t \$ this-> { $className } ->constructClasses(); \n \t } \n \n " ;
2008-05-30 11:40:08 +00:00
$out .= " \t function test { $className } ControllerInstance() { \n " ;
$out .= " \t \t \$ this->assertTrue(is_a( \$ this-> { $className } , ' { $className } Controller')); \n \t } \n \n " ;
2009-04-14 23:13:43 +00:00
$out .= " \t function endTest() { \n \t \t unset( \$ this-> { $className } ); \n \t } \n } \n " ;
2008-05-30 11:40:08 +00:00
$path = CONTROLLER_TESTS ;
if ( isset ( $this -> plugin )) {
$pluginPath = 'plugins' . DS . Inflector :: underscore ( $this -> plugin ) . DS ;
$path = APP . $pluginPath . 'tests' . DS . 'cases' . DS . 'controllers' . DS ;
}
$filename = Inflector :: underscore ( $className ) . '_controller.test.php' ;
$this -> out ( " \n Baking unit test for $className ... " );
$header = '$Id' ;
2009-07-28 20:01:42 +00:00
$content = " <?php \n /* SVN FILE: $header $ */ \n /* " . $className . " Controller Test cases generated on: " . date ( 'Y-m-d H:i:s' ) . " : " . time () . " */ \n { $out } ?> " ;
2008-05-30 11:40:08 +00:00
return $this -> createFile ( $path . $filename , $content );
}
/**
* Outputs and gets the list of possible models or controllers from database
*
* @ param string $useDbConfig Database configuration name
* @ return array Set of controllers
* @ access public
*/
function listAll ( $useDbConfig = 'default' ) {
$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 ();
}
if ( empty ( $tables )) {
$this -> err ( __ ( 'Your database does not have any tables.' , true ));
2008-06-04 19:04:58 +00:00
$this -> _stop ();
2008-05-30 11:40:08 +00:00
}
$this -> __tables = $tables ;
$this -> out ( 'Possible Controllers 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 ]);
}
return $this -> _controllerNames ;
}
/**
* Forces the user to specify the controller he wants to bake , and returns the selected controller name .
*
* @ return string Controller name
* @ access public
*/
function getName () {
$useDbConfig = 'default' ;
$controllers = $this -> listAll ( $useDbConfig , 'Controllers' );
$enteredController = '' ;
while ( $enteredController == '' ) {
$enteredController = $this -> in ( __ ( " Enter a number from the list above, type in the name of another controller, or 'q' to exit " , true ), null , 'q' );
if ( $enteredController === 'q' ) {
$this -> out ( __ ( " Exit " , true ));
2008-06-04 19:04:58 +00:00
$this -> _stop ();
2008-05-30 11:40:08 +00:00
}
if ( $enteredController == '' || intval ( $enteredController ) > count ( $controllers )) {
$this -> out ( __ ( 'Error:' , true ));
$this -> out ( __ ( " The Controller name you supplied was empty, or the number \n you selected was not an option. Please try again. " , true ));
$enteredController = '' ;
}
}
if ( intval ( $enteredController ) > 0 && intval ( $enteredController ) <= count ( $controllers ) ) {
$controllerName = $controllers [ intval ( $enteredController ) - 1 ];
} else {
$controllerName = Inflector :: camelize ( $enteredController );
}
return $controllerName ;
}
/**
* Displays help contents
*
* @ access public
*/
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) " );
$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') " );
$this -> out ( " " );
2008-06-04 19:04:58 +00:00
$this -> _stop ();
2008-05-30 11:40:08 +00:00
}
}
?>