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)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2009-05-13 01:19:15 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2009-05-13 01:19:15 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2010-01-26 22:03:03 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
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
|
|
|
|
2011-11-25 21:05:00 +00:00
|
|
|
App::uses('AppShell', 'Console/Command');
|
2010-12-08 00:20:56 +00:00
|
|
|
App::uses('Folder', 'Utility');
|
2011-11-25 21:05:00 +00:00
|
|
|
|
2011-07-26 01:46:52 +00:00
|
|
|
/**
|
|
|
|
* Template Task can generate templated output Used in other Tasks.
|
|
|
|
* Acts like a simplified View class.
|
|
|
|
*
|
2011-07-26 06:16:14 +00:00
|
|
|
* @package Cake.Console.Command.Task
|
2011-07-26 01:46:52 +00:00
|
|
|
*/
|
2011-11-25 21:05:00 +00:00
|
|
|
class TemplateTask extends AppShell {
|
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
|
|
|
/**
|
2012-12-22 22:48:15 +00:00
|
|
|
* Initialize callback. Setup paths for the template task.
|
2009-05-13 01:19:15 +00:00
|
|
|
*
|
|
|
|
* @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
|
|
|
*
|
2011-05-31 03:02:39 +00:00
|
|
|
* Bake themes are directories not named `skel` inside a `Console/Templates` path.
|
2012-04-24 00:40:42 +00:00
|
|
|
* They are listed in this order: app -> plugin -> default
|
2012-07-18 01:55:29 +00:00
|
|
|
*
|
2009-07-01 04:24:20 +00:00
|
|
|
* @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() {
|
2012-04-24 00:40:42 +00:00
|
|
|
$paths = App::path('Console');
|
2012-05-01 14:35:48 +00:00
|
|
|
|
2012-04-24 00:40:42 +00:00
|
|
|
$plugins = App::objects('plugin');
|
|
|
|
foreach ($plugins as $plugin) {
|
|
|
|
$paths[] = $this->_pluginPath($plugin) . 'Console' . DS;
|
|
|
|
}
|
|
|
|
|
2011-03-07 05:15:00 +00:00
|
|
|
$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
|
|
|
|
2011-05-31 03:02:39 +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];
|
|
|
|
|
2011-11-18 05:00:45 +00:00
|
|
|
$paths[] = $core;
|
2009-07-28 07:02:29 +00:00
|
|
|
|
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) {
|
2011-05-31 03:02:39 +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;
|
|
|
|
}
|
2011-05-31 03:02:39 +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)) {
|
2011-05-31 03:02:39 +00:00
|
|
|
$templateDir = $path . 'Templates' . DS . $dir . DS;
|
2009-07-28 07:02:29 +00:00
|
|
|
$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
|
|
|
|
*
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param string|array $one A string or an array of data.
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|array $two Value in case $one is a string (which then works as the key).
|
2009-05-13 01:19:15 +00:00
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2012-09-14 17:26:30 +00:00
|
|
|
if (!$data) {
|
2009-05-13 01:19:15 +00:00
|
|
|
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
|
2011-08-01 02:57:17 +00:00
|
|
|
* @param array $vars Additional vars to set to template scope.
|
|
|
|
* @return string 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);
|
2012-03-03 23:55:07 +00:00
|
|
|
include $templateFile;
|
2009-05-13 01:19:15 +00:00
|
|
|
$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() {
|
2012-09-14 18:26:00 +00:00
|
|
|
if (count($this->templatePaths) === 1) {
|
2009-07-01 04:24:20 +00:00
|
|
|
$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.
|
|
|
|
* @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
|
|
|
}
|
2012-03-03 23:55:07 +00:00
|
|
|
|
2009-07-17 03:55:41 +00:00
|
|
|
}
|