2008-05-30 11:40:08 +00:00
< ? php
/**
* The ControllerTask handles creating and updating controller files .
*
2009-11-06 06:46:59 +00:00
* CakePHP ( tm ) : Rapid Development Framework ( http :// cakephp . org )
2013-02-08 12:28:17 +00:00
* Copyright ( c ) Cake Software Foundation , Inc . ( http :// cakefoundation . org )
2008-05-30 11:40:08 +00:00
*
* Licensed under The MIT License
2013-02-08 12:22:51 +00:00
* For full copyright and license information , please see the LICENSE . txt
2008-05-30 11:40:08 +00:00
* Redistributions of files must retain the above copyright notice .
*
2013-02-08 11:59:49 +00:00
* @ copyright Copyright ( c ) 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
2013-05-30 22:11:14 +00:00
* @ license http :// www . opensource . org / licenses / mit - license . php MIT License
2008-05-30 11:40:08 +00:00
*/
2009-07-24 19:18:37 +00:00
2011-11-25 21:05:00 +00:00
App :: uses ( 'AppShell' , 'Console/Command' );
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' ;
}
2013-02-12 02:38:08 +00:00
if ( strtolower ( $this -> args [ 0 ]) === 'all' ) {
2009-04-28 03:36:57 +00:00
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
/**
2012-12-22 22:48:15 +00:00
* Bake All the controllers at once . Will only bake controllers for models that exist .
2009-04-28 03:36:57 +00:00
*
* @ 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 ();
2013-01-16 17:57:18 +00:00
$admin = false ;
if ( ! empty ( $this -> params [ 'admin' ])) {
$admin = $this -> Project -> getPrefix ();
}
2014-01-08 09:52:05 +00:00
$controllersCreated = 0 ;
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 );
2013-01-16 17:57:18 +00:00
if ( $admin ) {
$this -> out ( __d ( 'cake_console' , 'Adding %s methods' , $admin ));
$actions .= " \n " . $this -> bakeActions ( $controller , $admin );
}
2009-07-15 14:14:16 +00:00
if ( $this -> bake ( $controller , $actions ) && $unitTestExists ) {
$this -> bakeTest ( $controller );
}
2014-01-08 09:52:05 +00:00
$controllersCreated ++ ;
2009-04-28 03:36:57 +00:00
}
}
2014-01-08 10:16:06 +00:00
2014-01-08 10:10:40 +00:00
if ( ! $controllersCreated ) {
2014-01-08 12:23:03 +00:00
$this -> out ( __d ( 'cake_console' , 'No Controllers were baked, Models need to exist before Controllers can be baked.' ));
2014-01-08 09:52:05 +00:00
}
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 ();
2012-02-05 17:00:30 +00:00
$this -> out ( __d ( 'cake_console' , " Bake Controller \n Path: %s " , $this -> getPath ()));
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
2011-03-19 17:32:35 +00:00
$question [] = __d ( 'cake_console' , " Would you like to build your controller interactively? " );
2012-03-03 23:55:07 +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
}
2012-03-03 23:55:07 +00:00
$doItInteractive = $this -> in ( implode ( " \n " , $question ), array ( 'y' , 'n' ), 'y' );
2008-05-30 11:40:08 +00:00
2013-02-12 02:38:08 +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 (
2012-03-03 23:55:07 +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
2013-02-12 02:38:08 +00:00
if ( strtolower ( $useDynamicScaffold ) === 'y' ) {
2009-05-17 04:05:07 +00:00
$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 (
2016-05-02 21:37:47 +00:00
__d ( 'cake_console' , " Would you like to use the FlashComponent to display flash messages? " ), array ( 'y' , 'n' ), 'y'
2009-05-17 03:17:40 +00:00
);
2014-02-04 14:37:23 +00:00
if ( strtolower ( $wannaUseSession ) === 'y' ) {
2016-05-02 21:37:47 +00:00
array_push ( $components , 'Session' , 'Flash' );
2014-02-04 14:37:23 +00:00
}
2014-07-18 10:28:41 +00:00
array_unique ( $components );
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
}
2013-02-12 02:38:08 +00:00
if ( strtolower ( $wannaBakeCrud ) === 'y' ) {
$actions = $this -> bakeActions ( $controllerName , null , strtolower ( $wannaUseSession ) === 'y' );
2008-05-30 11:40:08 +00:00
}
2013-02-12 02:38:08 +00:00
if ( strtolower ( $wannaBakeAdminCrud ) === 'y' ) {
2009-10-07 04:46:13 +00:00
$admin = $this -> Project -> getPrefix ();
2013-02-12 02:38:08 +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 );
2013-06-09 15:39:48 +00:00
$looksGood = $this -> in ( __d ( 'cake_console' , 'Look okay?' ), array ( 'y' , 'n' ), 'y' );
2008-05-30 11:40:08 +00:00
2013-02-12 02:38:08 +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
*
2014-05-28 03:34:53 +00:00
* @ param string $controllerName The name of the controller .
* @ param string $useDynamicScaffold Whether or not to use dynamic scaffolds .
* @ param array $helpers The list of helpers to include .
* @ param array $components The list of components to include .
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
2013-02-12 02:38:08 +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 ) {
2014-08-10 23:33:25 +00:00
if ( count ( ${$var} )) {
2009-05-17 03:17:40 +00:00
$output = '' ;
2014-08-10 23:33:25 +00:00
$length = count ( ${$var} );
foreach ( ${$var} as $i => $propElement ) {
2012-03-03 23:55:07 +00:00
if ( $i != $length - 1 ) {
2009-05-17 03:17:40 +00:00
$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())? " ),
2013-06-09 15:39:48 +00:00
array ( 'y' , 'n' ), 'n'
2009-05-17 02:41:38 +00:00
);
$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? " ),
2013-06-09 15:39:48 +00:00
array ( 'y' , 'n' ), 'n'
2009-05-17 02:41:38 +00:00
);
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
2014-07-03 13:36:42 +00:00
* @ param bool $wannaUseSession Set to true to use sessions , false otherwise
2008-05-30 11:40:08 +00:00
* @ 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.' ));
2013-07-01 12:15:54 +00:00
return $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 . '.'
));
2013-07-20 18:21:35 +00:00
if ( ! in_array ( 'Paginator' , ( array ) $components )) {
$components [] = 'Paginator' ;
}
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? " ),
2012-09-30 07:38:57 +00:00
__d ( 'cake_console' , " Please provide a comma separated list of the other \n helper names you'd like to use. \n Example: 'Text, Js, 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 () {
2016-05-02 21:37:47 +00:00
$components = array ( 'Paginator' );
2013-07-20 14:59:39 +00:00
return array_merge ( $components , $this -> _doPropertyChoices (
2016-05-02 21:37:47 +00:00
__d ( 'cake_console' , " Would you like this controller to use other components \n besides PaginatorComponent? " ),
2011-03-19 17:32:35 +00:00
__d ( 'cake_console' , " Please provide a comma separated list of the component names you'd like to use. \n Example: 'Acl, Security, RequestHandler' " )
2013-07-20 14:59:39 +00:00
));
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 ) {
2013-06-09 15:39:48 +00:00
$proceed = $this -> in ( $prompt , array ( 'y' , 'n' ), 'n' );
2009-05-17 02:41:38 +00:00
$property = array ();
2013-02-12 02:38:08 +00:00
if ( strtolower ( $proceed ) === 'y' ) {
2009-05-17 02:41:38 +00:00
$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 ) {
2013-08-16 18:12:49 +00:00
if ( $useDbConfig === null ) {
2009-05-15 03:17:42 +00:00
$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
2012-09-14 17:54:29 +00:00
if ( $this -> interactive ) {
2011-03-19 17:32:35 +00:00
$this -> out ( __d ( 'cake_console' , 'Possible Controllers based on your current database:' ));
2012-06-04 06:01:22 +00:00
$this -> hr ();
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 ]));
2012-06-04 06:01:22 +00:00
$this -> out ( sprintf ( " %2d. %s " , $i + 1 , $this -> _controllerNames [ $i ]));
2009-04-28 03:36:57 +00:00
}
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 = '' ;
2012-09-14 17:42:25 +00:00
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
}
2014-09-10 13:52:57 +00:00
if ( ! $enteredController || ( int ) $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 = '' ;
}
}
2014-09-10 13:52:57 +00:00
if (( int ) $enteredController > 0 && ( int ) $enteredController <= count ( $controllers )) {
$controllerName = $controllers [( int ) $enteredController - 1 ];
2008-05-30 11:40:08 +00:00
} else {
$controllerName = Inflector :: camelize ( $enteredController );
}
return $controllerName ;
}
2009-07-24 19:18:37 +00:00
2010-10-13 03:23:18 +00:00
/**
2013-12-05 23:38:32 +00:00
* Gets the option parser instance and configures it .
2010-10-13 03:23:18 +00:00
*
2013-12-05 23:38:32 +00:00
* @ return ConsoleOptionParser
2010-10-13 03:23:18 +00:00
*/
public function getOptionParser () {
$parser = parent :: getOptionParser ();
2013-12-05 23:38:32 +00:00
$parser -> description (
__d ( 'cake_console' , 'Bake a controller for a model. Using options you can bake public, admin or both.'
)) -> addArgument ( 'name' , array (
'help' => __d ( 'cake_console' , 'Name of the controller to bake. Can use Plugin.name to bake controllers into plugins.' )
)) -> addOption ( 'public' , array (
'help' => __d ( 'cake_console' , 'Bake a controller with basic crud actions (index, view, add, edit, delete).' ),
'boolean' => true
)) -> addOption ( 'admin' , array (
'help' => __d ( 'cake_console' , 'Bake a controller with crud actions for one of the Routing.prefixes.' ),
'boolean' => true
)) -> addOption ( 'plugin' , array (
'short' => 'p' ,
'help' => __d ( 'cake_console' , 'Plugin to bake the controller into.' )
)) -> addOption ( 'connection' , array (
'short' => 'c' ,
'help' => __d ( 'cake_console' , 'The connection the controller\'s model is on.' )
)) -> addOption ( 'theme' , array (
'short' => 't' ,
'help' => __d ( 'cake_console' , 'Theme to use when baking code.' )
)) -> addOption ( 'force' , array (
'short' => 'f' ,
'help' => __d ( 'cake_console' , 'Force overwriting existing files without prompting.' )
)) -> addSubcommand ( 'all' , array (
'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.' )
);
return $parser ;
2010-10-13 03:23:18 +00:00
}
2011-05-01 18:58:32 +00:00
}