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)
|
2010-01-26 22:54:34 +00:00
|
|
|
* Copyright 2005-2010, 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.
|
|
|
|
*
|
2010-01-26 19:18:20 +00:00
|
|
|
* @copyright Copyright 2005-2010, 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
|
|
|
* @package cake
|
|
|
|
* @subpackage cake.cake.console.libs.tasks
|
|
|
|
* @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-03-05 01:52:25 +00:00
|
|
|
include_once dirname(__FILE__) . DS . 'bake.php';
|
|
|
|
|
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
|
|
|
*/
|
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
|
|
|
|
* @access public
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* path to CONTROLLERS directory
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
* @access public
|
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $path = CONTROLLERS;
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Override initialize
|
|
|
|
*
|
|
|
|
*/
|
2010-04-05 03:19:38 +00:00
|
|
|
public function initialize() {
|
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
|
|
|
|
*
|
|
|
|
*/
|
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'])) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(__('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) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(sprintf(__('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);
|
|
|
|
if (App::import('Model', $model)) {
|
|
|
|
$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();
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(sprintf(__("Bake Controller\nPath: %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();
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(sprintf(__('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
|
|
|
|
2009-05-17 02:41:38 +00:00
|
|
|
$controllerFile = strtolower(Inflector::underscore($controllerName));
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2010-04-15 15:43:39 +00:00
|
|
|
$question[] = __("Would you like to build your controller interactively?");
|
2008-05-30 11:40:08 +00:00
|
|
|
if (file_exists($this->path . $controllerFile .'_controller.php')) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$question[] = sprintf(__("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(
|
2010-04-15 15:43:39 +00:00
|
|
|
__("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(
|
2010-04-15 15:43:39 +00:00
|
|
|
__("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);
|
2010-04-15 15:43:39 +00:00
|
|
|
$looksGood = $this->in(__('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
|
|
|
|
*
|
|
|
|
* @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();
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(__('The following controller will be created:'));
|
2009-05-17 03:17:40 +00:00
|
|
|
$this->hr();
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(sprintf(__("Controller Name:\n\t%s"), $controllerName));
|
2009-05-17 03:17:40 +00:00
|
|
|
|
|
|
|
if (strtolower($useDynamicScaffold) == 'y') {
|
2009-05-17 04:05:07 +00:00
|
|
|
$this->out("var \$scaffold;");
|
2009-05-17 03:17:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$properties = array(
|
2010-04-15 15:43:39 +00:00
|
|
|
'helpers' => __('Helpers:'),
|
|
|
|
'components' => __('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(
|
2010-04-15 15:43:39 +00:00
|
|
|
__("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(
|
2010-04-15 15:43:39 +00:00
|
|
|
__("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
|
|
|
|
* @access private
|
|
|
|
*/
|
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) {
|
|
|
|
$modelImport = $plugin . '.' . $modelImport;
|
2009-06-17 02:17:18 +00:00
|
|
|
}
|
2009-07-28 07:13:30 +00:00
|
|
|
if (!App::import('Model', $modelImport)) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->err(__('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);
|
2009-05-17 04:31:37 +00:00
|
|
|
|
2010-09-03 16:33:59 +00:00
|
|
|
$this->Template->set(compact('plugin', 'admin', 'controllerPath', 'pluralName', 'singularName', 'singularHumanName',
|
2009-05-20 03:53:41 +00:00
|
|
|
'pluralHumanName', 'modelObj', 'wannaUseSession', 'currentModelName'));
|
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
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
*/
|
2010-04-24 01:57:59 +00:00
|
|
|
public function bake($controllerName, $actions = '', $helpers = null, $components = null) {
|
2010-11-21 17:47:49 +00:00
|
|
|
$this->out("\nBaking controller class for $controllerName...", 1, Shell::QUIET);
|
|
|
|
|
2009-05-17 04:05:07 +00:00
|
|
|
$isScaffold = ($actions === 'scaffold') ? true : false;
|
2008-05-30 11:40:08 +00:00
|
|
|
|
2009-06-17 02:29:48 +00:00
|
|
|
$this->Template->set('plugin', Inflector::camelize($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();
|
2009-06-07 00:25:57 +00:00
|
|
|
$filename = $path . $this->_controllerPath($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(
|
2010-04-15 15:43:39 +00:00
|
|
|
__("Would you like this controller to use other helpers\nbesides HtmlHelper and FormHelper?"),
|
|
|
|
__("Please provide a comma separated list of the other\nhelper names you'd like to use.\nExample: '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(
|
2010-04-15 15:43:39 +00:00
|
|
|
__("Would you like this controller to use any components?"),
|
|
|
|
__("Please provide a comma separated list of the component names you'd like to use.\nExample: '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
|
|
|
|
* @param sting $example A question for a comma separated list, with examples.
|
|
|
|
* @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
|
2009-04-28 03:36:57 +00:00
|
|
|
* @param boolean $interactive Whether you are using listAll interactively and want options output.
|
2008-05-30 11:40:08 +00:00
|
|
|
* @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) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(__('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 == '') {
|
2010-04-15 15:43:39 +00:00
|
|
|
$enteredController = $this->in(__("Enter a number from the list above,\ntype in the name of another controller, or 'q' to exit"), null, 'q');
|
2008-05-30 11:40:08 +00:00
|
|
|
if ($enteredController === 'q') {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->out(__('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)) {
|
2010-04-15 15:43:39 +00:00
|
|
|
$this->err(__("The Controller name you supplied was empty,\nor 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(
|
|
|
|
__('Bake a controller for a model. Using options you can bake public, admin or both.')
|
|
|
|
)->addArgument('name', array(
|
|
|
|
'help' => __('Name of the controller to bake. Can use Plugin.name to bake controllers into plugins.')
|
|
|
|
))->addOption('public', array(
|
|
|
|
'help' => __('Bake a controller with basic crud actions (index, view, add, edit, delete).'),
|
|
|
|
'boolean' => true
|
|
|
|
))->addOption('admin', array(
|
|
|
|
'help' => __('Bake a controller with crud actions for one of the Routing.prefixes.'),
|
|
|
|
'boolean' => true
|
|
|
|
))->addOption('plugin', array(
|
|
|
|
'short' => 'p',
|
|
|
|
'help' => __('Plugin to bake the controller into.')
|
|
|
|
))->addOption('connection', array(
|
|
|
|
'short' => 'c',
|
|
|
|
'help' => __('The connection the controller\'s model is on.')
|
|
|
|
))->addSubcommand('all', array(
|
|
|
|
'help' => __('Bake all controllers with CRUD methods.')
|
|
|
|
))->epilog(__('Omitting all arguments and options will enter into an interactive mode.'));
|
|
|
|
}
|
|
|
|
|
2008-05-30 11:40:08 +00:00
|
|
|
/**
|
|
|
|
* Displays help contents
|
|
|
|
*
|
|
|
|
*/
|
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("\tName of the controller to bake. Can use Plugin.name");
|
|
|
|
$this->out("\tas a shortcut for plugin baking.");
|
|
|
|
$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("\tbakes 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("\tbakes 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("\tbakes a controller with basic crud actions for one of the");
|
|
|
|
$this->out("\tConfigure::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("\tbakes a controller with basic crud actions for one");
|
|
|
|
$this->out("\tConfigure::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("\tadmin_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("\tbakes 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
|
|
|
}
|
|
|
|
}
|