PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/cake/console/libs/tasks/template.php

https://github.com/msadouni/cakephp2x
PHP | 212 lines | 119 code | 15 blank | 78 comment | 18 complexity | 2a645b90d79c72e95e0761d4817f3f13 MD5 | raw file
  1. <?php
  2. /**
  3. * Template Task can generate templated output Used in other Tasks
  4. *
  5. * PHP version 5.x
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake
  16. * @subpackage cake.console.libs.tasks
  17. * @since CakePHP(tm) v 1.3
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. class TemplateTask extends Shell {
  21. /**
  22. * variables to add to template scope
  23. *
  24. * @var array
  25. */
  26. var $templateVars = array();
  27. /**
  28. * Paths to look for templates on.
  29. * Contains a list of $theme => $path
  30. *
  31. * @var array
  32. */
  33. var $templatePaths = array();
  34. /**
  35. * Initialize callback. Setup paths for the template task.
  36. *
  37. * @access public
  38. * @return void
  39. */
  40. function initialize() {
  41. $this->templatePaths = $this->_findThemes();
  42. }
  43. /**
  44. * Find the paths to all the installed shell themes in the app.
  45. *
  46. * Bake themes are directories not named `skel` inside a `vendors/shells/templates` path.
  47. *
  48. * @return array Array of bake themes that are installed.
  49. */
  50. function _findThemes() {
  51. $paths = App::path('shells');
  52. $core = array_pop($paths);
  53. $core = str_replace('libs' . DS, '', $core);
  54. $paths[] = $core;
  55. $Folder =& new Folder($core . 'templates' . DS . 'default');
  56. $contents = $Folder->read();
  57. $themeFolders = $contents[0];
  58. $plugins = App::objects('plugin');
  59. foreach ($plugins as $plugin) {
  60. $paths[] = $this->_pluginPath($plugin) . 'vendors' . DS . 'shells' . DS;
  61. }
  62. // TEMPORARY TODO remove when all paths are DS terminated
  63. foreach ($paths as $i => $path) {
  64. $paths[$i] = rtrim($path, DS) . DS;
  65. }
  66. $themes = array();
  67. foreach ($paths as $path) {
  68. $Folder =& new Folder($path . 'templates', false);
  69. $contents = $Folder->read();
  70. $subDirs = $contents[0];
  71. foreach ($subDirs as $dir) {
  72. if (empty($dir) || preg_match('@^skel$|_skel$@', $dir)) {
  73. continue;
  74. }
  75. $Folder =& new Folder($path . 'templates' . DS . $dir);
  76. $contents = $Folder->read();
  77. $subDirs = $contents[0];
  78. if (array_intersect($contents[0], $themeFolders)) {
  79. $templateDir = $path . 'templates' . DS . $dir . DS;
  80. $themes[$dir] = $templateDir;
  81. }
  82. }
  83. }
  84. return $themes;
  85. }
  86. /**
  87. * Set variable values to the template scope
  88. *
  89. * @param mixed $one A string or an array of data.
  90. * @param mixed $two Value in case $one is a string (which then works as the key).
  91. * Unused if $one is an associative array, otherwise serves as the values to $one's keys.
  92. * @return void
  93. */
  94. function set($one, $two = null) {
  95. $data = null;
  96. if (is_array($one)) {
  97. if (is_array($two)) {
  98. $data = array_combine($one, $two);
  99. } else {
  100. $data = $one;
  101. }
  102. } else {
  103. $data = array($one => $two);
  104. }
  105. if ($data == null) {
  106. return false;
  107. }
  108. foreach ($data as $name => $value) {
  109. $this->templateVars[$name] = $value;
  110. }
  111. }
  112. /**
  113. * Runs the template
  114. *
  115. * @param string $directory directory / type of thing you want
  116. * @param string $filename template name
  117. * @param string $vars Additional vars to set to template scope.
  118. * @access public
  119. * @return contents of generated code template
  120. */
  121. function generate($directory, $filename, $vars = null) {
  122. if ($vars !== null) {
  123. $this->set($vars);
  124. }
  125. if (empty($this->templatePaths)) {
  126. $this->initialize();
  127. }
  128. $themePath = $this->getThemePath();
  129. $templateFile = $this->_findTemplate($themePath, $directory, $filename);
  130. if ($templateFile) {
  131. extract($this->templateVars);
  132. ob_start();
  133. ob_implicit_flush(0);
  134. include($templateFile);
  135. $content = ob_get_clean();
  136. return $content;
  137. }
  138. return '';
  139. }
  140. /**
  141. * Find the theme name for the current operation.
  142. * If there is only one theme in $templatePaths it will be used.
  143. * If there is a -theme param in the cli args, it will be used.
  144. * If there is more than one installed theme user interaction will happen
  145. *
  146. * @return string returns the path to the selected theme.
  147. */
  148. function getThemePath() {
  149. if (count($this->templatePaths) == 1) {
  150. $paths = array_values($this->templatePaths);
  151. return $paths[0];
  152. }
  153. if (!empty($this->params['theme']) && isset($this->templatePaths[$this->params['theme']])) {
  154. return $this->templatePaths[$this->params['theme']];
  155. }
  156. $this->hr();
  157. $this->out(__('You have more than one set of templates installed.', true));
  158. $this->out(__('Please choose the template set you wish to use:', true));
  159. $this->hr();
  160. $i = 1;
  161. $indexedPaths = array();
  162. foreach ($this->templatePaths as $key => $path) {
  163. $this->out($i . '. ' . $key);
  164. $indexedPaths[$i] = $path;
  165. $i++;
  166. }
  167. $index = $this->in(__('Which bake theme would you like to use?', true), range(1, $i - 1), 1);
  168. $themeNames = array_keys($this->templatePaths);
  169. $this->Dispatch->params['theme'] = $themeNames[$index - 1];
  170. return $indexedPaths[$index];
  171. }
  172. /**
  173. * Find a template inside a directory inside a path.
  174. * Will scan all other theme dirs if the template is not found in the first directory.
  175. *
  176. * @param string $path The initial path to look for the file on. If it is not found fallbacks will be used.
  177. * @param string $directory Subdirectory to look for ie. 'views', 'objects'
  178. * @param string $filename lower_case_underscored filename you want.
  179. * @access public
  180. * @return string filename will exit program if template is not found.
  181. */
  182. function _findTemplate($path, $directory, $filename) {
  183. $themeFile = $path . $directory . DS . $filename . '.ctp';
  184. if (file_exists($themeFile)) {
  185. return $themeFile;
  186. }
  187. foreach ($this->templatePaths as $path) {
  188. $templatePath = $path . $directory . DS . $filename . '.ctp';
  189. if (file_exists($templatePath)) {
  190. return $templatePath;
  191. }
  192. }
  193. $this->err(sprintf(__('Could not find template for %s', true), $filename));
  194. return false;
  195. }
  196. }
  197. ?>