2008-05-30 11:40:08 +00:00
< ? php
/**
* The ControllerTask handles creating and updating controller files .
*
2010-10-03 16:38:58 +00:00
* PHP 5
2008-05-30 11:40:08 +00:00
*
2009-11-06 06:46:59 +00:00
* CakePHP ( tm ) : Rapid Development Framework ( http :// cakephp . org )
2011-05-29 21:31:39 +00:00
* Copyright 2005 - 2011 , Cake Software Foundation , Inc .
2008-05-30 11:40:08 +00:00
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice .
*
2011-05-29 21:31:39 +00:00
* @ copyright Copyright 2005 - 2011 , Cake Software Foundation , Inc . ( http :// cakefoundation . org )
2009-11-06 06:00:11 +00:00
* @ link http :// cakephp . org CakePHP ( tm ) Project
2008-10-30 17:30:26 +00:00
* @ since CakePHP ( tm ) v 1.2
2009-11-06 06:51:51 +00:00
* @ license MIT License ( http :// www . opensource . org / licenses / mit - license . php )
2008-05-30 11:40:08 +00:00
*/
2009-07-24 19:18:37 +00:00
2010-12-08 00:20:56 +00:00
App :: uses ( 'BakeTask' , 'Console/Command/Task' );
2011-05-01 18:58:32 +00:00
App :: uses ( 'AppModel' , 'Model' );
2010-03-05 01:52:25 +00:00
2008-05-30 11:40:08 +00:00
/**
* Task class for creating and updating controller files .
*
2011-07-26 06:16:14 +00:00
* @ package Cake . Console . Command . Task
2008-05-30 11:40:08 +00:00
*/
2010-03-05 01:52:25 +00:00
class ControllerTask extends BakeTask {
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Tasks to be loaded by this Task
*
* @ var array
*/
2010-04-04 07:14:00 +00:00
public $tasks = array ( 'Model' , 'Test' , 'Template' , 'DbConfig' , 'Project' );
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
2011-04-17 09:13:04 +00:00
* path to Controller directory
2008-05-30 11:40:08 +00:00
*
* @ var array
*/
2011-04-17 09:13:04 +00:00
public $path = null ;
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Override initialize
*
2011-07-29 02:03:44 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
*/
2010-04-05 03:19:38 +00:00
public function initialize () {
2011-04-17 11:28:45 +00:00
$this -> path = current ( App :: path ( 'Controller' ));
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Execution method always used for tasks
*
2011-07-29 02:03:44 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
*/
2010-04-05 03:19:38 +00:00
public function execute () {
2010-10-19 03:09:23 +00:00
parent :: execute ();
2008-05-30 11:40:08 +00:00
if ( empty ( $this -> args )) {
2010-05-05 03:27:41 +00:00
return $this -> _interactive ();
2008-05-30 11:40:08 +00:00
}
if ( isset ( $this -> args [ 0 ])) {
2009-05-15 04:04:57 +00:00
if ( ! isset ( $this -> connection )) {
$this -> connection = 'default' ;
}
2009-04-28 03:36:57 +00:00
if ( strtolower ( $this -> args [ 0 ]) == 'all' ) {
return $this -> all ();
}
2009-07-29 16:47:25 +00:00
2010-03-06 02:30:58 +00:00
$controller = $this -> _controllerName ( $this -> args [ 0 ]);
2010-10-13 03:23:18 +00:00
$actions = '' ;
2009-07-29 16:47:25 +00:00
2010-10-13 03:23:18 +00:00
if ( ! empty ( $this -> params [ 'public' ])) {
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Baking basic crud methods for ' ) . $controller );
2010-10-13 03:23:18 +00:00
$actions .= $this -> bakeActions ( $controller );
2009-07-29 16:47:25 +00:00
}
2010-10-13 03:23:18 +00:00
if ( ! empty ( $this -> params [ 'admin' ])) {
2009-10-07 04:46:13 +00:00
$admin = $this -> Project -> getPrefix ();
2009-07-29 16:47:25 +00:00
if ( $admin ) {
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Adding %s methods' , $admin ));
2009-07-29 16:47:25 +00:00
$actions .= " \n " . $this -> bakeActions ( $controller , $admin );
2008-05-30 11:40:08 +00:00
}
}
2010-10-13 03:23:18 +00:00
if ( empty ( $actions )) {
$actions = 'scaffold' ;
}
2009-07-29 16:47:25 +00:00
2008-05-30 11:40:08 +00:00
if ( $this -> bake ( $controller , $actions )) {
if ( $this -> _checkUnitTest ()) {
$this -> bakeTest ( $controller );
}
}
}
}
2009-07-24 19:18:37 +00:00
2009-04-28 03:36:57 +00:00
/**
* Bake All the controllers at once . Will only bake controllers for models that exist .
*
* @ return void
2009-11-14 12:19:25 +00:00
*/
2010-04-24 01:57:59 +00:00
public function all () {
2009-04-28 03:58:36 +00:00
$this -> interactive = false ;
2009-05-15 03:17:42 +00:00
$this -> listAll ( $this -> connection , false );
2009-05-17 04:31:37 +00:00
ClassRegistry :: config ( 'Model' , array ( 'ds' => $this -> connection ));
2009-07-15 14:14:16 +00:00
$unitTestExists = $this -> _checkUnitTest ();
2009-04-28 03:36:57 +00:00
foreach ( $this -> __tables as $table ) {
$model = $this -> _modelName ( $table );
$controller = $this -> _controllerName ( $model );
2010-12-09 03:45:18 +00:00
App :: uses ( $model , 'Model' );
2011-03-08 05:55:01 +00:00
if ( class_exists ( $model )) {
2009-04-28 03:36:57 +00:00
$actions = $this -> bakeActions ( $controller );
2009-07-15 14:14:16 +00:00
if ( $this -> bake ( $controller , $actions ) && $unitTestExists ) {
$this -> bakeTest ( $controller );
}
2009-04-28 03:36:57 +00:00
}
}
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Interactive
*
2010-04-24 01:57:59 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
*/
2010-04-24 01:57:59 +00:00
protected function _interactive () {
2009-05-15 04:04:57 +00:00
$this -> interactive = true ;
$this -> hr ();
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , " Bake Controller \n Path: %s " , $this -> path ));
2009-05-15 04:04:57 +00:00
$this -> hr ();
if ( empty ( $this -> connection )) {
$this -> connection = $this -> DbConfig -> getConfig ();
2008-05-30 11:40:08 +00:00
}
2009-05-15 04:04:57 +00:00
$controllerName = $this -> getName ();
2008-05-30 11:40:08 +00:00
$this -> hr ();
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Baking %sController' , $controllerName ));
2008-05-30 11:40:08 +00:00
$this -> hr ();
2009-05-19 02:26:16 +00:00
$helpers = $components = array ();
2009-05-15 04:04:57 +00:00
$actions = '' ;
$wannaUseSession = 'y' ;
2009-05-17 02:41:38 +00:00
$wannaBakeAdminCrud = 'n' ;
$useDynamicScaffold = 'n' ;
$wannaBakeCrud = 'y' ;
2009-05-15 04:04:57 +00:00
2008-05-30 11:40:08 +00:00
2011-03-19 17:32:35 +00:00
$question [] = __d ( 'cake_console' , " Would you like to build your controller interactively? " );
2011-03-16 02:23:51 +00:00
if ( file_exists ( $this -> path . $controllerName . 'Controller.php' )) {
2011-03-19 17:32:35 +00:00
$question [] = __d ( 'cake_console' , " Warning: Choosing no will overwrite the %sController. " , $controllerName );
2008-05-30 11:40:08 +00:00
}
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-05-15 03:33:10 +00:00
if ( strtolower ( $doItInteractive ) == 'y' ) {
2008-05-30 11:40:08 +00:00
$this -> interactive = true ;
2009-05-17 02:41:38 +00:00
$useDynamicScaffold = $this -> in (
2011-03-19 17:32:35 +00:00
__d ( 'cake_console' , " Would you like to use dynamic scaffolding? " ), array ( 'y' , 'n' ), 'n'
2009-05-17 02:41:38 +00:00
);
2008-05-30 11:40:08 +00:00
2009-05-17 04:05:07 +00:00
if ( strtolower ( $useDynamicScaffold ) == 'y' ) {
$wannaBakeCrud = 'n' ;
$actions = 'scaffold' ;
} else {
2009-05-20 04:00:36 +00:00
list ( $wannaBakeCrud , $wannaBakeAdminCrud ) = $this -> _askAboutMethods ();
2008-05-30 11:40:08 +00:00
2009-05-15 04:04:57 +00:00
$helpers = $this -> doHelpers ();
$components = $this -> doComponents ();
2008-05-30 11:40:08 +00:00
2009-05-17 03:17:40 +00:00
$wannaUseSession = $this -> in (
2011-03-19 17:32:35 +00:00
__d ( 'cake_console' , " Would you like to use Session flash messages? " ), array ( 'y' , 'n' ), 'y'
2009-05-17 03:17:40 +00:00
);
2008-05-30 11:40:08 +00:00
}
} else {
2010-05-05 03:27:41 +00:00
list ( $wannaBakeCrud , $wannaBakeAdminCrud ) = $this -> _askAboutMethods ();
2008-05-30 11:40:08 +00:00
}
2009-05-17 02:41:38 +00:00
if ( strtolower ( $wannaBakeCrud ) == 'y' ) {
$actions = $this -> bakeActions ( $controllerName , null , strtolower ( $wannaUseSession ) == 'y' );
2008-05-30 11:40:08 +00:00
}
2009-05-17 02:41:38 +00:00
if ( strtolower ( $wannaBakeAdminCrud ) == 'y' ) {
2009-10-07 04:46:13 +00:00
$admin = $this -> Project -> getPrefix ();
2009-05-17 02:41:38 +00:00
$actions .= $this -> bakeActions ( $controllerName , $admin , strtolower ( $wannaUseSession ) == 'y' );
2008-05-30 11:40:08 +00:00
}
2010-05-05 03:27:41 +00:00
$baked = false ;
2008-05-30 11:40:08 +00:00
if ( $this -> interactive === true ) {
2009-05-19 04:18:59 +00:00
$this -> confirmController ( $controllerName , $useDynamicScaffold , $helpers , $components );
2011-03-19 17:32:35 +00:00
$looksGood = $this -> in ( __d ( 'cake_console' , 'Look okay?' ), array ( 'y' , 'n' ), 'y' );
2008-05-30 11:40:08 +00:00
2009-05-15 03:33:10 +00:00
if ( strtolower ( $looksGood ) == 'y' ) {
2009-05-19 02:26:16 +00:00
$baked = $this -> bake ( $controllerName , $actions , $helpers , $components );
2008-05-30 11:40:08 +00:00
if ( $baked && $this -> _checkUnitTest ()) {
$this -> bakeTest ( $controllerName );
}
}
} else {
2009-05-19 02:26:16 +00:00
$baked = $this -> bake ( $controllerName , $actions , $helpers , $components );
2008-05-30 11:40:08 +00:00
if ( $baked && $this -> _checkUnitTest ()) {
$this -> bakeTest ( $controllerName );
}
}
2010-05-05 03:27:41 +00:00
return $baked ;
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2009-05-17 03:17:40 +00:00
/**
* Confirm a to be baked controller with the user
*
2011-07-29 02:03:44 +00:00
* @ param string $controllerName
* @ param string $useDynamicScaffold
* @ param array $helpers
* @ param array $components
2009-05-17 03:17:40 +00:00
* @ return void
2009-11-14 12:19:25 +00:00
*/
2010-04-24 01:57:59 +00:00
public function confirmController ( $controllerName , $useDynamicScaffold , $helpers , $components ) {
2009-09-26 21:08:37 +00:00
$this -> out ();
2009-05-17 03:17:40 +00:00
$this -> hr ();
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'The following controller will be created:' ));
2009-05-17 03:17:40 +00:00
$this -> hr ();
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , " Controller Name: \n \t %s " , $controllerName ));
2009-05-17 03:17:40 +00:00
if ( strtolower ( $useDynamicScaffold ) == 'y' ) {
2011-11-06 16:50:00 +00:00
$this -> out ( " public \$ scaffold; " );
2009-05-17 03:17:40 +00:00
}
$properties = array (
2011-03-19 17:32:35 +00:00
'helpers' => __d ( 'cake_console' , 'Helpers:' ),
'components' => __d ( 'cake_console' , 'Components:' ),
2009-05-17 03:17:40 +00:00
);
foreach ( $properties as $var => $title ) {
if ( count ( $$var )) {
$output = '' ;
$length = count ( $$var );
foreach ( $$var as $i => $propElement ) {
if ( $i != $length - 1 ) {
$output .= ucfirst ( $propElement ) . ', ' ;
} else {
$output .= ucfirst ( $propElement );
}
}
$this -> out ( $title . " \n \t " . $output );
}
}
$this -> hr ();
}
2009-07-24 19:18:37 +00:00
2009-05-17 02:41:38 +00:00
/**
* Interact with the user and ask about which methods ( admin or regular they want to bake )
*
* @ return array Array containing ( bakeRegular , bakeAdmin ) answers
2009-11-14 12:19:25 +00:00
*/
2010-04-24 01:57:59 +00:00
protected function _askAboutMethods () {
2009-05-17 02:41:38 +00:00
$wannaBakeCrud = $this -> in (
2011-03-19 17:32:35 +00:00
__d ( 'cake_console' , " Would you like to create some basic class methods \n (index(), add(), view(), edit())? " ),
2009-05-17 02:41:38 +00:00
array ( 'y' , 'n' ), 'n'
);
$wannaBakeAdminCrud = $this -> in (
2011-03-19 17:32:35 +00:00
__d ( 'cake_console' , " Would you like to create the basic class methods for admin routing? " ),
2009-05-17 02:41:38 +00:00
array ( 'y' , 'n' ), 'n'
);
return array ( $wannaBakeCrud , $wannaBakeAdminCrud );
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* 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
*/
2010-04-24 01:57:59 +00:00
public function bakeActions ( $controllerName , $admin = null , $wannaUseSession = true ) {
2009-06-17 02:17:18 +00:00
$currentModelName = $modelImport = $this -> _modelName ( $controllerName );
2010-09-03 16:33:59 +00:00
$plugin = $this -> plugin ;
if ( $plugin ) {
2011-03-08 05:55:01 +00:00
$plugin .= '.' ;
2009-06-17 02:17:18 +00:00
}
2011-03-08 05:55:01 +00:00
App :: uses ( $modelImport , $plugin . 'Model' );
2010-12-09 03:45:18 +00:00
if ( ! class_exists ( $modelImport )) {
2011-03-19 17:32:35 +00:00
$this -> err ( __d ( 'cake_console' , 'You must have a model for this class to build basic methods. Please try again.' ));
2009-05-17 04:31:37 +00:00
$this -> _stop ();
2008-05-30 11:40:08 +00:00
}
2009-05-20 03:53:41 +00:00
2010-11-13 04:05:44 +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 );
2010-05-11 03:58:22 +00:00
$singularHumanName = $this -> _singularHumanName ( $controllerName );
2009-08-18 08:29:54 +00:00
$pluralHumanName = $this -> _pluralName ( $controllerName );
2010-10-13 12:06:03 +00:00
$displayField = $modelObj -> displayField ;
$primaryKey = $modelObj -> primaryKey ;
2009-05-17 04:31:37 +00:00
2011-09-10 00:40:32 +00:00
$this -> Template -> set ( compact (
'plugin' , 'admin' , 'controllerPath' , 'pluralName' , 'singularName' ,
2011-03-16 11:29:39 +00:00
'singularHumanName' , 'pluralHumanName' , 'modelObj' , 'wannaUseSession' , 'currentModelName' ,
2010-10-13 12:06:03 +00:00
'displayField' , 'primaryKey'
));
2009-07-01 04:50:38 +00:00
$actions = $this -> Template -> generate ( 'actions' , 'controller_actions' );
2008-05-30 11:40:08 +00:00
return $actions ;
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Assembles and writes a Controller file
*
2011-10-05 00:10:25 +00:00
* @ param string $controllerName Controller name already pluralized and correctly cased .
2008-05-30 11:40:08 +00:00
* @ 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
* @ return string Baked controller
*/
2010-04-24 01:57:59 +00:00
public function bake ( $controllerName , $actions = '' , $helpers = null , $components = null ) {
2011-04-25 19:17:59 +00:00
$this -> out ( " \n " . __d ( 'cake_console' , 'Baking controller class for %s...' , $controllerName ), 1 , Shell :: QUIET );
2010-11-21 17:47:49 +00:00
2009-05-17 04:05:07 +00:00
$isScaffold = ( $actions === 'scaffold' ) ? true : false ;
2008-05-30 11:40:08 +00:00
2011-09-10 00:40:32 +00:00
$this -> Template -> set ( array (
'plugin' => $this -> plugin ,
'pluginPath' => empty ( $this -> plugin ) ? '' : $this -> plugin . '.'
));
2009-05-19 02:26:16 +00:00
$this -> Template -> set ( compact ( 'controllerName' , 'actions' , 'helpers' , 'components' , 'isScaffold' ));
2009-07-01 04:50:38 +00:00
$contents = $this -> Template -> generate ( 'classes' , 'controller' );
2008-05-30 11:40:08 +00:00
2010-03-05 01:52:25 +00:00
$path = $this -> getPath ();
2011-10-05 00:10:25 +00:00
$filename = $path . $controllerName . 'Controller.php' ;
2009-05-17 04:05:07 +00:00
if ( $this -> createFile ( $filename , $contents )) {
return $contents ;
}
return false ;
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Assembles and writes a unit test file
*
* @ param string $className Controller class name
* @ return string Baked test
*/
2010-04-24 01:57:59 +00:00
public function bakeTest ( $className ) {
2009-05-20 04:46:09 +00:00
$this -> Test -> plugin = $this -> plugin ;
$this -> Test -> connection = $this -> connection ;
2010-03-05 03:02:49 +00:00
$this -> Test -> interactive = $this -> interactive ;
2009-05-20 04:46:09 +00:00
return $this -> Test -> bake ( 'Controller' , $className );
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2009-05-15 04:04:57 +00:00
/**
* Interact with the user and get a list of additional helpers
*
* @ return array Helpers that the user wants to use .
2009-11-14 12:19:25 +00:00
*/
2010-04-24 01:57:59 +00:00
public function doHelpers () {
2009-05-17 02:41:38 +00:00
return $this -> _doPropertyChoices (
2011-03-19 17:32:35 +00:00
__d ( 'cake_console' , " Would you like this controller to use other helpers \n besides HtmlHelper and FormHelper? " ),
__d ( 'cake_console' , " Please provide a comma separated list of the other \n helper names you'd like to use. \n Example: 'Ajax, Javascript, Time' " )
2009-05-17 02:41:38 +00:00
);
2009-05-15 04:04:57 +00:00
}
/**
* Interact with the user and get a list of additional components
*
* @ return array Components the user wants to use .
2009-11-14 12:19:25 +00:00
*/
2010-04-24 01:57:59 +00:00
public function doComponents () {
2009-05-17 02:41:38 +00:00
return $this -> _doPropertyChoices (
2011-03-19 17:32:35 +00:00
__d ( 'cake_console' , " Would you like this controller to use any components? " ),
__d ( 'cake_console' , " Please provide a comma separated list of the component names you'd like to use. \n Example: 'Acl, Security, RequestHandler' " )
2009-05-17 02:41:38 +00:00
);
}
2009-07-24 19:18:37 +00:00
2009-05-17 02:41:38 +00:00
/**
* Common code for property choice handling .
*
* @ param string $prompt A yes / no question to precede the list
2011-08-01 02:57:17 +00:00
* @ param string $example A question for a comma separated list , with examples .
2009-05-17 02:41:38 +00:00
* @ return array Array of values for property .
2009-11-14 12:19:25 +00:00
*/
2010-04-24 01:57:59 +00:00
protected function _doPropertyChoices ( $prompt , $example ) {
2009-05-17 02:41:38 +00:00
$proceed = $this -> in ( $prompt , array ( 'y' , 'n' ), 'n' );
$property = array ();
if ( strtolower ( $proceed ) == 'y' ) {
$propertyList = $this -> in ( $example );
$propertyListTrimmed = str_replace ( ' ' , '' , $propertyList );
$property = explode ( ',' , $propertyListTrimmed );
2009-05-15 04:04:57 +00:00
}
2009-06-07 16:22:43 +00:00
return array_filter ( $property );
2009-05-15 04:04:57 +00:00
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
2009-05-15 03:17:42 +00:00
* Outputs and gets the list of possible controllers from database
2008-05-30 11:40:08 +00:00
*
* @ param string $useDbConfig Database configuration name
* @ return array Set of controllers
*/
2010-04-05 03:19:38 +00:00
public function listAll ( $useDbConfig = null ) {
2009-05-15 03:17:42 +00:00
if ( is_null ( $useDbConfig )) {
$useDbConfig = $this -> connection ;
2008-05-30 11:40:08 +00:00
}
2009-05-15 03:17:42 +00:00
$this -> __tables = $this -> Model -> getAllTables ( $useDbConfig );
2008-05-30 11:40:08 +00:00
2009-05-15 03:17:42 +00:00
if ( $this -> interactive == true ) {
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Possible Controllers based on your current database:' ));
2009-04-28 03:36:57 +00:00
$this -> _controllerNames = array ();
2009-05-15 03:17:42 +00:00
$count = count ( $this -> __tables );
2009-04-28 03:36:57 +00:00
for ( $i = 0 ; $i < $count ; $i ++ ) {
2009-05-15 03:17:42 +00:00
$this -> _controllerNames [] = $this -> _controllerName ( $this -> _modelName ( $this -> __tables [ $i ]));
2009-04-28 03:36:57 +00:00
$this -> out ( $i + 1 . " . " . $this -> _controllerNames [ $i ]);
}
return $this -> _controllerNames ;
2008-05-30 11:40:08 +00:00
}
2009-04-28 03:58:36 +00:00
return $this -> __tables ;
2008-05-30 11:40:08 +00:00
}
2009-07-24 19:18:37 +00:00
2008-05-30 11:40:08 +00:00
/**
* Forces the user to specify the controller he wants to bake , and returns the selected controller name .
*
2009-05-15 03:28:27 +00:00
* @ param string $useDbConfig Connection name to get a controller name for .
2008-05-30 11:40:08 +00:00
* @ return string Controller name
*/
2010-04-05 03:19:38 +00:00
public function getName ( $useDbConfig = null ) {
2009-04-28 03:58:36 +00:00
$controllers = $this -> listAll ( $useDbConfig );
2008-05-30 11:40:08 +00:00
$enteredController = '' ;
while ( $enteredController == '' ) {
2011-03-19 17:32:35 +00:00
$enteredController = $this -> in ( __d ( 'cake_console' , " Enter a number from the list above, \n type in the name of another controller, or 'q' to exit " ), null , 'q' );
2008-05-30 11:40:08 +00:00
if ( $enteredController === 'q' ) {
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Exit' ));
2010-05-27 01:59:56 +00:00
return $this -> _stop ();
2008-05-30 11:40:08 +00:00
}
if ( $enteredController == '' || intval ( $enteredController ) > count ( $controllers )) {
2011-03-19 17:32:35 +00:00
$this -> err ( __d ( 'cake_console' , " The Controller name you supplied was empty, \n or the number you selected was not an option. Please try again. " ));
2008-05-30 11:40:08 +00:00
$enteredController = '' ;
}
}
if ( intval ( $enteredController ) > 0 && intval ( $enteredController ) <= count ( $controllers ) ) {
$controllerName = $controllers [ intval ( $enteredController ) - 1 ];
} else {
$controllerName = Inflector :: camelize ( $enteredController );
}
return $controllerName ;
}
2009-07-24 19:18:37 +00:00
2010-10-13 03:23:18 +00:00
/**
* get the option parser .
*
* @ return void
*/
public function getOptionParser () {
$parser = parent :: getOptionParser ();
return $parser -> description (
2011-03-19 17:32:35 +00:00
__d ( 'cake_console' , 'Bake a controller for a model. Using options you can bake public, admin or both.' )
2010-10-13 03:23:18 +00:00
) -> addArgument ( 'name' , array (
2011-03-19 17:32:35 +00:00
'help' => __d ( 'cake_console' , 'Name of the controller to bake. Can use Plugin.name to bake controllers into plugins.' )
2010-10-13 03:23:18 +00:00
)) -> addOption ( 'public' , array (
2011-03-19 17:32:35 +00:00
'help' => __d ( 'cake_console' , 'Bake a controller with basic crud actions (index, view, add, edit, delete).' ),
2010-10-13 03:23:18 +00:00
'boolean' => true
)) -> addOption ( 'admin' , array (
2011-03-19 17:32:35 +00:00
'help' => __d ( 'cake_console' , 'Bake a controller with crud actions for one of the Routing.prefixes.' ),
2010-10-13 03:23:18 +00:00
'boolean' => true
)) -> addOption ( 'plugin' , array (
'short' => 'p' ,
2011-03-19 17:32:35 +00:00
'help' => __d ( 'cake_console' , 'Plugin to bake the controller into.' )
2010-10-13 03:23:18 +00:00
)) -> addOption ( 'connection' , array (
'short' => 'c' ,
2011-03-19 17:32:35 +00:00
'help' => __d ( 'cake_console' , 'The connection the controller\'s model is on.' )
2010-10-13 03:23:18 +00:00
)) -> addSubcommand ( 'all' , array (
2011-03-19 17:32:35 +00:00
'help' => __d ( 'cake_console' , 'Bake all controllers with CRUD methods.' )
)) -> epilog ( __d ( 'cake_console' , 'Omitting all arguments and options will enter into an interactive mode.' ));
2010-10-13 03:23:18 +00:00
}
2008-05-30 11:40:08 +00:00
/**
* Displays help contents
*
2011-07-29 02:03:44 +00:00
* @ return void
2008-05-30 11:40:08 +00:00
*/
2010-04-05 03:19:38 +00:00
public function help () {
2008-05-30 11:40:08 +00:00
$this -> hr ();
$this -> out ( " Usage: cake bake controller <arg1> <arg2>... " );
$this -> hr ();
2009-10-04 21:59:12 +00:00
$this -> out ( 'Arguments:' );
$this -> out ();
$this -> out ( " <name> " );
$this -> out ( " \t Name of the controller to bake. Can use Plugin.name " );
$this -> out ( " \t as a shortcut for plugin baking. " );
$this -> out ();
2011-05-06 15:13:15 +00:00
$this -> out ( 'Params:' );
$this -> out ();
$this -> out ( '-connection <config>' );
$this -> out ( " \t set db config <config>. uses 'default' if none is specified " );
$this -> out ();
2008-05-30 11:40:08 +00:00
$this -> out ( 'Commands:' );
2009-09-26 21:08:37 +00:00
$this -> out ();
2009-05-30 04:55:44 +00:00
$this -> out ( " controller <name> " );
2009-07-29 16:47:25 +00:00
$this -> out ( " \t bakes controller with var \$ scaffold " );
2009-09-26 21:08:37 +00:00
$this -> out ();
2009-07-29 16:47:25 +00:00
$this -> out ( " controller <name> public " );
2009-07-29 02:53:29 +00:00
$this -> out ( " \t bakes controller with basic crud actions " );
2009-05-30 04:55:44 +00:00
$this -> out ( " \t (index, view, add, edit, delete) " );
2009-09-26 21:08:37 +00:00
$this -> out ();
2009-05-30 04:55:44 +00:00
$this -> out ( " controller <name> admin " );
2009-12-09 04:52:02 +00:00
$this -> out ( " \t bakes a controller with basic crud actions for one of the " );
$this -> out ( " \t Configure::read('Routing.prefixes') methods. " );
2009-09-26 21:08:37 +00:00
$this -> out ();
2009-07-29 16:47:25 +00:00
$this -> out ( " controller <name> public admin " );
2009-12-09 04:52:02 +00:00
$this -> out ( " \t bakes a controller with basic crud actions for one " );
$this -> out ( " \t Configure::read('Routing.prefixes') and non admin methods. " );
2009-07-29 16:47:25 +00:00
$this -> out ( " \t (index, view, add, edit, delete, " );
$this -> out ( " \t admin_index, admin_view, admin_edit, admin_add, admin_delete) " );
2009-09-26 21:08:37 +00:00
$this -> out ();
2009-05-30 04:55:44 +00:00
$this -> out ( " controller all " );
$this -> out ( " \t bakes all controllers with CRUD methods. " );
2009-09-26 21:08:37 +00:00
$this -> out ();
2008-06-04 19:04:58 +00:00
$this -> _stop ();
2008-05-30 11:40:08 +00:00
}
2011-05-01 18:58:32 +00:00
}