2009-05-13 01:19:15 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Template Task can generate templated output Used in other Tasks
|
|
|
|
*
|
2010-10-03 16:38:58 +00:00
|
|
|
* PHP 5
|
2009-05-13 01:19:15 +00:00
|
|
|
*
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2011-05-29 21:31:39 +00:00
|
|
|
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-05-13 01:19:15 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2011-05-29 21:31:39 +00:00
|
|
|
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-01-26 22:03:03 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
2010-12-24 19:26:26 +00:00
|
|
|
* @package cake.console.shells.tasks
|
2009-05-13 01:19:15 +00:00
|
|
|
* @since CakePHP(tm) v 1.3
|
|
|
|
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
|
|
|
|
*/
|
2010-12-08 00:20:56 +00:00
|
|
|
|
|
|
|
App::uses('Folder', 'Utility');
|
2010-10-24 19:27:44 +00:00
|
|
|
|
2009-05-13 01:19:15 +00:00
|
|
|
class TemplateTask extends Shell {
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-13 01:19:15 +00:00
|
|
|
/**
|
|
|
|
* variables to add to template scope
|
|
|
|
*
|
|
|
|
* @var array
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $templateVars = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-14 03:35:41 +00:00
|
|
|
/**
|
|
|
|
* Paths to look for templates on.
|
2009-07-01 04:24:20 +00:00
|
|
|
* Contains a list of $theme => $path
|
2009-05-14 03:35:41 +00:00
|
|
|
*
|
|
|
|
* @var array
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-04 07:14:00 +00:00
|
|
|
public $templatePaths = array();
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-13 01:19:15 +00:00
|
|
|
/**
|
2009-07-01 04:24:20 +00:00
|
|
|
* Initialize callback. Setup paths for the template task.
|
2009-05-13 01:19:15 +00:00
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-24 01:57:59 +00:00
|
|
|
public function initialize() {
|
2009-07-01 04:24:20 +00:00
|
|
|
$this->templatePaths = $this->_findThemes();
|
2009-05-13 01:19:15 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-13 01:19:15 +00:00
|
|
|
/**
|
2009-07-01 04:24:20 +00:00
|
|
|
* Find the paths to all the installed shell themes in the app.
|
2009-05-13 01:19:15 +00:00
|
|
|
*
|
2009-07-01 04:24:20 +00:00
|
|
|
* Bake themes are directories not named `skel` inside a `vendors/shells/templates` path.
|
|
|
|
*
|
|
|
|
* @return array Array of bake themes that are installed.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-24 01:57:59 +00:00
|
|
|
protected function _findThemes() {
|
2011-03-07 05:15:00 +00:00
|
|
|
$paths = App::path('Console');
|
|
|
|
$core = current(App::core('Console'));
|
2010-06-01 01:26:55 +00:00
|
|
|
$separator = DS === '/' ? '/' : '\\\\';
|
2010-10-17 19:43:20 +00:00
|
|
|
$core = preg_replace('#shells' . $separator . '$#', '', $core);
|
2011-02-05 17:20:09 +00:00
|
|
|
|
2010-11-13 04:05:44 +00:00
|
|
|
$Folder = new Folder($core . 'templates' . DS . 'default');
|
2011-01-28 00:30:21 +00:00
|
|
|
|
2009-07-28 07:02:29 +00:00
|
|
|
$contents = $Folder->read();
|
|
|
|
$themeFolders = $contents[0];
|
|
|
|
|
2009-07-29 02:14:33 +00:00
|
|
|
$plugins = App::objects('plugin');
|
|
|
|
foreach ($plugins as $plugin) {
|
2010-10-17 19:43:20 +00:00
|
|
|
$paths[] = $this->_pluginPath($plugin) . 'console' . DS . 'shells' . DS;
|
2009-07-29 02:14:33 +00:00
|
|
|
$paths[] = $this->_pluginPath($plugin) . 'vendors' . DS . 'shells' . DS;
|
2009-07-28 07:02:29 +00:00
|
|
|
}
|
2011-01-28 00:30:21 +00:00
|
|
|
$paths[] = $core;
|
2009-07-28 07:02:29 +00:00
|
|
|
|
|
|
|
// TEMPORARY TODO remove when all paths are DS terminated
|
2009-07-29 02:14:33 +00:00
|
|
|
foreach ($paths as $i => $path) {
|
|
|
|
$paths[$i] = rtrim($path, DS) . DS;
|
2009-07-28 07:02:29 +00:00
|
|
|
}
|
|
|
|
|
2009-07-01 04:24:20 +00:00
|
|
|
$themes = array();
|
|
|
|
foreach ($paths as $path) {
|
2010-11-13 04:05:44 +00:00
|
|
|
$Folder = new Folder($path . 'templates', false);
|
2009-07-01 04:24:20 +00:00
|
|
|
$contents = $Folder->read();
|
|
|
|
$subDirs = $contents[0];
|
|
|
|
foreach ($subDirs as $dir) {
|
2009-07-28 07:02:29 +00:00
|
|
|
if (empty($dir) || preg_match('@^skel$|_skel$@', $dir)) {
|
2009-07-01 04:24:20 +00:00
|
|
|
continue;
|
|
|
|
}
|
2010-11-13 04:05:44 +00:00
|
|
|
$Folder = new Folder($path . 'templates' . DS . $dir);
|
2009-07-28 07:02:29 +00:00
|
|
|
$contents = $Folder->read();
|
|
|
|
$subDirs = $contents[0];
|
|
|
|
if (array_intersect($contents[0], $themeFolders)) {
|
|
|
|
$templateDir = $path . 'templates' . DS . $dir . DS;
|
|
|
|
$themes[$dir] = $templateDir;
|
|
|
|
}
|
2009-05-13 01:19:15 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-01 04:24:20 +00:00
|
|
|
return $themes;
|
2009-05-13 01:19:15 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-13 01:19:15 +00:00
|
|
|
/**
|
|
|
|
* Set variable values to the template scope
|
|
|
|
*
|
|
|
|
* @param mixed $one A string or an array of data.
|
|
|
|
* @param mixed $two Value in case $one is a string (which then works as the key).
|
|
|
|
* Unused if $one is an associative array, otherwise serves as the values to $one's keys.
|
|
|
|
* @return void
|
|
|
|
*/
|
2010-04-24 01:57:59 +00:00
|
|
|
public function set($one, $two = null) {
|
2009-05-13 01:19:15 +00:00
|
|
|
if (is_array($one)) {
|
|
|
|
if (is_array($two)) {
|
|
|
|
$data = array_combine($one, $two);
|
|
|
|
} else {
|
|
|
|
$data = $one;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$data = array($one => $two);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($data == null) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-09-18 04:13:03 +00:00
|
|
|
$this->templateVars = $data + $this->templateVars;
|
2009-05-13 01:19:15 +00:00
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-05-13 01:19:15 +00:00
|
|
|
/**
|
|
|
|
* Runs the template
|
|
|
|
*
|
|
|
|
* @param string $directory directory / type of thing you want
|
|
|
|
* @param string $filename template name
|
|
|
|
* @param string $vars Additional vars to set to template scope.
|
|
|
|
* @access public
|
|
|
|
* @return contents of generated code template
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-24 01:57:59 +00:00
|
|
|
public function generate($directory, $filename, $vars = null) {
|
2009-05-13 01:19:15 +00:00
|
|
|
if ($vars !== null) {
|
|
|
|
$this->set($vars);
|
|
|
|
}
|
2009-05-14 03:35:41 +00:00
|
|
|
if (empty($this->templatePaths)) {
|
|
|
|
$this->initialize();
|
|
|
|
}
|
2009-07-01 04:24:20 +00:00
|
|
|
$themePath = $this->getThemePath();
|
|
|
|
$templateFile = $this->_findTemplate($themePath, $directory, $filename);
|
2009-05-13 01:19:15 +00:00
|
|
|
if ($templateFile) {
|
|
|
|
extract($this->templateVars);
|
|
|
|
ob_start();
|
|
|
|
ob_implicit_flush(0);
|
|
|
|
include($templateFile);
|
|
|
|
$content = ob_get_clean();
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-07-01 04:24:20 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2010-04-24 01:57:59 +00:00
|
|
|
public function getThemePath() {
|
2009-07-01 04:24:20 +00:00
|
|
|
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']];
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-07-02 03:49:50 +00:00
|
|
|
$this->hr();
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->out(__d('cake_console', 'You have more than one set of templates installed.'));
|
|
|
|
$this->out(__d('cake_console', 'Please choose the template set you wish to use:'));
|
2009-07-02 03:49:50 +00:00
|
|
|
$this->hr();
|
|
|
|
|
2009-07-01 04:24:20 +00:00
|
|
|
$i = 1;
|
|
|
|
$indexedPaths = array();
|
|
|
|
foreach ($this->templatePaths as $key => $path) {
|
|
|
|
$this->out($i . '. ' . $key);
|
|
|
|
$indexedPaths[$i] = $path;
|
|
|
|
$i++;
|
|
|
|
}
|
2011-03-19 17:32:35 +00:00
|
|
|
$index = $this->in(__d('cake_console', 'Which bake theme would you like to use?'), range(1, $i - 1), 1);
|
2009-07-02 03:49:50 +00:00
|
|
|
$themeNames = array_keys($this->templatePaths);
|
2010-10-24 19:27:44 +00:00
|
|
|
$this->params['theme'] = $themeNames[$index - 1];
|
2009-07-01 04:24:20 +00:00
|
|
|
return $indexedPaths[$index];
|
|
|
|
}
|
2009-07-24 19:18:37 +00:00
|
|
|
|
2009-07-01 04:24:20 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
2009-11-14 12:19:25 +00:00
|
|
|
*/
|
2011-05-30 20:26:42 +00:00
|
|
|
protected function _findTemplate($path, $directory, $filename) {
|
2009-07-01 04:24:20 +00:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
}
|
2011-03-19 17:32:35 +00:00
|
|
|
$this->err(__d('cake_console', 'Could not find template for %s', $filename));
|
2009-07-01 04:50:38 +00:00
|
|
|
return false;
|
2009-07-01 04:24:20 +00:00
|
|
|
}
|
2009-07-17 03:55:41 +00:00
|
|
|
}
|