TemplateTask.php 5.0 KB

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