Refactoring code generation into controller.ctp

This commit is contained in:
mark_story 2009-05-17 00:05:07 -04:00
parent c0f06be65e
commit d31c43d409
2 changed files with 77 additions and 48 deletions

View file

@ -160,7 +160,10 @@ class ControllerTask extends Shell {
__("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 @@ class ControllerTask extends Shell {
$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 @@ class ControllerTask extends Shell {
$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 @@ class ControllerTask extends Shell {
* @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) . "', ";
$this->Template->set('plugin', $this->plugin);
$this->Template->set(compact('controllerName', 'actions', 'helpers', 'components', 'uses', 'isScaffold'));
$contents = $this->Template->generate('objects', 'controller');
foreach ($uses as $use) {
if ($use != $uses[count($uses) - 1]) {
$out .= "'" . $this->_modelName($use) . "', ";
} else {
$out .= "'" . $this->_modelName($use) . "'";
}
}
$out .= ");\n";
}
$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

View 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 "?>"; ?>