PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/eryx/php-framework-benchmark
PHP | 218 lines | 118 code | 21 blank | 79 comment | 18 complexity | a2b47a9b240eda9f2db1462264e8da16 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause
  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-2012, 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-2012, 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. * They are listed in this order: app -> plugin -> default
  53. *
  54. * @return array Array of bake themes that are installed.
  55. */
  56. protected function _findThemes() {
  57. $paths = App::path('Console');
  58. $plugins = App::objects('plugin');
  59. foreach ($plugins as $plugin) {
  60. $paths[] = $this->_pluginPath($plugin) . 'Console' . DS;
  61. }
  62. $core = current(App::core('Console'));
  63. $separator = DS === '/' ? '/' : '\\\\';
  64. $core = preg_replace('#shells' . $separator . '$#', '', $core);
  65. $Folder = new Folder($core . 'Templates' . DS . 'default');
  66. $contents = $Folder->read();
  67. $themeFolders = $contents[0];
  68. $paths[] = $core;
  69. foreach ($paths as $i => $path) {
  70. $paths[$i] = rtrim($path, DS) . DS;
  71. }
  72. $themes = array();
  73. foreach ($paths as $path) {
  74. $Folder = new Folder($path . 'Templates', false);
  75. $contents = $Folder->read();
  76. $subDirs = $contents[0];
  77. foreach ($subDirs as $dir) {
  78. if (empty($dir) || preg_match('@^skel$|_skel$@', $dir)) {
  79. continue;
  80. }
  81. $Folder = new Folder($path . 'Templates' . DS . $dir);
  82. $contents = $Folder->read();
  83. $subDirs = $contents[0];
  84. if (array_intersect($contents[0], $themeFolders)) {
  85. $templateDir = $path . 'Templates' . DS . $dir . DS;
  86. $themes[$dir] = $templateDir;
  87. }
  88. }
  89. }
  90. return $themes;
  91. }
  92. /**
  93. * Set variable values to the template scope
  94. *
  95. * @param string|array $one A string or an array of data.
  96. * @param string|array $two Value in case $one is a string (which then works as the key).
  97. * Unused if $one is an associative array, otherwise serves as the values to $one's keys.
  98. * @return void
  99. */
  100. public function set($one, $two = null) {
  101. if (is_array($one)) {
  102. if (is_array($two)) {
  103. $data = array_combine($one, $two);
  104. } else {
  105. $data = $one;
  106. }
  107. } else {
  108. $data = array($one => $two);
  109. }
  110. if ($data == null) {
  111. return false;
  112. }
  113. $this->templateVars = $data + $this->templateVars;
  114. }
  115. /**
  116. * Runs the template
  117. *
  118. * @param string $directory directory / type of thing you want
  119. * @param string $filename template name
  120. * @param array $vars Additional vars to set to template scope.
  121. * @return string contents of generated code template
  122. */
  123. public function generate($directory, $filename, $vars = null) {
  124. if ($vars !== null) {
  125. $this->set($vars);
  126. }
  127. if (empty($this->templatePaths)) {
  128. $this->initialize();
  129. }
  130. $themePath = $this->getThemePath();
  131. $templateFile = $this->_findTemplate($themePath, $directory, $filename);
  132. if ($templateFile) {
  133. extract($this->templateVars);
  134. ob_start();
  135. ob_implicit_flush(0);
  136. include $templateFile;
  137. $content = ob_get_clean();
  138. return $content;
  139. }
  140. return '';
  141. }
  142. /**
  143. * Find the theme name for the current operation.
  144. * If there is only one theme in $templatePaths it will be used.
  145. * If there is a -theme param in the cli args, it will be used.
  146. * If there is more than one installed theme user interaction will happen
  147. *
  148. * @return string returns the path to the selected theme.
  149. */
  150. public function getThemePath() {
  151. if (count($this->templatePaths) == 1) {
  152. $paths = array_values($this->templatePaths);
  153. return $paths[0];
  154. }
  155. if (!empty($this->params['theme']) && isset($this->templatePaths[$this->params['theme']])) {
  156. return $this->templatePaths[$this->params['theme']];
  157. }
  158. $this->hr();
  159. $this->out(__d('cake_console', 'You have more than one set of templates installed.'));
  160. $this->out(__d('cake_console', 'Please choose the template set you wish to use:'));
  161. $this->hr();
  162. $i = 1;
  163. $indexedPaths = array();
  164. foreach ($this->templatePaths as $key => $path) {
  165. $this->out($i . '. ' . $key);
  166. $indexedPaths[$i] = $path;
  167. $i++;
  168. }
  169. $index = $this->in(__d('cake_console', 'Which bake theme would you like to use?'), range(1, $i - 1), 1);
  170. $themeNames = array_keys($this->templatePaths);
  171. $this->params['theme'] = $themeNames[$index - 1];
  172. return $indexedPaths[$index];
  173. }
  174. /**
  175. * Find a template inside a directory inside a path.
  176. * Will scan all other theme dirs if the template is not found in the first directory.
  177. *
  178. * @param string $path The initial path to look for the file on. If it is not found fallbacks will be used.
  179. * @param string $directory Subdirectory to look for ie. 'views', 'objects'
  180. * @param string $filename lower_case_underscored filename you want.
  181. * @return string filename will exit program if template is not found.
  182. */
  183. protected function _findTemplate($path, $directory, $filename) {
  184. $themeFile = $path . $directory . DS . $filename . '.ctp';
  185. if (file_exists($themeFile)) {
  186. return $themeFile;
  187. }
  188. foreach ($this->templatePaths as $path) {
  189. $templatePath = $path . $directory . DS . $filename . '.ctp';
  190. if (file_exists($templatePath)) {
  191. return $templatePath;
  192. }
  193. }
  194. $this->err(__d('cake_console', 'Could not find template for %s', $filename));
  195. return false;
  196. }
  197. }