1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19:
20: App::uses('AppShell', 'Console/Command');
21: App::uses('BakeTask', 'Console/Command/Task');
22: App::uses('AppModel', 'Model');
23:
24: 25: 26: 27: 28:
29: class ControllerTask extends BakeTask {
30:
31: 32: 33: 34: 35:
36: public $tasks = array('Model', 'Test', 'Template', 'DbConfig', 'Project');
37:
38: 39: 40: 41: 42:
43: public $path = null;
44:
45: 46: 47: 48: 49:
50: public function initialize() {
51: $this->path = current(App::path('Controller'));
52: }
53:
54: 55: 56: 57: 58:
59: public function execute() {
60: parent::execute();
61: if (empty($this->args)) {
62: return $this->_interactive();
63: }
64:
65: if (isset($this->args[0])) {
66: if (!isset($this->connection)) {
67: $this->connection = 'default';
68: }
69: if (strtolower($this->args[0]) === 'all') {
70: return $this->all();
71: }
72:
73: $controller = $this->_controllerName($this->args[0]);
74: $actions = '';
75:
76: if (!empty($this->params['public'])) {
77: $this->out(__d('cake_console', 'Baking basic crud methods for ') . $controller);
78: $actions .= $this->bakeActions($controller);
79: }
80: if (!empty($this->params['admin'])) {
81: $admin = $this->Project->getPrefix();
82: if ($admin) {
83: $this->out(__d('cake_console', 'Adding %s methods', $admin));
84: $actions .= "\n" . $this->bakeActions($controller, $admin);
85: }
86: }
87: if (empty($actions)) {
88: $actions = 'scaffold';
89: }
90:
91: if ($this->bake($controller, $actions)) {
92: if ($this->_checkUnitTest()) {
93: $this->bakeTest($controller);
94: }
95: }
96: }
97: }
98:
99: 100: 101: 102: 103:
104: public function all() {
105: $this->interactive = false;
106: $this->listAll($this->connection, false);
107: ClassRegistry::config('Model', array('ds' => $this->connection));
108: $unitTestExists = $this->_checkUnitTest();
109:
110: $admin = false;
111: if (!empty($this->params['admin'])) {
112: $admin = $this->Project->getPrefix();
113: }
114:
115: foreach ($this->__tables as $table) {
116: $model = $this->_modelName($table);
117: $controller = $this->_controllerName($model);
118: App::uses($model, 'Model');
119: if (class_exists($model)) {
120: $actions = $this->bakeActions($controller);
121: if ($admin) {
122: $this->out(__d('cake_console', 'Adding %s methods', $admin));
123: $actions .= "\n" . $this->bakeActions($controller, $admin);
124: }
125: if ($this->bake($controller, $actions) && $unitTestExists) {
126: $this->bakeTest($controller);
127: }
128: }
129: }
130: }
131:
132: 133: 134: 135: 136:
137: protected function _interactive() {
138: $this->interactive = true;
139: $this->hr();
140: $this->out(__d('cake_console', "Bake Controller\nPath: %s", $this->getPath()));
141: $this->hr();
142:
143: if (empty($this->connection)) {
144: $this->connection = $this->DbConfig->getConfig();
145: }
146:
147: $controllerName = $this->getName();
148: $this->hr();
149: $this->out(__d('cake_console', 'Baking %sController', $controllerName));
150: $this->hr();
151:
152: $helpers = $components = array();
153: $actions = '';
154: $wannaUseSession = 'y';
155: $wannaBakeAdminCrud = 'n';
156: $useDynamicScaffold = 'n';
157: $wannaBakeCrud = 'y';
158:
159: $question[] = __d('cake_console', "Would you like to build your controller interactively?");
160: if (file_exists($this->path . $controllerName . 'Controller.php')) {
161: $question[] = __d('cake_console', "Warning: Choosing no will overwrite the %sController.", $controllerName);
162: }
163: $doItInteractive = $this->in(implode("\n", $question), array('y', 'n'), 'y');
164:
165: if (strtolower($doItInteractive) === 'y') {
166: $this->interactive = true;
167: $useDynamicScaffold = $this->in(
168: __d('cake_console', "Would you like to use dynamic scaffolding?"), array('y', 'n'), 'n'
169: );
170:
171: if (strtolower($useDynamicScaffold) === 'y') {
172: $wannaBakeCrud = 'n';
173: $actions = 'scaffold';
174: } else {
175: list($wannaBakeCrud, $wannaBakeAdminCrud) = $this->_askAboutMethods();
176:
177: $helpers = $this->doHelpers();
178: $components = $this->doComponents();
179:
180: $wannaUseSession = $this->in(
181: __d('cake_console', "Would you like to use Session flash messages?"), array('y', 'n'), 'y'
182: );
183: }
184: } else {
185: list($wannaBakeCrud, $wannaBakeAdminCrud) = $this->_askAboutMethods();
186: }
187:
188: if (strtolower($wannaBakeCrud) === 'y') {
189: $actions = $this->bakeActions($controllerName, null, strtolower($wannaUseSession) === 'y');
190: }
191: if (strtolower($wannaBakeAdminCrud) === 'y') {
192: $admin = $this->Project->getPrefix();
193: $actions .= $this->bakeActions($controllerName, $admin, strtolower($wannaUseSession) === 'y');
194: }
195:
196: $baked = false;
197: if ($this->interactive === true) {
198: $this->confirmController($controllerName, $useDynamicScaffold, $helpers, $components);
199: $looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n'), 'y');
200:
201: if (strtolower($looksGood) === 'y') {
202: $baked = $this->bake($controllerName, $actions, $helpers, $components);
203: if ($baked && $this->_checkUnitTest()) {
204: $this->bakeTest($controllerName);
205: }
206: }
207: } else {
208: $baked = $this->bake($controllerName, $actions, $helpers, $components);
209: if ($baked && $this->_checkUnitTest()) {
210: $this->bakeTest($controllerName);
211: }
212: }
213: return $baked;
214: }
215:
216: 217: 218: 219: 220: 221: 222: 223: 224:
225: public function confirmController($controllerName, $useDynamicScaffold, $helpers, $components) {
226: $this->out();
227: $this->hr();
228: $this->out(__d('cake_console', 'The following controller will be created:'));
229: $this->hr();
230: $this->out(__d('cake_console', "Controller Name:\n\t%s", $controllerName));
231:
232: if (strtolower($useDynamicScaffold) === 'y') {
233: $this->out("public \$scaffold;");
234: }
235:
236: $properties = array(
237: 'helpers' => __d('cake_console', 'Helpers:'),
238: 'components' => __d('cake_console', 'Components:'),
239: );
240:
241: foreach ($properties as $var => $title) {
242: if (count($$var)) {
243: $output = '';
244: $length = count($$var);
245: foreach ($$var as $i => $propElement) {
246: if ($i != $length - 1) {
247: $output .= ucfirst($propElement) . ', ';
248: } else {
249: $output .= ucfirst($propElement);
250: }
251: }
252: $this->out($title . "\n\t" . $output);
253: }
254: }
255: $this->hr();
256: }
257:
258: 259: 260: 261: 262:
263: protected function _askAboutMethods() {
264: $wannaBakeCrud = $this->in(
265: __d('cake_console', "Would you like to create some basic class methods \n(index(), add(), view(), edit())?"),
266: array('y', 'n'), 'n'
267: );
268: $wannaBakeAdminCrud = $this->in(
269: __d('cake_console', "Would you like to create the basic class methods for admin routing?"),
270: array('y', 'n'), 'n'
271: );
272: return array($wannaBakeCrud, $wannaBakeAdminCrud);
273: }
274:
275: 276: 277: 278: 279: 280: 281: 282:
283: public function bakeActions($controllerName, $admin = null, $wannaUseSession = true) {
284: $currentModelName = $modelImport = $this->_modelName($controllerName);
285: $plugin = $this->plugin;
286: if ($plugin) {
287: $plugin .= '.';
288: }
289: App::uses($modelImport, $plugin . 'Model');
290: if (!class_exists($modelImport)) {
291: $this->err(__d('cake_console', 'You must have a model for this class to build basic methods. Please try again.'));
292: return $this->_stop();
293: }
294:
295: $modelObj = ClassRegistry::init($currentModelName);
296: $controllerPath = $this->_controllerPath($controllerName);
297: $pluralName = $this->_pluralName($currentModelName);
298: $singularName = Inflector::variable($currentModelName);
299: $singularHumanName = $this->_singularHumanName($controllerName);
300: $pluralHumanName = $this->_pluralName($controllerName);
301: $displayField = $modelObj->displayField;
302: $primaryKey = $modelObj->primaryKey;
303:
304: $this->Template->set(compact(
305: 'plugin', 'admin', 'controllerPath', 'pluralName', 'singularName',
306: 'singularHumanName', 'pluralHumanName', 'modelObj', 'wannaUseSession', 'currentModelName',
307: 'displayField', 'primaryKey'
308: ));
309: $actions = $this->Template->generate('actions', 'controller_actions');
310: return $actions;
311: }
312:
313: 314: 315: 316: 317: 318: 319: 320: 321:
322: public function bake($controllerName, $actions = '', $helpers = null, $components = null) {
323: $this->out("\n" . __d('cake_console', 'Baking controller class for %s...', $controllerName), 1, Shell::QUIET);
324:
325: $isScaffold = ($actions === 'scaffold') ? true : false;
326:
327: $this->Template->set(array(
328: 'plugin' => $this->plugin,
329: 'pluginPath' => empty($this->plugin) ? '' : $this->plugin . '.'
330: ));
331:
332: if (!in_array('Paginator', (array)$components)) {
333: $components[] = 'Paginator';
334: }
335:
336: $this->Template->set(compact('controllerName', 'actions', 'helpers', 'components', 'isScaffold'));
337: $contents = $this->Template->generate('classes', 'controller');
338:
339: $path = $this->getPath();
340: $filename = $path . $controllerName . 'Controller.php';
341: if ($this->createFile($filename, $contents)) {
342: return $contents;
343: }
344: return false;
345: }
346:
347: 348: 349: 350: 351: 352:
353: public function bakeTest($className) {
354: $this->Test->plugin = $this->plugin;
355: $this->Test->connection = $this->connection;
356: $this->Test->interactive = $this->interactive;
357: return $this->Test->bake('Controller', $className);
358: }
359:
360: 361: 362: 363: 364:
365: public function doHelpers() {
366: return $this->_doPropertyChoices(
367: __d('cake_console', "Would you like this controller to use other helpers\nbesides HtmlHelper and FormHelper?"),
368: __d('cake_console', "Please provide a comma separated list of the other\nhelper names you'd like to use.\nExample: 'Text, Js, Time'")
369: );
370: }
371:
372: 373: 374: 375: 376:
377: public function doComponents() {
378: $components = array('Paginator');
379: return array_merge($components, $this->_doPropertyChoices(
380: __d('cake_console', "Would you like this controller to use other components\nbesides PaginatorComponent?"),
381: __d('cake_console', "Please provide a comma separated list of the component names you'd like to use.\nExample: 'Acl, Security, RequestHandler'")
382: ));
383: }
384:
385: 386: 387: 388: 389: 390: 391:
392: protected function _doPropertyChoices($prompt, $example) {
393: $proceed = $this->in($prompt, array('y', 'n'), 'n');
394: $property = array();
395: if (strtolower($proceed) === 'y') {
396: $propertyList = $this->in($example);
397: $propertyListTrimmed = str_replace(' ', '', $propertyList);
398: $property = explode(',', $propertyListTrimmed);
399: }
400: return array_filter($property);
401: }
402:
403: 404: 405: 406: 407: 408:
409: public function listAll($useDbConfig = null) {
410: if ($useDbConfig === null) {
411: $useDbConfig = $this->connection;
412: }
413: $this->__tables = $this->Model->getAllTables($useDbConfig);
414:
415: if ($this->interactive) {
416: $this->out(__d('cake_console', 'Possible Controllers based on your current database:'));
417: $this->hr();
418: $this->_controllerNames = array();
419: $count = count($this->__tables);
420: for ($i = 0; $i < $count; $i++) {
421: $this->_controllerNames[] = $this->_controllerName($this->_modelName($this->__tables[$i]));
422: $this->out(sprintf("%2d. %s", $i + 1, $this->_controllerNames[$i]));
423: }
424: return $this->_controllerNames;
425: }
426: return $this->__tables;
427: }
428:
429: 430: 431: 432: 433: 434:
435: public function getName($useDbConfig = null) {
436: $controllers = $this->listAll($useDbConfig);
437: $enteredController = '';
438:
439: while (!$enteredController) {
440: $enteredController = $this->in(__d('cake_console', "Enter a number from the list above,\ntype in the name of another controller, or 'q' to exit"), null, 'q');
441: if ($enteredController === 'q') {
442: $this->out(__d('cake_console', 'Exit'));
443: return $this->_stop();
444: }
445:
446: if (!$enteredController || intval($enteredController) > count($controllers)) {
447: $this->err(__d('cake_console', "The Controller name you supplied was empty,\nor the number you selected was not an option. Please try again."));
448: $enteredController = '';
449: }
450: }
451:
452: if (intval($enteredController) > 0 && intval($enteredController) <= count($controllers)) {
453: $controllerName = $controllers[intval($enteredController) - 1];
454: } else {
455: $controllerName = Inflector::camelize($enteredController);
456: }
457: return $controllerName;
458: }
459:
460: 461: 462: 463: 464:
465: public function getOptionParser() {
466: $parser = parent::getOptionParser();
467: return $parser->description(
468: __d('cake_console', 'Bake a controller for a model. Using options you can bake public, admin or both.')
469: )->addArgument('name', array(
470: 'help' => __d('cake_console', 'Name of the controller to bake. Can use Plugin.name to bake controllers into plugins.')
471: ))->addOption('public', array(
472: 'help' => __d('cake_console', 'Bake a controller with basic crud actions (index, view, add, edit, delete).'),
473: 'boolean' => true
474: ))->addOption('admin', array(
475: 'help' => __d('cake_console', 'Bake a controller with crud actions for one of the Routing.prefixes.'),
476: 'boolean' => true
477: ))->addOption('plugin', array(
478: 'short' => 'p',
479: 'help' => __d('cake_console', 'Plugin to bake the controller into.')
480: ))->addOption('connection', array(
481: 'short' => 'c',
482: 'help' => __d('cake_console', 'The connection the controller\'s model is on.')
483: ))->addSubcommand('all', array(
484: 'help' => __d('cake_console', 'Bake all controllers with CRUD methods.')
485: ))->epilog(__d('cake_console', 'Omitting all arguments and options will enter into an interactive mode.'));
486: }
487:
488: }
489: