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.1 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.1
      • 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
    • Network
      • Email
      • Http
    • Routing
      • 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 2005-2012, 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-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 14:  * @link          http://cakephp.org CakePHP(tm) Project
 15:  * @since         CakePHP(tm) v 1.3
 16:  * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 17:  */
 18: 
 19: App::uses('AppShell', 'Console/Command');
 20: App::uses('Folder', 'Utility');
 21: 
 22: /**
 23:  * Template Task can generate templated output Used in other Tasks.
 24:  * Acts like a simplified View class.
 25:  *
 26:  * @package       Cake.Console.Command.Task
 27:  */
 28: class TemplateTask extends AppShell {
 29: 
 30: /**
 31:  * variables to add to template scope
 32:  *
 33:  * @var array
 34:  */
 35:     public $templateVars = array();
 36: 
 37: /**
 38:  * Paths to look for templates on.
 39:  * Contains a list of $theme => $path
 40:  *
 41:  * @var array
 42:  */
 43:     public $templatePaths = array();
 44: 
 45: /**
 46:  * Initialize callback.  Setup paths for the template task.
 47:  *
 48:  * @return void
 49:  */
 50:     public function initialize() {
 51:         $this->templatePaths = $this->_findThemes();
 52:     }
 53: 
 54: /**
 55:  * Find the paths to all the installed shell themes in the app.
 56:  *
 57:  * Bake themes are directories not named `skel` inside a `Console/Templates` path.
 58:  *
 59:  * @return array Array of bake themes that are installed.
 60:  */
 61:     protected function _findThemes() {
 62:         $paths = array();
 63:         $core = current(App::core('Console'));
 64:         $separator = DS === '/' ? '/' : '\\\\';
 65:         $core = preg_replace('#shells' . $separator . '$#', '', $core);
 66: 
 67:         $Folder = new Folder($core . 'Templates' . DS . 'default');
 68: 
 69:         $contents = $Folder->read();
 70:         $themeFolders = $contents[0];
 71: 
 72:         $plugins = App::objects('plugin');
 73:         $paths[] = $core;
 74:         foreach ($plugins as $plugin) {
 75:             $paths[] = $this->_pluginPath($plugin) . 'Console' . DS;
 76:         }
 77: 
 78:         $paths = array_merge($paths, App::path('Console'));
 79: 
 80:         // TEMPORARY TODO remove when all paths are DS terminated
 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:  * Set variable values to the template scope
108:  *
109:  * @param string|array $one A string or an array of data.
110:  * @param mixed $two Value in case $one is a string (which then works as the key).
111:  *   Unused if $one is an associative array, otherwise serves as the values to $one's keys.
112:  * @return void
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:  * Runs the template
133:  *
134:  * @param string $directory directory / type of thing you want
135:  * @param string $filename template name
136:  * @param array $vars Additional vars to set to template scope.
137:  * @return string contents of generated code template
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:  * Find the theme name for the current operation.
161:  * If there is only one theme in $templatePaths it will be used.
162:  * If there is a -theme param in the cli args, it will be used.
163:  * If there is more than one installed theme user interaction will happen
164:  *
165:  * @return string returns the path to the selected theme.
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:  * Find a template inside a directory inside a path.
196:  * Will scan all other theme dirs if the template is not found in the first directory.
197:  *
198:  * @param string $path The initial path to look for the file on. If it is not found fallbacks will be used.
199:  * @param string $directory Subdirectory to look for ie. 'views', 'objects'
200:  * @param string $filename lower_case_underscored filename you want.
201:  * @return string filename will exit program if template is not found.
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: 
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