3
* Command-line code generation utility to automate programmer chores.
5
* Bake is CakePHP's code generation script, which can help you kickstart
6
* application development by writing fully functional skeleton controllers,
7
* models, and views. Going further, Bake can also write Unit Tests for you.
11
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
12
* Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
14
* Licensed under The MIT License
15
* Redistributions of files must retain the above copyright notice.
17
* @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
18
* @link http://cakephp.org CakePHP(tm) Project
20
* @subpackage cake.cake.console.libs
21
* @since CakePHP(tm) v 1.2.0.5012
22
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
26
* Bake is a command-line code generation utility for automating programmer chores.
29
* @subpackage cake.cake.console.libs
30
* @link http://book.cakephp.org/view/1522/Code-Generation-with-Bake
32
class BakeShell extends Shell {
35
* Contains tasks to load and instantiate
40
var $tasks = array('Project', 'DbConfig', 'Model', 'Controller', 'View', 'Plugin', 'Fixture', 'Test');
43
* Override loadTasks() to handle paths
47
function loadTasks() {
49
$task = Inflector::classify($this->command);
50
if (isset($this->{$task}) && !in_array($task, array('Project', 'DbConfig'))) {
51
if (isset($this->params['connection'])) {
52
$this->{$task}->connection = $this->params['connection'];
54
foreach($this->args as $i => $arg) {
55
if (strpos($arg, '.')) {
56
list($this->params['plugin'], $this->args[$i]) = pluginSplit($arg);
60
if (isset($this->params['plugin'])) {
61
$this->{$task}->plugin = $this->params['plugin'];
67
* Override main() to handle action
72
if (!is_dir($this->DbConfig->path)) {
73
if ($this->Project->execute()) {
74
$this->DbConfig->path = $this->params['working'] . DS . 'config' . DS;
80
if (!config('database')) {
81
$this->out(__("Your database configuration was not found. Take a moment to create one.", true));
83
return $this->DbConfig->execute();
85
$this->out('Interactive Bake Shell');
87
$this->out('[D]atabase Configuration');
88
$this->out('[M]odel');
90
$this->out('[C]ontroller');
91
$this->out('[P]roject');
92
$this->out('[F]ixture');
93
$this->out('[T]est case');
96
$classToBake = strtoupper($this->in(__('What would you like to Bake?', true), array('D', 'M', 'V', 'C', 'P', 'F', 'T', 'Q')));
97
switch ($classToBake) {
99
$this->DbConfig->execute();
102
$this->Model->execute();
105
$this->View->execute();
108
$this->Controller->execute();
111
$this->Project->execute();
114
$this->Fixture->execute();
117
$this->Test->execute();
123
$this->out(__('You have made an invalid selection. Please choose a type of class to Bake by entering D, M, V, F, T, or C.', true));
130
* Quickly bake the MVC
136
$this->out('Bake All');
139
if (!isset($this->params['connection']) && empty($this->connection)) {
140
$this->connection = $this->DbConfig->getConfig();
143
if (empty($this->args)) {
144
$this->Model->interactive = true;
145
$name = $this->Model->getName($this->connection);
148
foreach (array('Model', 'Controller', 'View') as $task) {
149
$this->{$task}->connection = $this->connection;
150
$this->{$task}->interactive = false;
153
if (!empty($this->args[0])) {
154
$name = $this->args[0];
157
$modelExists = false;
158
$model = $this->_modelName($name);
159
if (App::import('Model', $model)) {
160
$object = new $model();
163
App::import('Model', 'Model', false);
164
$object = new Model(array('name' => $name, 'ds' => $this->connection));
167
$modelBaked = $this->Model->bake($object, false);
169
if ($modelBaked && $modelExists === false) {
170
$this->out(sprintf(__('%s Model was baked.', true), $model));
171
if ($this->_checkUnitTest()) {
172
$this->Model->bakeFixture($model);
173
$this->Model->bakeTest($model);
178
if ($modelExists === true) {
179
$controller = $this->_controllerName($name);
180
if ($this->Controller->bake($controller, $this->Controller->bakeActions($controller))) {
181
$this->out(sprintf(__('%s Controller was baked.', true), $name));
182
if ($this->_checkUnitTest()) {
183
$this->Controller->bakeTest($controller);
186
if (App::import('Controller', $controller)) {
187
$this->View->args = array($controller);
188
$this->View->execute();
189
$this->out(sprintf(__('%s Views were baked.', true), $name));
191
$this->out(__('Bake All complete', true));
192
array_shift($this->args);
194
$this->err(__('Bake All could not continue without a valid model', true));
200
* Displays help contents
205
$this->out('CakePHP Bake:');
207
$this->out('The Bake script generates controllers, views and models for your application.');
208
$this->out('If run with no command line arguments, Bake guides the user through the class');
209
$this->out('creation process. You can customize the generation process by telling Bake');
210
$this->out('where different parts of your application are using command line arguments.');
212
$this->out("Usage: cake bake <command> <arg1> <arg2>...");
214
$this->out('Params:');
215
$this->out("\t-app <path> Absolute/Relative path to your app folder.\n");
216
$this->out('Commands:');
217
$this->out("\n\tbake help\n\t\tshows this help message.");
218
$this->out("\n\tbake all <name>\n\t\tbakes complete MVC. optional <name> of a Model");
219
$this->out("\n\tbake project <path>\n\t\tbakes a new app folder in the path supplied\n\t\tor in current directory if no path is specified");
220
$this->out("\n\tbake plugin <name>\n\t\tbakes a new plugin folder in the path supplied\n\t\tor in current directory if no path is specified.");
221
$this->out("\n\tbake db_config\n\t\tbakes a database.php file in config directory.");
222
$this->out("\n\tbake model\n\t\tbakes a model. run 'bake model help' for more info");
223
$this->out("\n\tbake view\n\t\tbakes views. run 'bake view help' for more info");
224
$this->out("\n\tbake controller\n\t\tbakes a controller. run 'bake controller help' for more info");
225
$this->out("\n\tbake fixture\n\t\tbakes fixtures. run 'bake fixture help' for more info.");
226
$this->out("\n\tbake test\n\t\tbakes unit tests. run 'bake test help' for more info.");