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