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