TemplateTask.php 5.5 KB

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