CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.3 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.3
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • BakeTask
  • ControllerTask
  • DbConfigTask
  • ExtractTask
  • FixtureTask
  • ModelTask
  • PluginTask
  • ProjectTask
  • TemplateTask
  • TestTask
  • ViewTask
  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 (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9:  *
 10:  * Licensed under The MIT License
 11:  * For full copyright and license information, please see the LICENSE.txt
 12:  * Redistributions of files must retain the above copyright notice.
 13:  *
 14:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 15:  * @link          http://cakephp.org CakePHP(tm) Project
 16:  * @since         CakePHP(tm) v 1.3
 17:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 18:  */
 19: 
 20: App::uses('AppShell', 'Console/Command');
 21: App::uses('Folder', 'Utility');
 22: 
 23: /**
 24:  * Template Task can generate templated output Used in other Tasks.
 25:  * Acts like a simplified View class.
 26:  *
 27:  * @package       Cake.Console.Command.Task
 28:  */
 29: class TemplateTask extends AppShell {
 30: 
 31: /**
 32:  * variables to add to template scope
 33:  *
 34:  * @var array
 35:  */
 36:     public $templateVars = array();
 37: 
 38: /**
 39:  * Paths to look for templates on.
 40:  * Contains a list of $theme => $path
 41:  *
 42:  * @var array
 43:  */
 44:     public $templatePaths = array();
 45: 
 46: /**
 47:  * Initialize callback. Setup paths for the template task.
 48:  *
 49:  * @return void
 50:  */
 51:     public function initialize() {
 52:         $this->templatePaths = $this->_findThemes();
 53:     }
 54: 
 55: /**
 56:  * Find the paths to all the installed shell themes in the app.
 57:  *
 58:  * Bake themes are directories not named `skel` inside a `Console/Templates` path.
 59:  * They are listed in this order: app -> plugin -> default
 60:  *
 61:  * @return array Array of bake themes that are installed.
 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$|[email protected]', $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:  * Set variable values to the template scope
109:  *
110:  * @param string|array $one A string or an array of data.
111:  * @param string|array $two Value in case $one is a string (which then works as the key).
112:  *   Unused if $one is an associative array, otherwise serves as the values to $one's keys.
113:  * @return void
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:  * Runs the template
134:  *
135:  * @param string $directory directory / type of thing you want
136:  * @param string $filename template name
137:  * @param array $vars Additional vars to set to template scope.
138:  * @return string contents of generated code template
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:  * Find the theme name for the current operation.
162:  * If there is only one theme in $templatePaths it will be used.
163:  * If there is a -theme param in the cli args, it will be used.
164:  * If there is more than one installed theme user interaction will happen
165:  *
166:  * @return string returns the path to the selected theme.
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:  * Find a template inside a directory inside a path.
197:  * Will scan all other theme dirs if the template is not found in the first directory.
198:  *
199:  * @param string $path The initial path to look for the file on. If it is not found fallbacks will be used.
200:  * @param string $directory Subdirectory to look for ie. 'views', 'objects'
201:  * @param string $filename lower_case_underscored filename you want.
202:  * @return string filename will exit program if template is not found.
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: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs