Refactoring bake action generation into a template

file.
Adding template file for controller actions.
This commit is contained in:
mark_story 2009-05-19 23:53:41 -04:00
parent 2cae04308c
commit a4f6fdc3b9
2 changed files with 142 additions and 130 deletions

View file

@ -277,7 +277,7 @@ class ControllerTask extends Shell {
$this->err(__('You must have a model for this class to build basic methods. Please try again.', true)); $this->err(__('You must have a model for this class to build basic methods. Please try again.', true));
$this->_stop(); $this->_stop();
} }
$actions = null;
$modelObj =& ClassRegistry::init($currentModelName); $modelObj =& ClassRegistry::init($currentModelName);
$controllerPath = $this->_controllerPath($controllerName); $controllerPath = $this->_controllerPath($controllerName);
$pluralName = $this->_pluralName($currentModelName); $pluralName = $this->_pluralName($currentModelName);
@ -285,136 +285,10 @@ class ControllerTask extends Shell {
$singularHumanName = Inflector::humanize($currentModelName); $singularHumanName = Inflector::humanize($currentModelName);
$pluralHumanName = Inflector::humanize($controllerName); $pluralHumanName = Inflector::humanize($controllerName);
$actions .= "\n"; $this->Template->set(compact('admin', 'controllerPath', 'pluralName', 'singularName', 'singularHumanName',
$actions .= "\tfunction {$admin}index() {\n"; 'pluralHumanName', 'modelObj', 'wannaUseSession', 'currentModelName'));
$actions .= "\t\t\$this->{$currentModelName}->recursive = 0;\n"; $actions = $this->Template->generate('objects', 'controller_actions');
$actions .= "\t\t\$this->set('{$pluralName}', \$this->paginate());\n";
$actions .= "\t}\n";
$actions .= "\n";
$actions .= "\tfunction {$admin}view(\$id = null) {\n";
$actions .= "\t\tif (!\$id) {\n";
if ($wannaUseSession) {
$actions .= "\t\t\t\$this->Session->setFlash(__('Invalid {$singularHumanName}.', true));\n";
$actions .= "\t\t\t\$this->redirect(array('action'=>'index'));\n";
} else {
$actions .= "\t\t\t\$this->flash(__('Invalid {$singularHumanName}', true), array('action'=>'index'));\n";
}
$actions .= "\t\t}\n";
$actions .= "\t\t\$this->set('".$singularName."', \$this->{$currentModelName}->read(null, \$id));\n";
$actions .= "\t}\n";
$actions .= "\n";
/* ADD ACTION */
$compact = array();
$actions .= "\tfunction {$admin}add() {\n";
$actions .= "\t\tif (!empty(\$this->data)) {\n";
$actions .= "\t\t\t\$this->{$currentModelName}->create();\n";
$actions .= "\t\t\tif (\$this->{$currentModelName}->save(\$this->data)) {\n";
if ($wannaUseSession) {
$actions .= "\t\t\t\t\$this->Session->setFlash(__('The ".$singularHumanName." has been saved', true));\n";
$actions .= "\t\t\t\t\$this->redirect(array('action'=>'index'));\n";
} else {
$actions .= "\t\t\t\t\$this->flash(__('{$currentModelName} saved.', true), array('action'=>'index'));\n";
}
$actions .= "\t\t\t} else {\n";
if ($wannaUseSession) {
$actions .= "\t\t\t\t\$this->Session->setFlash(__('The {$singularHumanName} could not be saved. Please, try again.', true));\n";
}
$actions .= "\t\t\t}\n";
$actions .= "\t\t}\n";
foreach ($modelObj->hasAndBelongsToMany as $associationName => $relation) {
if (!empty($associationName)) {
$habtmModelName = $this->_modelName($associationName);
$habtmSingularName = $this->_singularName($associationName);
$habtmPluralName = $this->_pluralName($associationName);
$actions .= "\t\t\${$habtmPluralName} = \$this->{$currentModelName}->{$habtmModelName}->find('list');\n";
$compact[] = "'{$habtmPluralName}'";
}
}
foreach ($modelObj->belongsTo as $associationName => $relation) {
if (!empty($associationName)) {
$belongsToModelName = $this->_modelName($associationName);
$belongsToPluralName = $this->_pluralName($associationName);
$actions .= "\t\t\${$belongsToPluralName} = \$this->{$currentModelName}->{$belongsToModelName}->find('list');\n";
$compact[] = "'{$belongsToPluralName}'";
}
}
if (!empty($compact)) {
$actions .= "\t\t\$this->set(compact(".join(', ', $compact)."));\n";
}
$actions .= "\t}\n";
$actions .= "\n";
/* EDIT ACTION */
$compact = array();
$actions .= "\tfunction {$admin}edit(\$id = null) {\n";
$actions .= "\t\tif (!\$id && empty(\$this->data)) {\n";
if ($wannaUseSession) {
$actions .= "\t\t\t\$this->Session->setFlash(__('Invalid {$singularHumanName}', true));\n";
$actions .= "\t\t\t\$this->redirect(array('action'=>'index'));\n";
} else {
$actions .= "\t\t\t\$this->flash(__('Invalid {$singularHumanName}', true), array('action'=>'index'));\n";
}
$actions .= "\t\t}\n";
$actions .= "\t\tif (!empty(\$this->data)) {\n";
$actions .= "\t\t\tif (\$this->{$currentModelName}->save(\$this->data)) {\n";
if ($wannaUseSession) {
$actions .= "\t\t\t\t\$this->Session->setFlash(__('The ".$singularHumanName." has been saved', true));\n";
$actions .= "\t\t\t\t\$this->redirect(array('action'=>'index'));\n";
} else {
$actions .= "\t\t\t\t\$this->flash(__('The ".$singularHumanName." has been saved.', true), array('action'=>'index'));\n";
}
$actions .= "\t\t\t} else {\n";
if ($wannaUseSession) {
$actions .= "\t\t\t\t\$this->Session->setFlash(__('The {$singularHumanName} could not be saved. Please, try again.', true));\n";
}
$actions .= "\t\t\t}\n";
$actions .= "\t\t}\n";
$actions .= "\t\tif (empty(\$this->data)) {\n";
$actions .= "\t\t\t\$this->data = \$this->{$currentModelName}->read(null, \$id);\n";
$actions .= "\t\t}\n";
foreach ($modelObj->hasAndBelongsToMany as $associationName => $relation) {
if (!empty($associationName)) {
$habtmModelName = $this->_modelName($associationName);
$habtmSingularName = $this->_singularName($associationName);
$habtmPluralName = $this->_pluralName($associationName);
$actions .= "\t\t\${$habtmPluralName} = \$this->{$currentModelName}->{$habtmModelName}->find('list');\n";
$compact[] = "'{$habtmPluralName}'";
}
}
foreach ($modelObj->belongsTo as $associationName => $relation) {
if (!empty($associationName)) {
$belongsToModelName = $this->_modelName($associationName);
$belongsToPluralName = $this->_pluralName($associationName);
$actions .= "\t\t\${$belongsToPluralName} = \$this->{$currentModelName}->{$belongsToModelName}->find('list');\n";
$compact[] = "'{$belongsToPluralName}'";
}
}
if (!empty($compact)) {
$actions .= "\t\t\$this->set(compact(".join(',', $compact)."));\n";
}
$actions .= "\t}\n";
$actions .= "\n";
$actions .= "\tfunction {$admin}delete(\$id = null) {\n";
$actions .= "\t\tif (!\$id) {\n";
if ($wannaUseSession) {
$actions .= "\t\t\t\$this->Session->setFlash(__('Invalid id for {$singularHumanName}', true));\n";
$actions .= "\t\t\t\$this->redirect(array('action'=>'index'));\n";
} else {
$actions .= "\t\t\t\$this->flash(__('Invalid {$singularHumanName}', true), array('action'=>'index'));\n";
}
$actions .= "\t\t}\n";
$actions .= "\t\tif (\$this->{$currentModelName}->del(\$id)) {\n";
if ($wannaUseSession) {
$actions .= "\t\t\t\$this->Session->setFlash(__('{$singularHumanName} deleted', true));\n";
$actions .= "\t\t\t\$this->redirect(array('action'=>'index'));\n";
} else {
$actions .= "\t\t\t\$this->flash(__('{$singularHumanName} deleted', true), array('action'=>'index'));\n";
}
$actions .= "\t\t}\n";
$actions .= "\t}\n";
$actions .= "\n";
return $actions; return $actions;
} }

View file

@ -0,0 +1,138 @@
<?php
/**
* Bake Template for Controller action generation.
*
*
*
* 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.console.libs.template.objects
* @since CakePHP(tm) v 1.3
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
?>
function <?php echo $admin ?>index() {
$this-><?php echo $currentModelName ?>->recursive = 0;
$this->set('<?php echo $pluralName ?>', $this->paginate());
}
function <?php echo $admin ?>view($id = null) {
if (!$id) {
<?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('Invalid <?php echo $singularHumanName ?>', true));
$this->redirect(array('action'=>'index'));
<?php else: ?>
$this->flash(__('Invalid <?php echo $singularHumanName; ?>', true), array('action'=>'index'));
<?php endif; ?>
}
$this->set('<?php echo $singularName; ?>', $this-><?php echo $currentModelName; ?>->read(null, $id));
}
<?php $compact = array(); ?>
function <?php echo $admin ?>add() {
if (!empty($this->data)) {
$this-><?php echo $currentModelName; ?>->create();
if ($this-><?php echo $currentModelName; ?>->save($this->data)) {
<?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('The <?php echo $singularHumanName; ?> has been saved', true));
$this->redirect(array('action'=>'index'));
<?php else: ?>
$this->flash(__('<?php echo $currentModelName; ?> saved.', true), array('action'=>'index'));
<?php endif; ?>
} else {
<?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('The <?php echo $singularHumanName; ?> could not be saved. Please, try again.', true));
<?php endif; ?>
}
}
<?php
foreach (array('belongsTo', 'hasAndBelongsToMany') as $assoc):
foreach ($modelObj->{$assoc} as $associationName => $relation):
if (!empty($associationName)):
$otherModelName = $this->_modelName($associationName);
$otherPluralName = $this->_pluralName($associationName);
echo "\t\t\${$otherPluralName} = \$this->{$currentModelName}->{$otherModelName}->find('list');\n";
$compact[] = "'{$otherPluralName}'";
endif;
endforeach;
endforeach;
if (!empty($compact)):
echo "\t\t\$this->set(compact(".join(', ', $compact)."));\n";
endif;
?>
}
<?php $compact = array(); ?>
function <?php echo $admin; ?>edit($id = null) {
if (!$id && empty($this->data)) {
<?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('Invalid <?php echo $singularHumanName; ?>', true));
$this->redirect(array('action'=>'index'));
<?php else: ?>
$this->flash(__('Invalid <?php echo $singularHumanName; ?>', true), array('action'=>'index'));
<?php endif; ?>
}
if (!empty($this->data)) {
if ($this-><?php echo $currentModelName; ?>->save($this->data)) {
<?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('The <?php echo $singularHumanName; ?> has been saved', true));
$this->redirect(array('action'=>'index'));
<?php else: ?>
$this->flash(__('The <?php echo $singularHumanName; ?> has been saved.', true), array('action'=>'index'));
<?php endif; ?>
} else {
<?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('The <?php echo $singularHumanName; ?> could not be saved. Please, try again.', true));
<?php endif; ?>
}
}
if (empty($this->data)) {
$this->data = $this-><?php echo $currentModelName; ?>->read(null, $id);
}
<?php
foreach (array('belongsTo', 'hasAndBelongsToMany') as $assoc):
foreach ($modelObj->{$assoc} as $associationName => $relation):
if (!empty($associationName)):
$otherModelName = $this->_modelName($associationName);
$otherPluralName = $this->_pluralName($associationName);
echo "\t\t\${$otherPluralName} = \$this->{$currentModelName}->{$otherModelName}->find('list');\n";
$compact[] = "'{$otherPluralName}'";
endif;
endforeach;
endforeach;
if (!empty($compact)):
echo "\t\t\$this->set(compact(".join(', ', $compact)."));\n";
endif;
?>
}
function <?php echo $admin; ?>delete($id = null) {
if (!$id) {
<?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('Invalid id for <?php echo $singularHumanName; ?>', true));
$this->redirect(array('action'=>'index'));
<?php else: ?>
$this->flash(__('Invalid <?php echo $singularHumanName; ?>', true), array('action'=>'index'));
<?php endif; ?>
}
if ($this-><?php echo $currentModelName; ?>->del($id)) {
<?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('<?php echo $singularHumanName; ?> deleted', true));
$this->redirect(array('action'=>'index'));
<?php else: ?>
$this->flash(__('<?php echo $singularHumanName; ?> deleted', true), array('action'=>'index'));
<?php endif; ?>
}
}