PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/Console/Command/Task/TemplateTask.php

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