Permalink
Browse files

Refactoring code generation into controller.ctp

  • Loading branch information...
1 parent c0f06be commit d31c43d40995ad29c4fa2d68029dad1628fde6b9 @markstory markstory committed May 17, 2009
Showing with 77 additions and 48 deletions.
  1. +13 −48 cake/console/libs/tasks/controller.php
  2. +64 −0 cake/console/libs/templates/objects/controller.ctp
@@ -160,7 +160,10 @@ function __interactive() {
__("Would you like to use dynamic scaffolding?", true), array('y','n'), 'n'
);
- if (strtolower($useDynamicScaffold) == 'n') {
+ if (strtolower($useDynamicScaffold) == 'y') {
+ $wannaBakeCrud = 'n';
+ $actions = 'scaffold';
+ } else {
list($wannaBakeCrud, $wannaBakeCrud) = $this->_askAboutMethods();
$helpers = $this->doHelpers();
@@ -170,8 +173,6 @@ function __interactive() {
$wannaUseSession = $this->in(
__("Would you like to use Session flash messages?", true), array('y','n'), 'y'
);
- } else {
- $wannaBakeCrud = 'n';
}
} else {
list($wannaBakeCrud, $wannaBakeCrud) = $this->_askAboutMethods();
@@ -216,8 +217,7 @@ function confirmController($controllerName, $useDynamicScaffold, $uses, $helpers
$this->out("Controller Name:\n\t$controllerName");
if (strtolower($useDynamicScaffold) == 'y') {
- $this->out("\t\tvar \$scaffold;");
- $actions = 'scaffold';
+ $this->out("var \$scaffold;");
}
$properties = array(
@@ -428,52 +428,17 @@ function bakeActions($controllerName, $admin = null, $wannaUseSession = true) {
* @access private
*/
function bake($controllerName, $actions = '', $helpers = null, $components = null, $uses = null) {
- $out = "<?php\n";
- $out .= "class $controllerName" . "Controller extends {$this->plugin}AppController {\n\n";
- $out .= "\tvar \$name = '$controllerName';\n";
+ $isScaffold = ($actions === 'scaffold') ? true : false;
- if (low($actions) == 'scaffold') {
- $out .= "\tvar \$scaffold;\n";
- } else {
- if (count($uses)) {
- $out .= "\tvar \$uses = array('" . $this->_modelName($controllerName) . "', ";
-
- foreach ($uses as $use) {
- if ($use != $uses[count($uses) - 1]) {
- $out .= "'" . $this->_modelName($use) . "', ";
- } else {
- $out .= "'" . $this->_modelName($use) . "'";
- }
- }
- $out .= ");\n";
- }
+ $this->Template->set('plugin', $this->plugin);
+ $this->Template->set(compact('controllerName', 'actions', 'helpers', 'components', 'uses', 'isScaffold'));
+ $contents = $this->Template->generate('objects', 'controller');
- $out .= "\tvar \$helpers = array('Html', 'Form'";
- if (count($helpers)) {
- foreach ($helpers as $help) {
- $out .= ", '" . Inflector::camelize($help) . "'";
- }
- }
- $out .= ");\n";
-
- if (count($components)) {
- $out .= "\tvar \$components = array(";
-
- foreach ($components as $comp) {
- if ($comp != $components[count($components) - 1]) {
- $out .= "'" . Inflector::camelize($comp) . "', ";
- } else {
- $out .= "'" . Inflector::camelize($comp) . "'";
- }
- }
- $out .= ");\n";
- }
- $out .= $actions;
- }
- $out .= "}\n";
- $out .= "?>";
$filename = $this->path . $this->_controllerPath($controllerName) . '_controller.php';
- return $this->createFile($filename, $out);
+ if ($this->createFile($filename, $contents)) {
+ return $contents;
+ }
+ return false;
}
/**
* Assembles and writes a unit test file
@@ -0,0 +1,64 @@
+<?php
+/**
+ * Controller bake template file
+ *
+ * Allows templating of Controllers generated from bake.
+ *
+ * PHP versions 4 and 5
+ *
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link http://cakephp.org
+ * @package cake
+ * @subpackage cake.
+ * @since CakePHP(tm) v 1.3
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+
+echo "<?php\n";
+?>
+class <?php echo $controllerName; ?>Controller extends <?php echo $plugin; ?>AppController {
+
+ var $name = '<?php echo $controllerName; ?>';
+<?php if ($isScaffold): ?>
+ var $scaffold;
+<?php else: ?>
+<?php
+if (count($uses)):
+ echo "\tvar \$uses = array('" . $this->_modelName($controllerName) . "'";
+ foreach ($uses as $use):
+ echo ", '" . $this->_modelName($use) . "'";
+ endforeach;
+ echo ");\n";
+endif;
+
+echo "\tvar \$helpers = array('Html', 'Form'";
+if (count($helpers)):
+ foreach ($helpers as $help):
+ echo ", '" . Inflector::camelize($help) . "'";
+ endforeach;
+endif;
+echo ");\n";
+
+if (count($components)):
+ echo "\tvar \$components = array(";
+ for ($i = 0, $len = count($components); $i < $len; $i++):
+ if ($i != $len - 1):
+ echo "'" . Inflector::camelize($components[$i]) . "', ";
+ else:
+ echo "'" . Inflector::camelize($components[$i]) . "'";
+ endif;
+ endfor;
+ echo ");\n";
+endif;
+
+echo $actions;
+
+endif; ?>
+}
+<?php echo "?>"; ?>

0 comments on commit d31c43d

Please sign in to comment.