source: Dev/branches/cakephp/cake/console/libs/tasks/template.php @ 126

Last change on this file since 126 was 126, checked in by fpvanagthoven, 14 years ago

Cakephp branch.

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