1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19:
20: App::uses('AppShell', 'Console/Command');
21: App::uses('Folder', 'Utility');
22:
23: 24: 25: 26: 27: 28:
29: class TemplateTask extends AppShell {
30:
31: 32: 33: 34: 35:
36: public $templateVars = array();
37:
38: 39: 40: 41: 42: 43:
44: public $templatePaths = array();
45:
46: 47: 48: 49: 50:
51: public function initialize() {
52: $this->templatePaths = $this->_findThemes();
53: }
54:
55: 56: 57: 58: 59: 60: 61: 62:
63: protected function _findThemes() {
64: $paths = App::path('Console');
65:
66: $plugins = App::objects('plugin');
67: foreach ($plugins as $plugin) {
68: $paths[] = $this->_pluginPath($plugin) . 'Console' . DS;
69: }
70:
71: $core = current(App::core('Console'));
72: $separator = DS === '/' ? '/' : '\\\\';
73: $core = preg_replace('#shells' . $separator . '$#', '', $core);
74:
75: $Folder = new Folder($core . 'Templates' . DS . 'default');
76:
77: $contents = $Folder->read();
78: $themeFolders = $contents[0];
79:
80: $paths[] = $core;
81:
82: foreach ($paths as $i => $path) {
83: $paths[$i] = rtrim($path, DS) . DS;
84: }
85:
86: $themes = array();
87: foreach ($paths as $path) {
88: $Folder = new Folder($path . 'Templates', false);
89: $contents = $Folder->read();
90: $subDirs = $contents[0];
91: foreach ($subDirs as $dir) {
92: if (empty($dir) || preg_match('@^skel$|_skel$@', $dir)) {
93: continue;
94: }
95: $Folder = new Folder($path . 'Templates' . DS . $dir);
96: $contents = $Folder->read();
97: $subDirs = $contents[0];
98: if (array_intersect($contents[0], $themeFolders)) {
99: $templateDir = $path . 'Templates' . DS . $dir . DS;
100: $themes[$dir] = $templateDir;
101: }
102: }
103: }
104: return $themes;
105: }
106:
107: 108: 109: 110: 111: 112: 113: 114:
115: public function set($one, $two = null) {
116: if (is_array($one)) {
117: if (is_array($two)) {
118: $data = array_combine($one, $two);
119: } else {
120: $data = $one;
121: }
122: } else {
123: $data = array($one => $two);
124: }
125:
126: if (!$data) {
127: return false;
128: }
129: $this->templateVars = $data + $this->templateVars;
130: }
131:
132: 133: 134: 135: 136: 137: 138: 139:
140: public function generate($directory, $filename, $vars = null) {
141: if ($vars !== null) {
142: $this->set($vars);
143: }
144: if (empty($this->templatePaths)) {
145: $this->initialize();
146: }
147: $themePath = $this->getThemePath();
148: $templateFile = $this->_findTemplate($themePath, $directory, $filename);
149: if ($templateFile) {
150: extract($this->templateVars);
151: ob_start();
152: ob_implicit_flush(0);
153: include $templateFile;
154: $content = ob_get_clean();
155: return $content;
156: }
157: return '';
158: }
159:
160: 161: 162: 163: 164: 165: 166: 167:
168: public function getThemePath() {
169: if (count($this->templatePaths) === 1) {
170: $paths = array_values($this->templatePaths);
171: return $paths[0];
172: }
173: if (!empty($this->params['theme']) && isset($this->templatePaths[$this->params['theme']])) {
174: return $this->templatePaths[$this->params['theme']];
175: }
176:
177: $this->hr();
178: $this->out(__d('cake_console', 'You have more than one set of templates installed.'));
179: $this->out(__d('cake_console', 'Please choose the template set you wish to use:'));
180: $this->hr();
181:
182: $i = 1;
183: $indexedPaths = array();
184: foreach ($this->templatePaths as $key => $path) {
185: $this->out($i . '. ' . $key);
186: $indexedPaths[$i] = $path;
187: $i++;
188: }
189: $index = $this->in(__d('cake_console', 'Which bake theme would you like to use?'), range(1, $i - 1), 1);
190: $themeNames = array_keys($this->templatePaths);
191: $this->params['theme'] = $themeNames[$index - 1];
192: return $indexedPaths[$index];
193: }
194:
195: 196: 197: 198: 199: 200: 201: 202: 203:
204: protected function _findTemplate($path, $directory, $filename) {
205: $themeFile = $path . $directory . DS . $filename . '.ctp';
206: if (file_exists($themeFile)) {
207: return $themeFile;
208: }
209: foreach ($this->templatePaths as $path) {
210: $templatePath = $path . $directory . DS . $filename . '.ctp';
211: if (file_exists($templatePath)) {
212: return $templatePath;
213: }
214: }
215: $this->err(__d('cake_console', 'Could not find template for %s', $filename));
216: return false;
217: }
218:
219: }
220: