1 | <?php |
---|
2 | /** |
---|
3 | * Command-line code generation utility to automate programmer chores. |
---|
4 | * |
---|
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. |
---|
8 | * |
---|
9 | * PHP versions 4 and 5 |
---|
10 | * |
---|
11 | * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
---|
12 | * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) |
---|
13 | * |
---|
14 | * Licensed under The MIT License |
---|
15 | * Redistributions of files must retain the above copyright notice. |
---|
16 | * |
---|
17 | * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) |
---|
18 | * @link http://cakephp.org CakePHP(tm) Project |
---|
19 | * @package cake |
---|
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) |
---|
23 | */ |
---|
24 | |
---|
25 | /** |
---|
26 | * Bake is a command-line code generation utility for automating programmer chores. |
---|
27 | * |
---|
28 | * @package cake |
---|
29 | * @subpackage cake.cake.console.libs |
---|
30 | * @link http://book.cakephp.org/view/1522/Code-Generation-with-Bake |
---|
31 | */ |
---|
32 | class BakeShell extends Shell { |
---|
33 | |
---|
34 | /** |
---|
35 | * Contains tasks to load and instantiate |
---|
36 | * |
---|
37 | * @var array |
---|
38 | * @access public |
---|
39 | */ |
---|
40 | var $tasks = array('Project', 'DbConfig', 'Model', 'Controller', 'View', 'Plugin', 'Fixture', 'Test'); |
---|
41 | |
---|
42 | /** |
---|
43 | * Override loadTasks() to handle paths |
---|
44 | * |
---|
45 | * @access public |
---|
46 | */ |
---|
47 | function loadTasks() { |
---|
48 | parent::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']; |
---|
53 | } |
---|
54 | foreach($this->args as $i => $arg) { |
---|
55 | if (strpos($arg, '.')) { |
---|
56 | list($this->params['plugin'], $this->args[$i]) = pluginSplit($arg); |
---|
57 | break; |
---|
58 | } |
---|
59 | } |
---|
60 | if (isset($this->params['plugin'])) { |
---|
61 | $this->{$task}->plugin = $this->params['plugin']; |
---|
62 | } |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | /** |
---|
67 | * Override main() to handle action |
---|
68 | * |
---|
69 | * @access public |
---|
70 | */ |
---|
71 | function main() { |
---|
72 | Configure::write('Cache.disable', 1); |
---|
73 | |
---|
74 | if (!is_dir($this->DbConfig->path)) { |
---|
75 | if ($this->Project->execute()) { |
---|
76 | $this->DbConfig->path = $this->params['working'] . DS . 'config' . DS; |
---|
77 | } else { |
---|
78 | return false; |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | if (!config('database')) { |
---|
83 | $this->out(__("Your database configuration was not found. Take a moment to create one.", true)); |
---|
84 | $this->args = null; |
---|
85 | return $this->DbConfig->execute(); |
---|
86 | } |
---|
87 | $this->out('Interactive Bake Shell'); |
---|
88 | $this->hr(); |
---|
89 | $this->out('[D]atabase Configuration'); |
---|
90 | $this->out('[M]odel'); |
---|
91 | $this->out('[V]iew'); |
---|
92 | $this->out('[C]ontroller'); |
---|
93 | $this->out('[P]roject'); |
---|
94 | $this->out('[F]ixture'); |
---|
95 | $this->out('[T]est case'); |
---|
96 | $this->out('[Q]uit'); |
---|
97 | |
---|
98 | $classToBake = strtoupper($this->in(__('What would you like to Bake?', true), array('D', 'M', 'V', 'C', 'P', 'F', 'T', 'Q'))); |
---|
99 | switch ($classToBake) { |
---|
100 | case 'D': |
---|
101 | $this->DbConfig->execute(); |
---|
102 | break; |
---|
103 | case 'M': |
---|
104 | $this->Model->execute(); |
---|
105 | break; |
---|
106 | case 'V': |
---|
107 | $this->View->execute(); |
---|
108 | break; |
---|
109 | case 'C': |
---|
110 | $this->Controller->execute(); |
---|
111 | break; |
---|
112 | case 'P': |
---|
113 | $this->Project->execute(); |
---|
114 | break; |
---|
115 | case 'F': |
---|
116 | $this->Fixture->execute(); |
---|
117 | break; |
---|
118 | case 'T': |
---|
119 | $this->Test->execute(); |
---|
120 | break; |
---|
121 | case 'Q': |
---|
122 | exit(0); |
---|
123 | break; |
---|
124 | default: |
---|
125 | $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)); |
---|
126 | } |
---|
127 | $this->hr(); |
---|
128 | $this->main(); |
---|
129 | } |
---|
130 | |
---|
131 | /** |
---|
132 | * Quickly bake the MVC |
---|
133 | * |
---|
134 | * @access public |
---|
135 | */ |
---|
136 | function all() { |
---|
137 | $this->hr(); |
---|
138 | $this->out('Bake All'); |
---|
139 | $this->hr(); |
---|
140 | |
---|
141 | if (!isset($this->params['connection']) && empty($this->connection)) { |
---|
142 | $this->connection = $this->DbConfig->getConfig(); |
---|
143 | } |
---|
144 | |
---|
145 | if (empty($this->args)) { |
---|
146 | $this->Model->interactive = true; |
---|
147 | $name = $this->Model->getName($this->connection); |
---|
148 | } |
---|
149 | |
---|
150 | foreach (array('Model', 'Controller', 'View') as $task) { |
---|
151 | $this->{$task}->connection = $this->connection; |
---|
152 | $this->{$task}->interactive = false; |
---|
153 | } |
---|
154 | |
---|
155 | if (!empty($this->args[0])) { |
---|
156 | $name = $this->args[0]; |
---|
157 | } |
---|
158 | |
---|
159 | $modelExists = false; |
---|
160 | $model = $this->_modelName($name); |
---|
161 | if (App::import('Model', $model)) { |
---|
162 | $object = new $model(); |
---|
163 | $modelExists = true; |
---|
164 | } else { |
---|
165 | App::import('Model', 'Model', false); |
---|
166 | $object = new Model(array('name' => $name, 'ds' => $this->connection)); |
---|
167 | } |
---|
168 | |
---|
169 | $modelBaked = $this->Model->bake($object, false); |
---|
170 | |
---|
171 | if ($modelBaked && $modelExists === false) { |
---|
172 | $this->out(sprintf(__('%s Model was baked.', true), $model)); |
---|
173 | if ($this->_checkUnitTest()) { |
---|
174 | $this->Model->bakeFixture($model); |
---|
175 | $this->Model->bakeTest($model); |
---|
176 | } |
---|
177 | $modelExists = true; |
---|
178 | } |
---|
179 | |
---|
180 | if ($modelExists === true) { |
---|
181 | $controller = $this->_controllerName($name); |
---|
182 | if ($this->Controller->bake($controller, $this->Controller->bakeActions($controller))) { |
---|
183 | $this->out(sprintf(__('%s Controller was baked.', true), $name)); |
---|
184 | if ($this->_checkUnitTest()) { |
---|
185 | $this->Controller->bakeTest($controller); |
---|
186 | } |
---|
187 | } |
---|
188 | if (App::import('Controller', $controller)) { |
---|
189 | $this->View->args = array($controller); |
---|
190 | $this->View->execute(); |
---|
191 | $this->out(sprintf(__('%s Views were baked.', true), $name)); |
---|
192 | } |
---|
193 | $this->out(__('Bake All complete', true)); |
---|
194 | array_shift($this->args); |
---|
195 | } else { |
---|
196 | $this->err(__('Bake All could not continue without a valid model', true)); |
---|
197 | } |
---|
198 | $this->_stop(); |
---|
199 | } |
---|
200 | |
---|
201 | /** |
---|
202 | * Displays help contents |
---|
203 | * |
---|
204 | * @access public |
---|
205 | */ |
---|
206 | function help() { |
---|
207 | $this->out('CakePHP Bake:'); |
---|
208 | $this->hr(); |
---|
209 | $this->out('The Bake script generates controllers, views and models for your application.'); |
---|
210 | $this->out('If run with no command line arguments, Bake guides the user through the class'); |
---|
211 | $this->out('creation process. You can customize the generation process by telling Bake'); |
---|
212 | $this->out('where different parts of your application are using command line arguments.'); |
---|
213 | $this->hr(); |
---|
214 | $this->out("Usage: cake bake <command> <arg1> <arg2>..."); |
---|
215 | $this->hr(); |
---|
216 | $this->out('Params:'); |
---|
217 | $this->out("\t-app <path> Absolute/Relative path to your app folder.\n"); |
---|
218 | $this->out('Commands:'); |
---|
219 | $this->out("\n\tbake help\n\t\tshows this help message."); |
---|
220 | $this->out("\n\tbake all <name>\n\t\tbakes complete MVC. optional <name> of a Model"); |
---|
221 | $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"); |
---|
222 | $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."); |
---|
223 | $this->out("\n\tbake db_config\n\t\tbakes a database.php file in config directory."); |
---|
224 | $this->out("\n\tbake model\n\t\tbakes a model. run 'bake model help' for more info"); |
---|
225 | $this->out("\n\tbake view\n\t\tbakes views. run 'bake view help' for more info"); |
---|
226 | $this->out("\n\tbake controller\n\t\tbakes a controller. run 'bake controller help' for more info"); |
---|
227 | $this->out("\n\tbake fixture\n\t\tbakes fixtures. run 'bake fixture help' for more info."); |
---|
228 | $this->out("\n\tbake test\n\t\tbakes unit tests. run 'bake test help' for more info."); |
---|
229 | $this->out(); |
---|
230 | |
---|
231 | } |
---|
232 | } |
---|