TemplateTask.php 6.1 KB

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