Navigation Menu

Skip to content

Commit

Permalink
Moving bake templates into templates/default.
Browse files Browse the repository at this point in the history
Adding 'console template themes' to TemplateTask.
Adding test cases for console themes.
Moving files around.
  • Loading branch information
markstory committed Jul 1, 2009
1 parent 9627af2 commit ad98139
Show file tree
Hide file tree
Showing 13 changed files with 963 additions and 24 deletions.
94 changes: 78 additions & 16 deletions cake/console/libs/tasks/template.php
Expand Up @@ -2,7 +2,6 @@
/**
* Template Task can generate templated output Used in other Tasks
*
*
*
* PHP versions 4 and 5
*
Expand All @@ -26,39 +25,48 @@ class TemplateTask extends Shell {
* @var array
**/
var $templateVars = array();

/**
* Paths to look for templates on.
* Contains a list of $theme => $path
*
* @var array
**/
var $templatePaths = array();

/**
* Initialize callback
* Initialize callback. Setup paths for the template task.
*
* @access public
* @return void
**/
function initialize() {
$this->templatePaths = $this->Dispatch->shellPaths;
$this->templatePaths = $this->_findThemes();
}

/**
* Find a template
* Find the paths to all the installed shell themes in the app.
*
* @param string $directory Subdirectory to look for ie. 'views', 'objects'
* @param string $filename lower_case_underscored filename you want.
* @access public
* @return string filename or false if scan failed.
* Bake themes are directories not named `skel` inside a `vendors/shells/templates` path.
*
* @return array Array of bake themes that are installed.
**/
function _findTemplate($directory, $filename) {
foreach ($this->templatePaths as $path) {
$templatePath = $path . 'templates' . DS . $directory . DS . $filename . '.ctp';
if (file_exists($templatePath) && is_file($templatePath)) {
return $templatePath;
function _findThemes() {
$paths = $this->Dispatch->shellPaths;
$themes = array();
foreach ($paths as $path) {
$Folder =& new Folder($path . 'templates', false);
$contents = $Folder->read();
$subDirs = $contents[0];
foreach ($subDirs as $dir) {
if (empty($dir) || $dir == 'skel') {
continue;
}
$templateDir = $path . 'templates' . DS . $dir . DS;
$themes[$dir] = $templateDir;
}
}
return false;
return $themes;
}

/**
Expand Down Expand Up @@ -106,7 +114,8 @@ function generate($directory, $filename, $vars = null) {
if (empty($this->templatePaths)) {
$this->initialize();
}
$templateFile = $this->_findTemplate($directory, $filename);
$themePath = $this->getThemePath();
$templateFile = $this->_findTemplate($themePath, $directory, $filename);
if ($templateFile) {
extract($this->templateVars);
ob_start();
Expand All @@ -117,4 +126,57 @@ function generate($directory, $filename, $vars = null) {
}
return '';
}

/**
* Find the theme name for the current operation.
* If there is only one theme in $templatePaths it will be used.
* If there is a -theme param in the cli args, it will be used.
* If there is more than one installed theme user interaction will happen
*
* @return string returns the path to the selected theme.
**/
function getThemePath() {
if (count($this->templatePaths) == 1) {
$paths = array_values($this->templatePaths);
return $paths[0];
}
if (!empty($this->params['theme']) && isset($this->templatePaths[$this->params['theme']])) {
return $this->templatePaths[$this->params['theme']];
}
$i = 1;
$indexedPaths = array();
foreach ($this->templatePaths as $key => $path) {
$this->out($i . '. ' . $key);
$indexedPaths[$i] = $path;
$i++;
}
$index = $this->in(__('Which bake theme would you like to use?', true), range(1, $i), 1);
return $indexedPaths[$index];
}

/**
* Find a template inside a directory inside a path.
* Will scan all other theme dirs if the template is not found in the first directory.
*
* @param string $path The initial path to look for the file on. If it is not found fallbacks will be used.
* @param string $directory Subdirectory to look for ie. 'views', 'objects'
* @param string $filename lower_case_underscored filename you want.
* @access public
* @return string filename will exit program if template is not found.
**/
function _findTemplate($path, $directory, $filename) {
$themeFile = $path . $directory . DS . $filename . '.ctp';
if (file_exists($themeFile)) {
return $themeFile;
}
foreach ($this->templatePaths as $path) {
$templatePath = $path . $directory . DS . $filename . '.ctp';
if (file_exists($templatePath)) {
return $templatePath;
}
}
$this->err(sprintf(__('Could not find template for %s, exiting.'), $filename));
$this->_stop();
}

}
142 changes: 142 additions & 0 deletions cake/console/libs/templates/default/actions/controller_actions.ctp
@@ -0,0 +1,142 @@
<?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; ?>
}
<?php if ($wannaUseSession): ?>
$this->Session->setFlash(__('<?php echo $singularHumanName; ?> was not deleted', true));
<?php else: ?>
$this->flash(__('<?php echo $singularHumanName; ?> was not deleted', true), array('action' => 'index'));
<?php endif; ?>
$this->redirect(array('action' => 'index'));
}
57 changes: 57 additions & 0 deletions cake/console/libs/templates/default/classes/controller.ctp
@@ -0,0 +1,57 @@
<?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

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 "?>"; ?>
42 changes: 42 additions & 0 deletions cake/console/libs/templates/default/classes/fixture.ctp
@@ -0,0 +1,42 @@
<?php
/**
* Fixture Template file
*
* Fixture Template used when baking fixtures with 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)
*/
?>
<?php echo '<?php' . "\n"; ?>
/* <?php echo $model; ?> Fixture generated on: <?php echo date('Y-m-d H:m:s') . " : ". time(); ?> */
class <?php echo $model; ?>Fixture extends CakeTestFixture {
var $name = '<?php echo $model; ?>';
<?php if ($table): ?>
var $table = '<?php echo $table; ?>';
<?php endif; ?>
<?php if ($import): ?>
var $import = <?php echo $import; ?>;
<?php endif;?>

<?php if ($schema): ?>
var $fields = <?php echo $schema; ?>;
<?php endif;?>

<?php if ($records): ?>
var $records = <?php echo $records; ?>;
<?php endif;?>
}
<?php echo '?>'; ?>

0 comments on commit ad98139

Please sign in to comment.