93b20bd1eb10f63f64369d1ff49fa26319123b71

Author: alkemann

Date: 2009-01-23 23:52:24 +0100

added custom bake templates

diff --git a/vendors/shells/bake.php b/vendors/shells/bake.php new file mode 100644 index 0000000..ec7ca6b --- /dev/null +++ b/vendors/shells/bake.php @@ -0,0 +1,270 @@ +<?php +/* SVN FILE: $Id: bake.php 7125 2008-06-05 15:06:49Z gwoo $ */ +/** + * Command-line code generation utility to automate programmer chores. + * + * Bake is CakePHP's code generation script, which can help you kickstart + * application development by writing fully functional skeleton controllers, + * models, and views. Going further, Bake can also write Unit Tests for you. + * + * PHP versions 4 and 5 + * + * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> + * Copyright 2005-2008, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The MIT License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. + * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project + * @package cake + * @subpackage cake.cake.console.libs + * @since CakePHP(tm) v 1.2.0.5012 + * @version $Revision: 7125 $ + * @modifiedby $LastChangedBy: gwoo $ + * @lastmodified $Date: 2008-06-05 08:06:49 -0700 (Thu, 05 Jun 2008) $ + * @license http://www.opensource.org/licenses/mit-license.php The MIT License + */ +/** + * Bake is a command-line code generation utility for automating programmer chores. + * + * @package cake + * @subpackage cake.cake.console.libs + */ +class BakeShell extends Shell { +/** + * Contains tasks to load and instantiate + * + * @var array + * @access public + */ + var $tasks = array('Project', 'DbConfig', 'Model', 'Controller', 'View', 'Plugin', 'Test'); +/** + * Override loadTasks() to handle paths + * + * @access public + */ + function loadTasks() { + parent::loadTasks(); + $task = Inflector::classify($this->command); + if (isset($this->{$task}) && !in_array($task, array('Project', 'DbConfig'))) { + $path = Inflector::underscore(Inflector::pluralize($this->command)); + $this->{$task}->path = $this->params['working'] . DS . $path . DS; + if (!is_dir($this->{$task}->path)) { + $this->err(sprintf(__("%s directory could not be found.\nBe sure you have created %s", true), $task, $this->{$task}->path)); + $this->_stop(); + } + } + } + +/** + * Generates and writes 'Security.salt' + * + * @param string $path Project path + * @return boolean Success + * @access public + */ + function securitySalt($path) { + $File =& new File($path . DS .'config' . DS . 'core.php'); + $contents = $File->read(); + if (preg_match('/([\\t\\x20]*Configure::write\\(\\\'Security.salt\\\',[\\t\\x20\'A-z0-9]*\\);)/', $contents, $match)) { + if (!class_exists('Security')) { + uses('Security'); + } + $string = Security::generateAuthKey(); + $result = str_replace($match[0], "\t" . 'Configure::write(\'Security.salt\', \''.$string.'\');', $contents); + if ($File->write($result)) { + return true; + } else { + return false; + } + } else { + return false; + } + } +/** + * Override main() to handle action + * + * @access public + */ + function main() { + if (!is_dir($this->DbConfig->path)) { + if ($this->Project->execute()) { + $this->DbConfig->path = $this->params['working'] . DS . 'config' . DS; + } + } + if (!config('database')) { + $this->out(__("Making salt.", true)); + if (!$this->securitySalt($this->params['working'])) { + $this->out(__("Salt generation FAILED")); + $this->_stop(); + } + $this->out(__("Your database configuration was not found. Take a moment to create one.", true)); + $this->args = null; + return $this->DbConfig->execute(); + } + $this->out('Interactive Bake Shell'); + $this->hr(); + $this->out('[D]atabase Configuration'); + $this->out('[M]odel'); + $this->out('[V]iew'); + $this->out('[C]ontroller'); + $this->out('[P]roject'); + $this->out('[Q]uit'); + + $classToBake = strtoupper($this->in(__('What would you like to Bake?', true), array('D', 'M', 'V', 'C', 'P', 'Q'))); + switch($classToBake) { + case 'D': + $this->DbConfig->execute(); + break; + case 'M': + $this->Model->execute(); + break; + case 'V': + $this->View->execute(); + break; + case 'C': + $this->Controller->execute(); + break; + case 'P': + $this->Project->execute(); + break; + case 'Q': + exit(0); + break; + default: + $this->out(__('You have made an invalid selection. Please choose a type of class to Bake by entering D, M, V, or C.', true)); + } + $this->hr(); + $this->main(); + } + + + function scaffolds() { + $ds = 'default'; + $this->hr(); + $this->out('Bake Scaffolds'); + $this->hr(); + $db =& ConnectionManager::getDataSource($ds); + $usePrefix = empty($db->config['prefix']) ? '' : $db->config['prefix']; + if ($usePrefix) { + $tables = array(); + foreach ($db->listSources() as $table) { + if (!strncmp($table, $usePrefix, strlen($usePrefix))) { + $tables[] = substr($table, strlen($usePrefix)); + } + } + } else { + $tables = $db->listSources(); + } + if (empty($tables)) { + $this->err(__('Your database does not have any tables.', true)); + $this->_stop(); + } + foreach ($tables as $table) { + $this->Controller->bake(Inflector::camelize($table),'scaffold'); + } + } + +/** + * Quickly bake the MVC + * + * @access public + */ + function all() { + $ds = 'default'; + $this->hr(); + $this->out('Bake All'); + $this->hr(); + + if (isset($this->params['connection'])) { + $ds = $this->params['connection']; + } + + if (empty($this->args)) { + $name = $this->Model->getName($ds); + } + + if (!empty($this->args[0])) { + $name = $this->args[0]; + $this->Model->listAll($ds, false); + } + + $modelExists = false; + $model = $this->_modelName($name); + if (App::import('Model', $model)) { + $object = new $model(); + $modelExists = true; + } else { + App::import('Model'); + $object = new Model(array('name' => $name, 'ds' => $ds)); + } + + $modelBaked = $this->Model->bake($object, false, array()); + + if ($modelBaked && $modelExists === false) { + $this->out(sprintf(__('%s Model was baked.', true), $model)); + if ($this->_checkUnitTest()) { + $this->Model->bakeTest($model); + } + $modelExists = true; + } + + if ($modelExists === true) { + $controller = $this->_controllerName($name); + if ($this->Controller->bake($controller, $this->Controller->bakeActions($controller))) { + $this->out(sprintf(__('%s Controller was baked.', true), $name)); + if ($this->_checkUnitTest()) { + $this->Controller->bakeTest($controller); + } + } + if (App::import('Controller', $controller)) { + $this->View->args = array($controller); + $this->View->execute(); + } + $this->out(__('Bake All complete')); + array_shift($this->args); + } else { + $this->err(__('Bake All could not continue without a valid model', true)); + } + + if (empty($this->args)) { + $this->all(); + } + $this->_stop(); + } + +/** + * Displays help contents + * + * @access public + */ + function help() { + $this->out('CakePHP Bake:'); + $this->hr(); + $this->out('The Bake script generates controllers, views and models for your application.'); + $this->out('If run with no command line arguments, Bake guides the user through the class'); + $this->out('creation process. You can customize the generation process by telling Bake'); + $this->out('where different parts of your application are using command line arguments.'); + $this->hr(); + $this->out("Usage: cake bake <command> <arg1> <arg2>..."); + $this->hr(); + $this->out('Params:'); + $this->out("\t-app <path> Absolute/Relative path to your app folder.\n"); + $this->out('Commands:'); + $this->out("\n\tbake help\n\t\tshows this help message."); + $this->out("\n\tbake all <name>\n\t\tbakes complete MVC. optional <name> of a Model"); + $this->out("\n\tbake project <path>\n\t\tbakes a new app folder in the path supplied\n\t\tor in current directory if no path is specified"); + $this->out("\n\tbake plugin <name>\n\t\tbakes a new plugin folder in the path supplied\n\t\tor in current directory if no path is specified."); + $this->out("\n\tbake db_config\n\t\tbakes a database.php file in config directory."); + $this->out("\n\tbake model\n\t\tbakes a model. run 'bake model help' for more info"); + $this->out("\n\tbake view\n\t\tbakes views. run 'bake view help' for more info"); + $this->out("\n\tbake controller\n\t\tbakes a controller. run 'bake controller help' for more info"); + $this->out(""); + + } +} +?> \ No newline at end of file diff --git a/vendors/shells/templates/form.ctp b/vendors/shells/templates/form.ctp new file mode 100644 index 0000000..b650724 --- /dev/null +++ b/vendors/shells/templates/form.ctp @@ -0,0 +1,43 @@ +<div class="<?php echo $pluralVar;?> form"> +<?php echo "<?php\n";?> + <?php echo "echo \$form->create('{$modelClass}');\n";?> + <?php echo "echo \$form->inputs(array('fieldset' => false,\n"; //'legend' => '$action $singularHumanName',\n"; + foreach ($fields as $field) { + if ($action == 'add' && $field == $primaryKey) { + continue; + } elseif (!in_array($field, array('created', 'modified', 'updated'))) { + if (isset($keyFields[$field])) { + echo "\t\t\t'$field' => array('empty' => true),\n"; + } else { + echo "\t\t\t'$field',\n"; + } + } + } + if(!empty($associations['hasAndBelongsToMany'])) { + foreach ($associations['hasAndBelongsToMany'] as $assocName => $assocData) { + echo "\t\t\t'{$assocName}',\n"; + } + } + echo "\t));\n";?> + <?php echo "echo \$form->end('Submit');?>\n"; ?> +</div> +<?php + echo "<?php\n"; + if ($action != 'add'): + echo "\t\$menu->add('context',array(__('Delete', true), array('action'=>'delete', \$form->value('{$modelClass}.{$primaryKey}')), null, sprintf(__('Are you sure you want to delete # %s?', true), \$form->value('{$modelClass}.{$primaryKey}'))));\n"; + endif; + echo "\t\$menu->add('context',array(__('List {$pluralHumanName}', true), array('action'=>'index')));\n"; + + $done = array(); + foreach ($associations as $type => $data) { + foreach($data as $alias => $details) { + if ($details['controller'] != $this->name && !in_array($details['controller'], $done)) { + echo "\t\$menu->add('context',array(__('List ".Inflector::humanize($details['controller'])."', true), array('controller'=> '{$details['controller']}', 'action'=>'index')));\n"; + // echo "\t\$menu->add('context',array(__('New ".Inflector::humanize(Inflector::underscore($alias))."', true), array('controller'=> '{$details['controller']}', 'action'=>'add')));\n"; + $done[] = $details['controller']; + } + } + } + + echo "?>\n"; +?> diff --git a/vendors/shells/templates/index.ctp b/vendors/shells/templates/index.ctp new file mode 100644 index 0000000..5ab4ae0 --- /dev/null +++ b/vendors/shells/templates/index.ctp @@ -0,0 +1,104 @@ +<?php +/* SVN FILE: $Id: index.ctp 6311 2008-01-02 06:33:52Z phpnut $ */ +/** + * + * PHP versions 4 and 5 + * + * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> + * Copyright 2005-2008, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The MIT License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. + * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project + * @package cake + * @subpackage cake.cake.console.libs.templates.views + * @since CakePHP(tm) v 1.2.0.5234 + * @version $Revision: 6311 $ + * @modifiedby $LastChangedBy: phpnut $ + * @lastmodified $Date: 2008-01-01 22:33:52 -0800 (Tue, 01 Jan 2008) $ + * @license http://www.opensource.org/licenses/mit-license.php The MIT License + */ +?> +<div class="<?php echo $pluralVar;?> index"> +<h2><?php echo "<?php __('{$pluralHumanName}');?>";?></h2> +<p> +<?php echo "<?php +echo \$paginator->counter(array( +'format' => __('Page %page% of %pages%, showing %current% records out of %count% total, starting on record %start%, ending on %end%', true) +)); +?>";?> +</p> +<table cellpadding="0" cellspacing="0"> +<tr> +<?php foreach ($fields as $field):?> + <th><?php echo "<?php echo \$paginator->sort('{$field}');?>";?></th> +<?php endforeach;?> + <th class="actions"><?php echo "<?php __('Actions');?>";?></th> +</tr> +<?php +echo "<?php +\$i = 1; +foreach (\${$pluralVar} as \${$singularVar}): + \$class = null; + if (\$i++ % 2 == 0) { + \$class = ' class=\"altrow\"'; + } +?>\n"; + echo "\t<tr<?php echo \$class;?>>\n"; + foreach ($fields as $field) { + $isKey = false; + if(!empty($associations['belongsTo'])) { + foreach ($associations['belongsTo'] as $alias => $details) { + if($field === $details['foreignKey']) { + $isKey = true; + echo "\t\t<td>\n\t\t\t<?php echo \$html->link(\${$singularVar}['{$alias}']['{$details['displayField']}'], array('controller'=> '{$details['controller']}', 'action'=>'view', \${$singularVar}['{$alias}']['{$details['primaryKey']}'])); ?>\n\t\t</td>\n"; + break; + } + } + } + if ($field == $displayField) { + echo "\t\t<td>\n\t\t\t<?php echo \$html->link(\${$singularVar}['{$modelClass}']['{$field}'], array('action'=>'view', \${$singularVar}['{$modelClass}']['{$primaryKey}'])); ?>\n\t\t</td>\n"; + $isKey = true; + } + if($isKey !== true) { + echo "\t\t<td>\n\t\t\t<?php echo \${$singularVar}['{$modelClass}']['{$field}']; ?>\n\t\t</td>\n"; + } + } + + echo "\t\t<td class=\"actions\">\n"; + echo "\t\t\t<?php echo \$html->link(__('Edit', true), array('action'=>'edit', \${$singularVar}['{$modelClass}']['{$primaryKey}'])); ?>\n"; + echo "\t\t\t<?php echo \$html->link(__('Delete', true), array('action'=>'delete', \${$singularVar}['{$modelClass}']['{$primaryKey}']), null, sprintf(__('Are you sure you want to delete # %s?', true), \${$singularVar}['{$modelClass}']['{$primaryKey}'])); ?>\n"; + echo "\t\t</td>\n"; + echo "\t</tr>\n"; + +echo "<?php endforeach; ?>\n"; +?> +</table> +</div> +<div class="paging"> +<?php echo "\t<?php echo \$paginator->prev('<< '.__('previous', true), array(), null, array('class'=>'disabled'));?>\n";?> + | <?php echo "\t<?php echo \$paginator->numbers();?>\n"?> +<?php echo "\t<?php echo \$paginator->next(__('next', true).' >>', array(), null, array('class'=>'disabled'));?>\n";?> +</div> +<?php + echo "<?php\n"; + echo "\t\$menu->add('context',array(__('New {$singularHumanName}', true), array('action'=>'add')));\n"; + + $done = array(); + foreach ($associations as $type => $data) { + foreach($data as $alias => $details) { + if ($details['controller'] != $this->name && !in_array($details['controller'], $done)) { + echo "\t\$menu->add('context',array(__('List ".Inflector::humanize($details['controller'])."', true), array('controller'=> '{$details['controller']}', 'action'=>'index')));\n"; + // echo "\t\$menu->add('context',array(__('New ".Inflector::humanize(Inflector::underscore($alias))."', true), array('controller'=> '{$details['controller']}', 'action'=>'add')));\n"; + $done[] = $details['controller']; + } + } + } + echo "?>\n"; +?> + diff --git a/vendors/shells/templates/view.ctp b/vendors/shells/templates/view.ctp new file mode 100644 index 0000000..7db6f32 --- /dev/null +++ b/vendors/shells/templates/view.ctp @@ -0,0 +1,148 @@ +<?php +/* SVN FILE: $Id: view.ctp 6311 2008-01-02 06:33:52Z phpnut $ */ +/** + * + * PHP versions 4 and 5 + * + * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/> + * Copyright 2005-2008, Cake Software Foundation, Inc. + * 1785 E. Sahara Avenue, Suite 490-204 + * Las Vegas, Nevada 89104 + * + * Licensed under The MIT License + * Redistributions of files must retain the above copyright notice. + * + * @filesource + * @copyright Copyright 2005-2008, Cake Software Foundation, Inc. + * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project + * @package cake + * @subpackage cake.cake.console.libs.templates.views + * @since CakePHP(tm) v 1.2.0.5234 + * @version $Revision: 6311 $ + * @modifiedby $LastChangedBy: phpnut $ + * @lastmodified $Date: 2008-01-01 22:33:52 -0800 (Tue, 01 Jan 2008) $ + * @license http://www.opensource.org/licenses/mit-license.php The MIT License + */ +?> +<div class="<?php echo $pluralVar;?> view"> +<h2><?php echo "<?php __('{$singularHumanName}');?>";?></h2> + <dl> +<?php +foreach ($fields as $field) { + $isKey = false; + if(!empty($associations['belongsTo'])) { + foreach ($associations['belongsTo'] as $alias => $details) { + if($field === $details['foreignKey']) { + $isKey = true; + echo "\t\t<dt><?php __('".Inflector::humanize(Inflector::underscore($alias))."'); ?></dt>\n"; + echo "\t\t<dd>\n\t\t\t<?php echo \$html->link(\${$singularVar}['{$alias}']['{$details['displayField']}'], array('controller'=> '{$details['controller']}', 'action'=>'view', \${$singularVar}['{$alias}']['{$details['primaryKey']}'])); ?>\n\t\t\t&nbsp;\n\t\t</dd>\n"; + break; + } + } + } + if($isKey !== true) { + echo "\t\t<dt><?php __('".Inflector::humanize($field)."'); ?></dt>\n"; + echo "\t\t<dd>\n\t\t\t<?php echo \${$singularVar}['{$modelClass}']['{$field}']; ?>\n\t\t\t&nbsp;\n\t\t</dd>\n"; + } +} +?> + </dl> +</div> +<?php + echo "<?php\n"; + echo "\t\$menu->add('context',array(__('Edit {$singularHumanName}', true), array('action'=>'edit', \${$singularVar}['{$modelClass}']['{$primaryKey}'])));\n"; + echo "\t\$menu->add('context',array(__('Delete {$singularHumanName}', true), array('action'=>'delete', \${$singularVar}['{$modelClass}']['{$primaryKey}']), null, sprintf(__('Are you sure you want to delete # %s?', true), \${$singularVar}['{$modelClass}']['{$primaryKey}'])));\n"; + echo "\t\$menu->add('context',array(__('List {$pluralHumanName}', true), array('action'=>'index')));\n"; + echo "\t\$menu->add('context',array(__('New {$singularHumanName}', true), array('action'=>'add')));\n"; + + $done = array(); + foreach ($associations as $type => $data) { + foreach($data as $alias => $details) { + if ($details['controller'] != $this->name && !in_array($details['controller'], $done)) { + echo "\t\$menu->add('context',array(__('List ".Inflector::humanize($details['controller'])."', true), array('controller'=> '{$details['controller']}', 'action'=>'index')));\n"; + echo "\t\$menu->add('context',array(__('New ".Inflector::humanize(Inflector::underscore($alias))."', true), array('controller'=> '{$details['controller']}', 'action'=>'add')));\n"; + $done[] = $details['controller']; + } + } + } + echo "?>\n"; +if(!empty($associations['hasOne'])) : + foreach ($associations['hasOne'] as $alias => $details): ?> + <div class="related"> + <h3><?php echo "<?php __('Related ".Inflector::humanize($details['controller'])."');?>";?></h3> + <?php echo "<?php if (!empty(\${$singularVar}['{$alias}'])):?>\n";?> + <dl> + <?php + foreach ($details['fields'] as $field) { + echo "\t\t<dt><?php __('".Inflector::humanize($field)."');?></dt>\n"; + echo "\t\t<dd>\n\t<?php echo \${$singularVar}['{$alias}']['{$field}'];?>\n&nbsp;</dd>\n"; + } + ?> + </dl> + <?php echo "<?php endif; ?>\n";?> + <div class="actions"> + <ul> + <li><?php echo "<?php echo \$html->link(__('Edit ".Inflector::humanize(Inflector::underscore($alias))."', true), array('controller'=> '{$details['controller']}', 'action'=>'edit', \${$singularVar}['{$alias}']['{$details['primaryKey']}'])); ?></li>\n";?> + </ul> + </div> + </div> + <?php + endforeach; +endif; +if(empty($associations['hasMany'])) { + $associations['hasMany'] = array(); +} +if(empty($associations['hasAndBelongsToMany'])) { + $associations['hasAndBelongsToMany'] = array(); +} +$relations = array_merge($associations['hasMany'], $associations['hasAndBelongsToMany']); +$i = 0; +foreach ($relations as $alias => $details): + $otherSingularVar = Inflector::variable($alias); + $otherPluralHumanName = Inflector::humanize($details['controller']); + ?> +<div class="related"> + <h3><?php echo "<?php __('Related {$otherPluralHumanName}');?>";?></h3> + <?php echo "<?php if (!empty(\${$singularVar}['{$alias}'])):?>\n";?> + <table cellpadding = "0" cellspacing = "0"> + <tr> +<?php + foreach ($details['fields'] as $field) { + echo "\t\t<th><?php __('".Inflector::humanize($field)."'); ?></th>\n"; + } +?> + <th class="actions"><?php echo "<?php __('Actions');?>";?></th> + </tr> +<?php +echo "\t<?php + \$i = 0; + foreach (\${$singularVar}['{$alias}'] as \${$otherSingularVar}): + \$class = null; + if (\$i++ % 2 == 0) { + \$class = ' class=\"altrow\"'; + } + ?>\n"; + echo "\t\t<tr<?php echo \$class;?>>\n"; + + foreach ($details['fields'] as $field) { + echo "\t\t\t<td><?php echo \${$otherSingularVar}['{$field}'];?></td>\n"; + } + + echo "\t\t\t<td class=\"actions\">\n"; + echo "\t\t\t\t<?php echo \$html->link(__('View', true), array('controller'=> '{$details['controller']}', 'action'=>'view', \${$otherSingularVar}['{$details['primaryKey']}'])); ?>\n"; + echo "\t\t\t\t<?php echo \$html->link(__('Edit', true), array('controller'=> '{$details['controller']}', 'action'=>'edit', \${$otherSingularVar}['{$details['primaryKey']}'])); ?>\n"; + echo "\t\t\t\t<?php echo \$html->link(__('Delete', true), array('controller'=> '{$details['controller']}', 'action'=>'delete', \${$otherSingularVar}['{$details['primaryKey']}']), null, sprintf(__('Are you sure you want to delete # %s?', true), \${$otherSingularVar}['{$details['primaryKey']}'])); ?>\n"; + echo "\t\t\t</td>\n"; + echo "\t\t</tr>\n"; + +echo "\t<?php endforeach; ?>\n"; +?> + </table> +<?php echo "<?php endif; ?>\n\n";?> + <div class="actions"> + <ul> + <li><?php echo "<?php echo \$html->link(__('New ".Inflector::humanize(Inflector::underscore($alias))."', true), array('controller'=> '{$details['controller']}', 'action'=>'add'));?>";?> </li> + </ul> + </div> +</div> +<?php endforeach;?> \ No newline at end of file