| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* SVN FILE: $Id: controller.php 7945 2008-12-19 02:16:01Z gwoo $ */
|
|
|
3 |
/**
|
|
|
4 |
* The ControllerTask handles creating and updating controller files.
|
|
|
5 |
*
|
|
|
6 |
* Long description for file
|
|
|
7 |
*
|
|
|
8 |
* PHP versions 4 and 5
|
|
|
9 |
*
|
|
|
10 |
* CakePHP(tm) : Rapid Development Framework (http://www.cakephp.org)
|
|
|
11 |
* Copyright 2005-2008, Cake Software Foundation, Inc.
|
|
|
12 |
*
|
|
|
13 |
* Licensed under The MIT License
|
|
|
14 |
* Redistributions of files must retain the above copyright notice.
|
|
|
15 |
*
|
|
|
16 |
* @filesource
|
|
|
17 |
* @copyright Copyright 2005-2008, Cake Software Foundation, Inc. (http://www.cakefoundation.org)
|
|
|
18 |
* @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
|
|
|
19 |
* @package cake
|
|
|
20 |
* @subpackage cake.cake.console.libs.tasks
|
|
|
21 |
* @since CakePHP(tm) v 1.2
|
|
|
22 |
* @version $Revision: 7945 $
|
|
|
23 |
* @modifiedby $LastChangedBy: gwoo $
|
|
|
24 |
* @lastmodified $Date: 2008-12-18 18:16:01 -0800 (Thu, 18 Dec 2008) $
|
|
|
25 |
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
|
|
|
26 |
*/
|
|
|
27 |
/**
|
|
|
28 |
* Task class for creating and updating controller files.
|
|
|
29 |
*
|
|
|
30 |
* @package cake
|
|
|
31 |
* @subpackage cake.cake.console.libs.tasks
|
|
|
32 |
*/
|
|
|
33 |
class ControllerTask extends Shell {
|
|
|
34 |
/**
|
|
|
35 |
* Name of plugin
|
|
|
36 |
*
|
|
|
37 |
* @var string
|
|
|
38 |
* @access public
|
|
|
39 |
*/
|
|
|
40 |
var $plugin = null;
|
|
|
41 |
/**
|
|
|
42 |
* Tasks to be loaded by this Task
|
|
|
43 |
*
|
|
|
44 |
* @var array
|
|
|
45 |
* @access public
|
|
|
46 |
*/
|
|
|
47 |
var $tasks = array('Project');
|
|
|
48 |
/**
|
|
|
49 |
* path to CONTROLLERS directory
|
|
|
50 |
*
|
|
|
51 |
* @var array
|
|
|
52 |
* @access public
|
|
|
53 |
*/
|
|
|
54 |
var $path = CONTROLLERS;
|
|
|
55 |
/**
|
|
|
56 |
* Override initialize
|
|
|
57 |
*
|
|
|
58 |
* @access public
|
|
|
59 |
*/
|
|
|
60 |
function initialize() {
|
|
|
61 |
}
|
|
|
62 |
/**
|
|
|
63 |
* Execution method always used for tasks
|
|
|
64 |
*
|
|
|
65 |
* @access public
|
|
|
66 |
*/
|
|
|
67 |
function execute() {
|
|
|
68 |
if (empty($this->args)) {
|
|
|
69 |
$this->__interactive();
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
if (isset($this->args[0])) {
|
|
|
73 |
$controller = Inflector::camelize($this->args[0]);
|
|
|
74 |
$actions = null;
|
|
|
75 |
if (isset($this->args[1]) && $this->args[1] == 'scaffold') {
|
|
|
76 |
$this->out('Baking scaffold for ' . $controller);
|
|
|
77 |
$actions = $this->bakeActions($controller);
|
|
|
78 |
} else {
|
|
|
79 |
$actions = 'scaffold';
|
|
|
80 |
}
|
|
|
81 |
if ((isset($this->args[1]) && $this->args[1] == 'admin') || (isset($this->args[2]) && $this->args[2] == 'admin')) {
|
|
|
82 |
if ($admin = $this->getAdmin()) {
|
|
|
83 |
$this->out('Adding ' . Configure::read('Routing.admin') .' methods');
|
|
|
84 |
if ($actions == 'scaffold') {
|
|
|
85 |
$actions = $this->bakeActions($controller, $admin);
|
|
|
86 |
} else {
|
|
|
87 |
$actions .= $this->bakeActions($controller, $admin);
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
}
|
|
|
91 |
if ($this->bake($controller, $actions)) {
|
|
|
92 |
if ($this->_checkUnitTest()) {
|
|
|
93 |
$this->bakeTest($controller);
|
|
|
94 |
}
|
|
|
95 |
}
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
/**
|
|
|
99 |
* Interactive
|
|
|
100 |
*
|
|
|
101 |
* @access private
|
|
|
102 |
*/
|
|
|
103 |
function __interactive($controllerName = false) {
|
|
|
104 |
if (!$controllerName) {
|
|
|
105 |
$this->interactive = true;
|
|
|
106 |
$this->hr();
|
|
|
107 |
$this->out(sprintf("Bake Controller\nPath: %s", $this->path));
|
|
|
108 |
$this->hr();
|
|
|
109 |
$actions = '';
|
|
|
110 |
$uses = array();
|
|
|
111 |
$helpers = array();
|
|
|
112 |
$components = array();
|
|
|
113 |
$wannaUseSession = 'y';
|
|
|
114 |
$wannaDoAdmin = 'n';
|
|
|
115 |
$wannaUseScaffold = 'n';
|
|
|
116 |
$wannaDoScaffolding = 'y';
|
|
|
117 |
$controllerName = $this->getName();
|
|
|
118 |
}
|
|
|
119 |
$this->hr();
|
|
|
120 |
$this->out("Baking {$controllerName}Controller");
|
|
|
121 |
$this->hr();
|
|
|
122 |
|
|
|
123 |
$controllerFile = low(Inflector::underscore($controllerName));
|
|
|
124 |
|
|
|
125 |
$question[] = __("Would you like to build your controller interactively?", true);
|
|
|
126 |
if (file_exists($this->path . $controllerFile .'_controller.php')) {
|
|
|
127 |
$question[] = sprintf(__("Warning: Choosing no will overwrite the %sController.", true), $controllerName);
|
|
|
128 |
}
|
|
|
129 |
$doItInteractive = $this->in(join("\n", $question), array('y','n'), 'y');
|
|
|
130 |
|
|
|
131 |
if (low($doItInteractive) == 'y' || low($doItInteractive) == 'yes') {
|
|
|
132 |
$this->interactive = true;
|
|
|
133 |
|
|
|
134 |
$wannaUseScaffold = $this->in(__("Would you like to use scaffolding?", true), array('y','n'), 'n');
|
|
|
135 |
|
|
|
136 |
if (low($wannaUseScaffold) == 'n' || low($wannaUseScaffold) == 'no') {
|
|
|
137 |
|
|
|
138 |
$wannaDoScaffolding = $this->in(__("Would you like to include some basic class methods (index(), add(), view(), edit())?", true), array('y','n'), 'n');
|
|
|
139 |
|
|
|
140 |
if (low($wannaDoScaffolding) == 'y' || low($wannaDoScaffolding) == 'yes') {
|
|
|
141 |
$wannaDoAdmin = $this->in(__("Would you like to create the methods for admin routing?", true), array('y','n'), 'n');
|
|
|
142 |
}
|
|
|
143 |
|
|
|
144 |
$wannaDoHelpers = $this->in(__("Would you like this controller to use other helpers besides HtmlHelper and FormHelper?", true), array('y','n'), 'n');
|
|
|
145 |
|
|
|
146 |
if (low($wannaDoHelpers) == 'y' || low($wannaDoHelpers) == 'yes') {
|
|
|
147 |
$helpersList = $this->in(__("Please provide a comma separated list of the other helper names you'd like to use.\nExample: 'Ajax, Javascript, Time'", true));
|
|
|
148 |
$helpersListTrimmed = str_replace(' ', '', $helpersList);
|
|
|
149 |
$helpers = explode(',', $helpersListTrimmed);
|
|
|
150 |
}
|
|
|
151 |
$wannaDoComponents = $this->in(__("Would you like this controller to use any components?", true), array('y','n'), 'n');
|
|
|
152 |
|
|
|
153 |
if (low($wannaDoComponents) == 'y' || low($wannaDoComponents) == 'yes') {
|
|
|
154 |
$componentsList = $this->in(__("Please provide a comma separated list of the component names you'd like to use.\nExample: 'Acl, Security, RequestHandler'", true));
|
|
|
155 |
$componentsListTrimmed = str_replace(' ', '', $componentsList);
|
|
|
156 |
$components = explode(',', $componentsListTrimmed);
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
$wannaUseSession = $this->in(__("Would you like to use Sessions?", true), array('y','n'), 'y');
|
|
|
160 |
} else {
|
|
|
161 |
$wannaDoScaffolding = 'n';
|
|
|
162 |
}
|
|
|
163 |
} else {
|
|
|
164 |
$wannaDoScaffolding = $this->in(__("Would you like to include some basic class methods (index(), add(), view(), edit())?", true), array('y','n'), 'y');
|
|
|
165 |
|
|
|
166 |
if (low($wannaDoScaffolding) == 'y' || low($wannaDoScaffolding) == 'yes') {
|
|
|
167 |
$wannaDoAdmin = $this->in(__("Would you like to create the methods for admin routing?", true), array('y','n'), 'y');
|
|
|
168 |
}
|
|
|
169 |
}
|
|
|
170 |
$admin = false;
|
|
|
171 |
|
|
|
172 |
if ((low($wannaDoAdmin) == 'y' || low($wannaDoAdmin) == 'yes')) {
|
|
|
173 |
$admin = $this->getAdmin();
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
if (low($wannaDoScaffolding) == 'y' || low($wannaDoScaffolding) == 'yes') {
|
|
|
177 |
$actions = $this->bakeActions($controllerName, null, in_array(low($wannaUseSession), array('y', 'yes')));
|
|
|
178 |
if ($admin) {
|
|
|
179 |
$actions .= $this->bakeActions($controllerName, $admin, in_array(low($wannaUseSession), array('y', 'yes')));
|
|
|
180 |
}
|
|
|
181 |
}
|
|
|
182 |
|
|
|
183 |
if ($this->interactive === true) {
|
|
|
184 |
$this->out('');
|
|
|
185 |
$this->hr();
|
|
|
186 |
$this->out('The following controller will be created:');
|
|
|
187 |
$this->hr();
|
|
|
188 |
$this->out("Controller Name: $controllerName");
|
|
|
189 |
|
|
|
190 |
if (low($wannaUseScaffold) == 'y' || low($wannaUseScaffold) == 'yes') {
|
|
|
191 |
$this->out(" var \$scaffold;");
|
|
|
192 |
$actions = 'scaffold';
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
if (count($helpers)) {
|
|
|
196 |
$this->out("Helpers: ", false);
|
|
|
197 |
|
|
|
198 |
foreach ($helpers as $help) {
|
|
|
199 |
if ($help != $helpers[count($helpers) - 1]) {
|
|
|
200 |
$this->out(ucfirst($help) . ", ", false);
|
|
|
201 |
} else {
|
|
|
202 |
$this->out(ucfirst($help));
|
|
|
203 |
}
|
|
|
204 |
}
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
if (count($components)) {
|
|
|
208 |
$this->out("Components: ", false);
|
|
|
209 |
|
|
|
210 |
foreach ($components as $comp) {
|
|
|
211 |
if ($comp != $components[count($components) - 1]) {
|
|
|
212 |
$this->out(ucfirst($comp) . ", ", false);
|
|
|
213 |
} else {
|
|
|
214 |
$this->out(ucfirst($comp));
|
|
|
215 |
}
|
|
|
216 |
}
|
|
|
217 |
}
|
|
|
218 |
$this->hr();
|
|
|
219 |
$looksGood = $this->in(__('Look okay?', true), array('y','n'), 'y');
|
|
|
220 |
|
|
|
221 |
if (low($looksGood) == 'y' || low($looksGood) == 'yes') {
|
|
|
222 |
$baked = $this->bake($controllerName, $actions, $helpers, $components, $uses);
|
|
|
223 |
if ($baked && $this->_checkUnitTest()) {
|
|
|
224 |
$this->bakeTest($controllerName);
|
|
|
225 |
}
|
|
|
226 |
} else {
|
|
|
227 |
$this->__interactive($controllerName);
|
|
|
228 |
}
|
|
|
229 |
} else {
|
|
|
230 |
$baked = $this->bake($controllerName, $actions, $helpers, $components, $uses);
|
|
|
231 |
if ($baked && $this->_checkUnitTest()) {
|
|
|
232 |
$this->bakeTest($controllerName);
|
|
|
233 |
}
|
|
|
234 |
}
|
|
|
235 |
}
|
|
|
236 |
/**
|
|
|
237 |
* Bake scaffold actions
|
|
|
238 |
*
|
|
|
239 |
* @param string $controllerName Controller name
|
|
|
240 |
* @param string $admin Admin route to use
|
|
|
241 |
* @param boolean $wannaUseSession Set to true to use sessions, false otherwise
|
|
|
242 |
* @return string Baked actions
|
|
|
243 |
* @access private
|
|
|
244 |
*/
|
|
|
245 |
function bakeActions($controllerName, $admin = null, $wannaUseSession = true) {
|
|
|
246 |
$currentModelName = $this->_modelName($controllerName);
|
|
|
247 |
if (!App::import('Model', $currentModelName)) {
|
|
|
248 |
$this->err(__('You must have a model for this class to build scaffold methods. Please try again.', true));
|
|
|
249 |
exit;
|
|
|
250 |
}
|
|
|
251 |
$actions = null;
|
|
|
252 |
$modelObj =& new $currentModelName();
|
|
|
253 |
$controllerPath = $this->_controllerPath($controllerName);
|
|
|
254 |
$pluralName = $this->_pluralName($currentModelName);
|
|
|
255 |
$singularName = Inflector::variable($currentModelName);
|
|
|
256 |
$singularHumanName = Inflector::humanize($currentModelName);
|
|
|
257 |
$pluralHumanName = Inflector::humanize($controllerName);
|
|
|
258 |
$actions .= "\n";
|
|
|
259 |
$actions .= "\tfunction {$admin}index() {\n";
|
|
|
260 |
$actions .= "\t\t\$this->{$currentModelName}->recursive = 0;\n";
|
|
|
261 |
$actions .= "\t\t\$this->set('{$pluralName}', \$this->paginate());\n";
|
|
|
262 |
$actions .= "\t}\n";
|
|
|
263 |
$actions .= "\n";
|
|
|
264 |
$actions .= "\tfunction {$admin}view(\$id = null) {\n";
|
|
|
265 |
$actions .= "\t\tif (!\$id) {\n";
|
|
|
266 |
if ($wannaUseSession) {
|
|
|
267 |
$actions .= "\t\t\t\$this->Session->setFlash(__('Invalid {$singularHumanName}.', true));\n";
|
|
|
268 |
$actions .= "\t\t\t\$this->redirect(array('action'=>'index'));\n";
|
|
|
269 |
} else {
|
|
|
270 |
$actions .= "\t\t\t\$this->flash(__('Invalid {$singularHumanName}', true), array('action'=>'index'));\n";
|
|
|
271 |
}
|
|
|
272 |
$actions .= "\t\t}\n";
|
|
|
273 |
$actions .= "\t\t\$this->set('".$singularName."', \$this->{$currentModelName}->read(null, \$id));\n";
|
|
|
274 |
$actions .= "\t}\n";
|
|
|
275 |
$actions .= "\n";
|
|
|
276 |
|
|
|
277 |
/* ADD ACTION */
|
|
|
278 |
$compact = array();
|
|
|
279 |
$actions .= "\tfunction {$admin}add() {\n";
|
|
|
280 |
$actions .= "\t\tif (!empty(\$this->data)) {\n";
|
|
|
281 |
$actions .= "\t\t\t\$this->{$currentModelName}->create();\n";
|
|
|
282 |
$actions .= "\t\t\tif (\$this->{$currentModelName}->save(\$this->data)) {\n";
|
|
|
283 |
if ($wannaUseSession) {
|
|
|
284 |
$actions .= "\t\t\t\t\$this->Session->setFlash(__('The ".$singularHumanName." has been saved', true));\n";
|
|
|
285 |
$actions .= "\t\t\t\t\$this->redirect(array('action'=>'index'));\n";
|
|
|
286 |
} else {
|
|
|
287 |
$actions .= "\t\t\t\t\$this->flash(__('{$currentModelName} saved.', true), array('action'=>'index'));\n";
|
|
|
288 |
}
|
|
|
289 |
$actions .= "\t\t\t} else {\n";
|
|
|
290 |
if ($wannaUseSession) {
|
|
|
291 |
$actions .= "\t\t\t\t\$this->Session->setFlash(__('The {$singularHumanName} could not be saved. Please, try again.', true));\n";
|
|
|
292 |
}
|
|
|
293 |
$actions .= "\t\t\t}\n";
|
|
|
294 |
$actions .= "\t\t}\n";
|
|
|
295 |
foreach ($modelObj->hasAndBelongsToMany as $associationName => $relation) {
|
|
|
296 |
if (!empty($associationName)) {
|
|
|
297 |
$habtmModelName = $this->_modelName($associationName);
|
|
|
298 |
$habtmSingularName = $this->_singularName($associationName);
|
|
|
299 |
$habtmPluralName = $this->_pluralName($associationName);
|
|
|
300 |
$actions .= "\t\t\${$habtmPluralName} = \$this->{$currentModelName}->{$habtmModelName}->find('list');\n";
|
|
|
301 |
$compact[] = "'{$habtmPluralName}'";
|
|
|
302 |
}
|
|
|
303 |
}
|
|
|
304 |
foreach ($modelObj->belongsTo as $associationName => $relation) {
|
|
|
305 |
if (!empty($associationName)) {
|
|
|
306 |
$belongsToModelName = $this->_modelName($associationName);
|
|
|
307 |
$belongsToPluralName = $this->_pluralName($associationName);
|
|
|
308 |
$actions .= "\t\t\${$belongsToPluralName} = \$this->{$currentModelName}->{$belongsToModelName}->find('list');\n";
|
|
|
309 |
$compact[] = "'{$belongsToPluralName}'";
|
|
|
310 |
}
|
|
|
311 |
}
|
|
|
312 |
if (!empty($compact)) {
|
|
|
313 |
$actions .= "\t\t\$this->set(compact(".join(', ', $compact)."));\n";
|
|
|
314 |
}
|
|
|
315 |
$actions .= "\t}\n";
|
|
|
316 |
$actions .= "\n";
|
|
|
317 |
|
|
|
318 |
/* EDIT ACTION */
|
|
|
319 |
$compact = array();
|
|
|
320 |
$actions .= "\tfunction {$admin}edit(\$id = null) {\n";
|
|
|
321 |
$actions .= "\t\tif (!\$id && empty(\$this->data)) {\n";
|
|
|
322 |
if ($wannaUseSession) {
|
|
|
323 |
$actions .= "\t\t\t\$this->Session->setFlash(__('Invalid {$singularHumanName}', true));\n";
|
|
|
324 |
$actions .= "\t\t\t\$this->redirect(array('action'=>'index'));\n";
|
|
|
325 |
} else {
|
|
|
326 |
$actions .= "\t\t\t\$this->flash(__('Invalid {$singularHumanName}', true), array('action'=>'index'));\n";
|
|
|
327 |
}
|
|
|
328 |
$actions .= "\t\t}\n";
|
|
|
329 |
$actions .= "\t\tif (!empty(\$this->data)) {\n";
|
|
|
330 |
$actions .= "\t\t\tif (\$this->{$currentModelName}->save(\$this->data)) {\n";
|
|
|
331 |
if ($wannaUseSession) {
|
|
|
332 |
$actions .= "\t\t\t\t\$this->Session->setFlash(__('The ".$singularHumanName." has been saved', true));\n";
|
|
|
333 |
$actions .= "\t\t\t\t\$this->redirect(array('action'=>'index'));\n";
|
|
|
334 |
} else {
|
|
|
335 |
$actions .= "\t\t\t\t\$this->flash(__('The ".$singularHumanName." has been saved.', true), array('action'=>'index'));\n";
|
|
|
336 |
}
|
|
|
337 |
$actions .= "\t\t\t} else {\n";
|
|
|
338 |
if ($wannaUseSession) {
|
|
|
339 |
$actions .= "\t\t\t\t\$this->Session->setFlash(__('The {$singularHumanName} could not be saved. Please, try again.', true));\n";
|
|
|
340 |
}
|
|
|
341 |
$actions .= "\t\t\t}\n";
|
|
|
342 |
$actions .= "\t\t}\n";
|
|
|
343 |
$actions .= "\t\tif (empty(\$this->data)) {\n";
|
|
|
344 |
$actions .= "\t\t\t\$this->data = \$this->{$currentModelName}->read(null, \$id);\n";
|
|
|
345 |
$actions .= "\t\t}\n";
|
|
|
346 |
|
|
|
347 |
foreach ($modelObj->hasAndBelongsToMany as $associationName => $relation) {
|
|
|
348 |
if (!empty($associationName)) {
|
|
|
349 |
$habtmModelName = $this->_modelName($associationName);
|
|
|
350 |
$habtmSingularName = $this->_singularName($associationName);
|
|
|
351 |
$habtmPluralName = $this->_pluralName($associationName);
|
|
|
352 |
$actions .= "\t\t\${$habtmPluralName} = \$this->{$currentModelName}->{$habtmModelName}->find('list');\n";
|
|
|
353 |
$compact[] = "'{$habtmPluralName}'";
|
|
|
354 |
}
|
|
|
355 |
}
|
|
|
356 |
foreach ($modelObj->belongsTo as $associationName => $relation) {
|
|
|
357 |
if (!empty($associationName)) {
|
|
|
358 |
$belongsToModelName = $this->_modelName($associationName);
|
|
|
359 |
$belongsToPluralName = $this->_pluralName($associationName);
|
|
|
360 |
$actions .= "\t\t\${$belongsToPluralName} = \$this->{$currentModelName}->{$belongsToModelName}->find('list');\n";
|
|
|
361 |
$compact[] = "'{$belongsToPluralName}'";
|
|
|
362 |
}
|
|
|
363 |
}
|
|
|
364 |
if (!empty($compact)) {
|
|
|
365 |
$actions .= "\t\t\$this->set(compact(".join(',', $compact)."));\n";
|
|
|
366 |
}
|
|
|
367 |
$actions .= "\t}\n";
|
|
|
368 |
$actions .= "\n";
|
|
|
369 |
$actions .= "\tfunction {$admin}delete(\$id = null) {\n";
|
|
|
370 |
$actions .= "\t\tif (!\$id) {\n";
|
|
|
371 |
if ($wannaUseSession) {
|
|
|
372 |
$actions .= "\t\t\t\$this->Session->setFlash(__('Invalid id for {$singularHumanName}', true));\n";
|
|
|
373 |
$actions .= "\t\t\t\$this->redirect(array('action'=>'index'));\n";
|
|
|
374 |
} else {
|
|
|
375 |
$actions .= "\t\t\t\$this->flash(__('Invalid {$singularHumanName}', true), array('action'=>'index'));\n";
|
|
|
376 |
}
|
|
|
377 |
$actions .= "\t\t}\n";
|
|
|
378 |
$actions .= "\t\tif (\$this->{$currentModelName}->del(\$id)) {\n";
|
|
|
379 |
if ($wannaUseSession) {
|
|
|
380 |
$actions .= "\t\t\t\$this->Session->setFlash(__('{$singularHumanName} deleted', true));\n";
|
|
|
381 |
$actions .= "\t\t\t\$this->redirect(array('action'=>'index'));\n";
|
|
|
382 |
} else {
|
|
|
383 |
$actions .= "\t\t\t\$this->flash(__('{$singularHumanName} deleted', true), array('action'=>'index'));\n";
|
|
|
384 |
}
|
|
|
385 |
$actions .= "\t\t}\n";
|
|
|
386 |
$actions .= "\t}\n";
|
|
|
387 |
$actions .= "\n";
|
|
|
388 |
return $actions;
|
|
|
389 |
}
|
|
|
390 |
|
|
|
391 |
|
|
|
392 |
/**
|
|
|
393 |
* Assembles and writes a Controller file
|
|
|
394 |
*
|
|
|
395 |
* @param string $controllerName Controller name
|
|
|
396 |
* @param string $actions Actions to add, or set the whole controller to use $scaffold (set $actions to 'scaffold')
|
|
|
397 |
* @param array $helpers Helpers to use in controller
|
|
|
398 |
* @param array $components Components to use in controller
|
|
|
399 |
* @param array $uses Models to use in controller
|
|
|
400 |
* @return string Baked controller
|
|
|
401 |
* @access private
|
|
|
402 |
*/
|
|
|
403 |
function bake($controllerName, $actions = '', $helpers = null, $components = null, $uses = null) {
|
|
|
404 |
$out = "<?php\n";
|
|
|
405 |
$out .= "class $controllerName" . "Controller extends {$this->plugin}AppController {\n\n";
|
|
|
406 |
$out .= "\tvar \$name = '$controllerName';\n";
|
|
|
407 |
|
|
|
408 |
if (low($actions) == 'scaffold') {
|
|
|
409 |
$out .= "\tvar \$scaffold;\n";
|
|
|
410 |
} else {
|
|
|
411 |
if (count($uses)) {
|
|
|
412 |
$out .= "\tvar \$uses = array('" . $this->_modelName($controllerName) . "', ";
|
|
|
413 |
|
|
|
414 |
foreach ($uses as $use) {
|
|
|
415 |
if ($use != $uses[count($uses) - 1]) {
|
|
|
416 |
$out .= "'" . $this->_modelName($use) . "', ";
|
|
|
417 |
} else {
|
|
|
418 |
$out .= "'" . $this->_modelName($use) . "'";
|
|
|
419 |
}
|
|
|
420 |
}
|
|
|
421 |
$out .= ");\n";
|
|
|
422 |
}
|
|
|
423 |
|
|
|
424 |
$out .= "\tvar \$helpers = array('Html', 'Form'";
|
|
|
425 |
if (count($helpers)) {
|
|
|
426 |
foreach ($helpers as $help) {
|
|
|
427 |
$out .= ", '" . Inflector::camelize($help) . "'";
|
|
|
428 |
}
|
|
|
429 |
}
|
|
|
430 |
$out .= ");\n";
|
|
|
431 |
|
|
|
432 |
if (count($components)) {
|
|
|
433 |
$out .= "\tvar \$components = array(";
|
|
|
434 |
|
|
|
435 |
foreach ($components as $comp) {
|
|
|
436 |
if ($comp != $components[count($components) - 1]) {
|
|
|
437 |
$out .= "'" . Inflector::camelize($comp) . "', ";
|
|
|
438 |
} else {
|
|
|
439 |
$out .= "'" . Inflector::camelize($comp) . "'";
|
|
|
440 |
}
|
|
|
441 |
}
|
|
|
442 |
$out .= ");\n";
|
|
|
443 |
}
|
|
|
444 |
$out .= $actions;
|
|
|
445 |
}
|
|
|
446 |
$out .= "}\n";
|
|
|
447 |
$out .= "?>";
|
|
|
448 |
$filename = $this->path . $this->_controllerPath($controllerName) . '_controller.php';
|
|
|
449 |
return $this->createFile($filename, $out);
|
|
|
450 |
}
|
|
|
451 |
/**
|
|
|
452 |
* Assembles and writes a unit test file
|
|
|
453 |
*
|
|
|
454 |
* @param string $className Controller class name
|
|
|
455 |
* @return string Baked test
|
|
|
456 |
* @access private
|
|
|
457 |
*/
|
|
|
458 |
function bakeTest($className) {
|
|
|
459 |
$import = $className;
|
|
|
460 |
if ($this->plugin) {
|
|
|
461 |
$import = $this->plugin . '.' . $className;
|
|
|
462 |
}
|
|
|
463 |
$out = "App::import('Controller', '$import');\n\n";
|
|
|
464 |
$out .= "class Test{$className} extends {$className}Controller {\n";
|
|
|
465 |
$out .= "\tvar \$autoRender = false;\n}\n\n";
|
|
|
466 |
$out .= "class {$className}ControllerTest extends CakeTestCase {\n";
|
|
|
467 |
$out .= "\tvar \${$className} = null;\n\n";
|
|
|
468 |
$out .= "\tfunction setUp() {\n\t\t\$this->{$className} = new Test{$className}();";
|
|
|
469 |
$out .= "\n\t\t\$this->{$className}->constructClasses();\n\t}\n\n";
|
|
|
470 |
$out .= "\tfunction test{$className}ControllerInstance() {\n";
|
|
|
471 |
$out .= "\t\t\$this->assertTrue(is_a(\$this->{$className}, '{$className}Controller'));\n\t}\n\n";
|
|
|
472 |
$out .= "\tfunction tearDown() {\n\t\tunset(\$this->{$className});\n\t}\n}\n";
|
|
|
473 |
|
|
|
474 |
$path = CONTROLLER_TESTS;
|
|
|
475 |
if (isset($this->plugin)) {
|
|
|
476 |
$pluginPath = 'plugins' . DS . Inflector::underscore($this->plugin) . DS;
|
|
|
477 |
$path = APP . $pluginPath . 'tests' . DS . 'cases' . DS . 'controllers' . DS;
|
|
|
478 |
}
|
|
|
479 |
|
|
|
480 |
$filename = Inflector::underscore($className).'_controller.test.php';
|
|
|
481 |
$this->out("\nBaking unit test for $className...");
|
|
|
482 |
|
|
|
483 |
$header = '$Id';
|
|
|
484 |
$content = "<?php \n/* SVN FILE: $header$ */\n/* ". $className ."Controller Test cases generated on: " . date('Y-m-d H:m:s') . " : ". time() . "*/\n{$out}?>";
|
|
|
485 |
return $this->createFile($path . $filename, $content);
|
|
|
486 |
}
|
|
|
487 |
/**
|
|
|
488 |
* Outputs and gets the list of possible models or controllers from database
|
|
|
489 |
*
|
|
|
490 |
* @param string $useDbConfig Database configuration name
|
|
|
491 |
* @return array Set of controllers
|
|
|
492 |
* @access public
|
|
|
493 |
*/
|
|
|
494 |
function listAll($useDbConfig = 'default') {
|
|
|
495 |
$db =& ConnectionManager::getDataSource($useDbConfig);
|
|
|
496 |
$usePrefix = empty($db->config['prefix']) ? '' : $db->config['prefix'];
|
|
|
497 |
if ($usePrefix) {
|
|
|
498 |
$tables = array();
|
|
|
499 |
foreach ($db->listSources() as $table) {
|
|
|
500 |
if (!strncmp($table, $usePrefix, strlen($usePrefix))) {
|
|
|
501 |
$tables[] = substr($table, strlen($usePrefix));
|
|
|
502 |
}
|
|
|
503 |
}
|
|
|
504 |
} else {
|
|
|
505 |
$tables = $db->listSources();
|
|
|
506 |
}
|
|
|
507 |
|
|
|
508 |
if (empty($tables)) {
|
|
|
509 |
$this->err(__('Your database does not have any tables.', true));
|
|
|
510 |
$this->_stop();
|
|
|
511 |
}
|
|
|
512 |
|
|
|
513 |
$this->__tables = $tables;
|
|
|
514 |
$this->out('Possible Controllers based on your current database:');
|
|
|
515 |
$this->_controllerNames = array();
|
|
|
516 |
$count = count($tables);
|
|
|
517 |
for ($i = 0; $i < $count; $i++) {
|
|
|
518 |
$this->_controllerNames[] = $this->_controllerName($this->_modelName($tables[$i]));
|
|
|
519 |
$this->out($i + 1 . ". " . $this->_controllerNames[$i]);
|
|
|
520 |
}
|
|
|
521 |
return $this->_controllerNames;
|
|
|
522 |
}
|
|
|
523 |
|
|
|
524 |
/**
|
|
|
525 |
* Forces the user to specify the controller he wants to bake, and returns the selected controller name.
|
|
|
526 |
*
|
|
|
527 |
* @return string Controller name
|
|
|
528 |
* @access public
|
|
|
529 |
*/
|
|
|
530 |
function getName() {
|
|
|
531 |
$useDbConfig = 'default';
|
|
|
532 |
$controllers = $this->listAll($useDbConfig, 'Controllers');
|
|
|
533 |
$enteredController = '';
|
|
|
534 |
|
|
|
535 |
while ($enteredController == '') {
|
|
|
536 |
$enteredController = $this->in(__("Enter a number from the list above, type in the name of another controller, or 'q' to exit", true), null, 'q');
|
|
|
537 |
|
|
|
538 |
if ($enteredController === 'q') {
|
|
|
539 |
$this->out(__("Exit", true));
|
|
|
540 |
$this->_stop();
|
|
|
541 |
}
|
|
|
542 |
|
|
|
543 |
if ($enteredController == '' || intval($enteredController) > count($controllers)) {
|
|
|
544 |
$this->out(__('Error:', true));
|
|
|
545 |
$this->out(__("The Controller name you supplied was empty, or the number \nyou selected was not an option. Please try again.", true));
|
|
|
546 |
$enteredController = '';
|
|
|
547 |
}
|
|
|
548 |
}
|
|
|
549 |
|
|
|
550 |
if (intval($enteredController) > 0 && intval($enteredController) <= count($controllers) ) {
|
|
|
551 |
$controllerName = $controllers[intval($enteredController) - 1];
|
|
|
552 |
} else {
|
|
|
553 |
$controllerName = Inflector::camelize($enteredController);
|
|
|
554 |
}
|
|
|
555 |
|
|
|
556 |
return $controllerName;
|
|
|
557 |
}
|
|
|
558 |
/**
|
|
|
559 |
* Displays help contents
|
|
|
560 |
*
|
|
|
561 |
* @access public
|
|
|
562 |
*/
|
|
|
563 |
function help() {
|
|
|
564 |
$this->hr();
|
|
|
565 |
$this->out("Usage: cake bake controller <arg1> <arg2>...");
|
|
|
566 |
$this->hr();
|
|
|
567 |
$this->out('Commands:');
|
|
|
568 |
$this->out("\n\tcontroller <name>\n\t\tbakes controller with var \$scaffold");
|
|
|
569 |
$this->out("\n\tcontroller <name> scaffold\n\t\tbakes controller with scaffold actions.\n\t\t(index, view, add, edit, delete)");
|
|
|
570 |
$this->out("\n\tcontroller <name> scaffold admin\n\t\tbakes a controller with scaffold actions for both public and Configure::read('Routing.admin')");
|
|
|
571 |
$this->out("\n\tcontroller <name> admin\n\t\tbakes a controller with scaffold actions only for Configure::read('Routing.admin')");
|
|
|
572 |
$this->out("");
|
|
|
573 |
$this->_stop();
|
|
|
574 |
}
|
|
|
575 |
}
|
|
|
576 |
?>
|