TemplateTask.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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-2010, 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-2010, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package cake.console.shells.tasks
  16. * @since CakePHP(tm) v 1.3
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Folder', 'Utility');
  20. class TemplateTask extends Shell {
  21. /**
  22. * variables to add to template scope
  23. *
  24. * @var array
  25. */
  26. public $templateVars = array();
  27. /**
  28. * Paths to look for templates on.
  29. * Contains a list of $theme => $path
  30. *
  31. * @var array
  32. */
  33. public $templatePaths = array();
  34. /**
  35. * Initialize callback. Setup paths for the template task.
  36. *
  37. * @access public
  38. * @return void
  39. */
  40. public 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. protected function _findThemes() {
  51. $paths = App::path('Console');
  52. $core = current(App::core('Console'));
  53. $separator = DS === '/' ? '/' : '\\\\';
  54. $core = preg_replace('#shells' . $separator . '$#', '', $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) . 'console' . DS . 'shells' . DS;
  61. $paths[] = $this->_pluginPath($plugin) . 'vendors' . DS . 'shells' . DS;
  62. }
  63. $paths[] = $core;
  64. // TEMPORARY TODO remove when all paths are DS terminated
  65. foreach ($paths as $i => $path) {
  66. $paths[$i] = rtrim($path, DS) . DS;
  67. }
  68. $themes = array();
  69. foreach ($paths as $path) {
  70. $Folder = new Folder($path . 'templates', false);
  71. $contents = $Folder->read();
  72. $subDirs = $contents[0];
  73. foreach ($subDirs as $dir) {
  74. if (empty($dir) || preg_match('@^skel$|_skel$@', $dir)) {
  75. continue;
  76. }
  77. $Folder = new Folder($path . 'templates' . DS . $dir);
  78. $contents = $Folder->read();
  79. $subDirs = $contents[0];
  80. if (array_intersect($contents[0], $themeFolders)) {
  81. $templateDir = $path . 'templates' . DS . $dir . DS;
  82. $themes[$dir] = $templateDir;
  83. }
  84. }
  85. }
  86. return $themes;
  87. }
  88. /**
  89. * Set variable values to the template scope
  90. *
  91. * @param mixed $one A string or an array of data.
  92. * @param mixed $two Value in case $one is a string (which then works as the key).
  93. * Unused if $one is an associative array, otherwise serves as the values to $one's keys.
  94. * @return void
  95. */
  96. public function set($one, $two = null) {
  97. $data = null;
  98. if (is_array($one)) {
  99. if (is_array($two)) {
  100. $data = array_combine($one, $two);
  101. } else {
  102. $data = $one;
  103. }
  104. } else {
  105. $data = array($one => $two);
  106. }
  107. if ($data == null) {
  108. return false;
  109. }
  110. $this->templateVars = $data + $this->templateVars;
  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. public 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. public 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.'));
  158. $this->out(__('Please choose the template set you wish to use:'));
  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?'), range(1, $i - 1), 1);
  168. $themeNames = array_keys($this->templatePaths);
  169. $this->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. public 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(__('Could not find template for %s', $filename));
  194. return false;
  195. }
  196. }