| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* SVN FILE: $Id: bake.php 7945 2008-12-19 02:16:01Z gwoo $ */
|
|
|
3 |
/**
|
|
|
4 |
* Command-line code generation utility to automate programmer chores.
|
|
|
5 |
*
|
|
|
6 |
* Bake is CakePHP's code generation script, which can help you kickstart
|
|
|
7 |
* application development by writing fully functional skeleton controllers,
|
|
|
8 |
* models, and views. Going further, Bake can also write Unit Tests for you.
|
|
|
9 |
*
|
|
|
10 |
* PHP versions 4 and 5
|
|
|
11 |
*
|
|
|
12 |
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
|
|
13 |
* Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
14 |
*
|
|
|
15 |
* Licensed under The MIT License
|
|
|
16 |
* Redistributions of files must retain the above copyright notice.
|
|
|
17 |
*
|
|
|
18 |
* @filesource
|
|
|
19 |
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
20 |
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
21 |
* @package cake
|
|
|
22 |
* @subpackage cake.cake.console.libs
|
|
|
23 |
* @since CakePHP(tm) v 1.2.0.5012
|
|
|
24 |
* @version $Revision: 7945 $
|
|
|
25 |
* @modifiedby $LastChangedBy: gwoo $
|
|
|
26 |
* @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
|
|
|
27 |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
28 |
*/
|
|
|
29 |
/**
|
|
|
30 |
* Bake is a command-line code generation utility for automating programmer chores.
|
|
|
31 |
*
|
|
|
32 |
* @package cake
|
|
|
33 |
* @subpackage cake.cake.console.libs
|
|
|
34 |
* @link http://book.cakephp.org/view/113/Code-Generation-with-Bake
|
|
|
35 |
*/
|
|
|
36 |
class BakeShell extends Shell {
|
|
|
37 |
/**
|
|
|
38 |
* Contains tasks to load and instantiate
|
|
|
39 |
*
|
|
|
40 |
* @var array
|
|
|
41 |
* @access public
|
|
|
42 |
*/
|
|
|
43 |
var $tasks = array('Project', 'DbConfig', 'Model', 'Controller', 'View', 'Plugin', 'Test');
|
|
|
44 |
/**
|
|
|
45 |
* Override loadTasks() to handle paths
|
|
|
46 |
*
|
|
|
47 |
* @access public
|
|
|
48 |
*/
|
|
|
49 |
function loadTasks() {
|
|
|
50 |
parent::loadTasks();
|
|
|
51 |
$task = Inflector::classify($this->command);
|
|
|
52 |
if (isset($this->{$task}) && !in_array($task, array('Project', 'DbConfig'))) {
|
|
|
53 |
$path = Inflector::underscore(Inflector::pluralize($this->command));
|
|
|
54 |
$this->{$task}->path = $this->params['working'] . DS . $path . DS;
|
|
|
55 |
if (!is_dir($this->{$task}->path)) {
|
|
|
56 |
$this->err(sprintf(__("%s directory could not be found.\nBe sure you have created %s", true), $task, $this->{$task}->path));
|
|
|
57 |
$this->_stop();
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
}
|
|
|
61 |
/**
|
|
|
62 |
* Override main() to handle action
|
|
|
63 |
*
|
|
|
64 |
* @access public
|
|
|
65 |
*/
|
|
|
66 |
function main() {
|
|
|
67 |
if (!is_dir($this->DbConfig->path)) {
|
|
|
68 |
if ($this->Project->execute()) {
|
|
|
69 |
$this->DbConfig->path = $this->params['working'] . DS . 'config' . DS;
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
if (!config('database')) {
|
|
|
74 |
$this->out(__("Your database configuration was not found. Take a moment to create one.", true));
|
|
|
75 |
$this->args = null;
|
|
|
76 |
return $this->DbConfig->execute();
|
|
|
77 |
}
|
|
|
78 |
$this->out('Interactive Bake Shell');
|
|
|
79 |
$this->hr();
|
|
|
80 |
$this->out('[D]atabase Configuration');
|
|
|
81 |
$this->out('[M]odel');
|
|
|
82 |
$this->out('[V]iew');
|
|
|
83 |
$this->out('[C]ontroller');
|
|
|
84 |
$this->out('[P]roject');
|
|
|
85 |
$this->out('[Q]uit');
|
|
|
86 |
|
|
|
87 |
$classToBake = strtoupper($this->in(__('What would you like to Bake?', true), array('D', 'M', 'V', 'C', 'P', 'Q')));
|
|
|
88 |
switch ($classToBake) {
|
|
|
89 |
case 'D':
|
|
|
90 |
$this->DbConfig->execute();
|
|
|
91 |
break;
|
|
|
92 |
case 'M':
|
|
|
93 |
$this->Model->execute();
|
|
|
94 |
break;
|
|
|
95 |
case 'V':
|
|
|
96 |
$this->View->execute();
|
|
|
97 |
break;
|
|
|
98 |
case 'C':
|
|
|
99 |
$this->Controller->execute();
|
|
|
100 |
break;
|
|
|
101 |
case 'P':
|
|
|
102 |
$this->Project->execute();
|
|
|
103 |
break;
|
|
|
104 |
case 'Q':
|
|
|
105 |
exit(0);
|
|
|
106 |
break;
|
|
|
107 |
default:
|
|
|
108 |
$this->out(__('You have made an invalid selection. Please choose a type of class to Bake by entering D, M, V, or C.', true));
|
|
|
109 |
}
|
|
|
110 |
$this->hr();
|
|
|
111 |
$this->main();
|
|
|
112 |
}
|
|
|
113 |
/**
|
|
|
114 |
* Quickly bake the MVC
|
|
|
115 |
*
|
|
|
116 |
* @access public
|
|
|
117 |
*/
|
|
|
118 |
function all() {
|
|
|
119 |
$ds = 'default';
|
|
|
120 |
$this->hr();
|
|
|
121 |
$this->out('Bake All');
|
|
|
122 |
$this->hr();
|
|
|
123 |
|
|
|
124 |
if (isset($this->params['connection'])) {
|
|
|
125 |
$ds = $this->params['connection'];
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
if (empty($this->args)) {
|
|
|
129 |
$name = $this->Model->getName($ds);
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
if (!empty($this->args[0])) {
|
|
|
133 |
$name = $this->args[0];
|
|
|
134 |
$this->Model->listAll($ds, false);
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
$modelExists = false;
|
|
|
138 |
$model = $this->_modelName($name);
|
|
|
139 |
if (App::import('Model', $model)) {
|
|
|
140 |
$object = new $model();
|
|
|
141 |
$modelExists = true;
|
|
|
142 |
} else {
|
|
|
143 |
App::import('Model');
|
|
|
144 |
$object = new Model(array('name' => $name, 'ds' => $ds));
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
$modelBaked = $this->Model->bake($object, false);
|
|
|
148 |
|
|
|
149 |
if ($modelBaked && $modelExists === false) {
|
|
|
150 |
$this->out(sprintf(__('%s Model was baked.', true), $model));
|
|
|
151 |
if ($this->_checkUnitTest()) {
|
|
|
152 |
$this->Model->bakeTest($model);
|
|
|
153 |
}
|
|
|
154 |
$modelExists = true;
|
|
|
155 |
}
|
|
|
156 |
|
|
|
157 |
if ($modelExists === true) {
|
|
|
158 |
$controller = $this->_controllerName($name);
|
|
|
159 |
if ($this->Controller->bake($controller, $this->Controller->bakeActions($controller))) {
|
|
|
160 |
$this->out(sprintf(__('%s Controller was baked.', true), $name));
|
|
|
161 |
if ($this->_checkUnitTest()) {
|
|
|
162 |
$this->Controller->bakeTest($controller);
|
|
|
163 |
}
|
|
|
164 |
}
|
|
|
165 |
if (App::import('Controller', $controller)) {
|
|
|
166 |
$this->View->args = array($controller);
|
|
|
167 |
$this->View->execute();
|
|
|
168 |
}
|
|
|
169 |
$this->out(__('Bake All complete'));
|
|
|
170 |
array_shift($this->args);
|
|
|
171 |
} else {
|
|
|
172 |
$this->err(__('Bake All could not continue without a valid model', true));
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
if (empty($this->args)) {
|
|
|
176 |
$this->all();
|
|
|
177 |
}
|
|
|
178 |
$this->_stop();
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
/**
|
|
|
182 |
* Displays help contents
|
|
|
183 |
*
|
|
|
184 |
* @access public
|
|
|
185 |
*/
|
|
|
186 |
function help() {
|
|
|
187 |
$this->out('CakePHP Bake:');
|
|
|
188 |
$this->hr();
|
|
|
189 |
$this->out('The Bake script generates controllers, views and models for your application.');
|
|
|
190 |
$this->out('If run with no command line arguments, Bake guides the user through the class');
|
|
|
191 |
$this->out('creation process. You can customize the generation process by telling Bake');
|
|
|
192 |
$this->out('where different parts of your application are using command line arguments.');
|
|
|
193 |
$this->hr();
|
|
|
194 |
$this->out("Usage: cake bake <command> <arg1> <arg2>...");
|
|
|
195 |
$this->hr();
|
|
|
196 |
$this->out('Params:');
|
|
|
197 |
$this->out("\t-app <path> Absolute/Relative path to your app folder.\n");
|
|
|
198 |
$this->out('Commands:');
|
|
|
199 |
$this->out("\n\tbake help\n\t\tshows this help message.");
|
|
|
200 |
$this->out("\n\tbake all <name>\n\t\tbakes complete MVC. optional <name> of a Model");
|
|
|
201 |
$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");
|
|
|
202 |
$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.");
|
|
|
203 |
$this->out("\n\tbake db_config\n\t\tbakes a database.php file in config directory.");
|
|
|
204 |
$this->out("\n\tbake model\n\t\tbakes a model. run 'bake model help' for more info");
|
|
|
205 |
$this->out("\n\tbake view\n\t\tbakes views. run 'bake view help' for more info");
|
|
|
206 |
$this->out("\n\tbake controller\n\t\tbakes a controller. run 'bake controller help' for more info");
|
|
|
207 |
$this->out("");
|
|
|
208 |
|
|
|
209 |
}
|
|
|
210 |
}
|
|
|
211 |
?>
|