PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/template/view/adapter/File.php

http://github.com/UnionOfRAD/lithium
PHP | 239 lines | 82 code | 23 blank | 134 comment | 4 complexity | 583a972b6e815249fc0b55f2a7aa2c5d MD5 | raw file
  1. <?php
  2. /**
  3. * li₃: the most RAD framework for PHP (http://li3.me)
  4. *
  5. * Copyright 2009, Union of RAD. All rights reserved. This source
  6. * code is distributed under the terms of the BSD 3-Clause License.
  7. * The full license text can be found in the LICENSE.txt file.
  8. */
  9. namespace lithium\template\view\adapter;
  10. use lithium\util\Text;
  11. use lithium\core\Libraries;
  12. use lithium\template\TemplateException;
  13. /**
  14. * The File adapter implements both template loading and rendering, and uses the
  15. * `lithium\template\view\Stream` class or `lithium\template\view\Compiler` class to auto-escape
  16. * template output with short tags (i.e. `<?=`).
  17. *
  18. * For more information about implementing your own template loaders or renderers, see the
  19. * `lithium\template\View` class.
  20. *
  21. * @see lithium\template\View
  22. * @see lithium\template\view\Compiler
  23. */
  24. class File extends \lithium\template\view\Renderer implements \ArrayAccess {
  25. /**
  26. * These configuration variables will automatically be assigned to their corresponding protected
  27. * properties when the object is initialized.
  28. *
  29. * @var array
  30. */
  31. protected $_autoConfig = [
  32. 'classes' => 'merge', 'request', 'response', 'context',
  33. 'strings', 'handlers', 'view', 'compile', 'paths'
  34. ];
  35. /**
  36. * Boolean flag indicating whether templates should be pre-compiled before inclusion. For more
  37. * information on template compilation, see `view\Compiler`.
  38. *
  39. * @see lithium\template\view\Compiler
  40. * @var boolean
  41. */
  42. protected $_compile = true;
  43. /**
  44. * An array containing the variables currently in the scope of the template. These values are
  45. * manipulable using array syntax against the template object, i.e. `$this['foo'] = 'bar'`
  46. * inside your template files.
  47. *
  48. * @var array
  49. */
  50. protected $_data = [];
  51. /**
  52. * Variables that have been set from a view/element/layout/etc. that should be available to the
  53. * same rendering context.
  54. *
  55. * @var array Key/value pairs of variables
  56. */
  57. protected $_vars = [];
  58. protected $_paths = [];
  59. /**
  60. * `File`'s dependencies. These classes are used by the output handlers to generate URLs
  61. * for dynamic resources and static assets, as well as compiling the templates.
  62. *
  63. * @see Renderer::$_handlers
  64. * @var array
  65. */
  66. protected $_classes = [
  67. 'compiler' => 'lithium\template\view\Compiler',
  68. 'router' => 'lithium\net\http\Router',
  69. 'media' => 'lithium\net\http\Media'
  70. ];
  71. /**
  72. * Constructor.
  73. *
  74. * @param array $config Configuration options.
  75. * @return void
  76. */
  77. public function __construct(array $config = []) {
  78. $defaults = [
  79. 'classes' => [],
  80. 'compile' => true,
  81. 'compiler' => [],
  82. 'extract' => true,
  83. 'paths' => []
  84. ];
  85. parent::__construct($config + $defaults);
  86. }
  87. /**
  88. * Renders content from a template file provided by `template()`.
  89. *
  90. * @param string $template
  91. * @param array|string $data
  92. * @param array $options
  93. * @return string
  94. */
  95. public function render($template, $data = [], array $options = []) {
  96. $defaults = ['context' => []];
  97. $options += $defaults;
  98. $this->_context = $options['context'] + $this->_context;
  99. $this->_data = (array) $data + $this->_vars;
  100. $this->_options = $options;
  101. $template__ = $template;
  102. unset($options, $template, $defaults, $data);
  103. if ($this->_config['extract']) {
  104. extract($this->_data, EXTR_OVERWRITE);
  105. } elseif ($this->_view) {
  106. extract((array) $this->_view->outputFilters, EXTR_OVERWRITE);
  107. }
  108. ob_start();
  109. include $template__;
  110. return ob_get_clean();
  111. }
  112. /**
  113. * Returns a template file name
  114. *
  115. * @param string $type
  116. * @param array $params
  117. * @return string
  118. */
  119. public function template($type, array $params) {
  120. $library = Libraries::get(isset($params['library']) ? $params['library'] : true);
  121. $params['library'] = $library['path'];
  122. $path = $this->_paths($type, $params);
  123. if ($this->_compile) {
  124. $compiler = $this->_classes['compiler'];
  125. $path = $compiler::template($path, $this->_config['compiler']);
  126. }
  127. return $path;
  128. }
  129. /**
  130. * Allows checking to see if a value is set in template data.
  131. *
  132. * Part of `ArrayAccess`.
  133. *
  134. * ```
  135. * isset($file['bar']);
  136. * $file->offsetExists('bar');
  137. * ```
  138. *
  139. * @param string $offset Key / variable name to check.
  140. * @return boolean Returns `true` if the value is set, otherwise `false`.
  141. */
  142. public function offsetExists($offset) {
  143. return array_key_exists($offset, $this->_data);
  144. }
  145. /**
  146. * Gets the offset, or null in the template data.
  147. *
  148. * Part of `ArrayAccess`.
  149. *
  150. * ```
  151. * $file['bar'];
  152. * $file->offsetGet('bar');
  153. * ```
  154. *
  155. * @param string $offset Key / variable name to check.
  156. * @return mixed
  157. */
  158. public function offsetGet($offset) {
  159. return isset($this->_data[$offset]) ? $this->_data[$offset] : null;
  160. }
  161. /**
  162. * Sets the offset with the given value.
  163. *
  164. * Part of `ArrayAccess`.
  165. *
  166. * ```
  167. * $file['bar'] = 'baz';
  168. * $file->offsetSet('bar', 'baz');
  169. * ```
  170. *
  171. * @param string $offset Key / variable name to check.
  172. * @param mixed $value Value you wish to set to `$offset`.
  173. * @return void
  174. */
  175. public function offsetSet($offset, $value) {
  176. $this->_data[$offset] = $value;
  177. }
  178. /**
  179. * Unsets the given offset.
  180. *
  181. * Part of `ArrayAccess`.
  182. *
  183. * ```
  184. * unset($file['bar']);
  185. * $file->offsetUnset('bar');
  186. * ```
  187. *
  188. * @param string $offset Key / variable name to check.
  189. * @return void
  190. */
  191. public function offsetUnset($offset) {
  192. unset($this->_data[$offset]);
  193. }
  194. /**
  195. * Searches one or more path templates for a matching template file, and returns the file name.
  196. *
  197. * @param string $type
  198. * @param array $params The set of options keys to be interpolated into the path templates
  199. * when searching for the correct file to load.
  200. * @return string Returns the first template file found. Throws an exception if no templates
  201. * are available.
  202. */
  203. protected function _paths($type, array $params) {
  204. if (!isset($this->_paths[$type])) {
  205. throw new TemplateException("Invalid template type '{$type}'.");
  206. }
  207. foreach ((array) $this->_paths[$type] as $path) {
  208. if (!file_exists($path = Text::insert($path, $params))) {
  209. continue;
  210. }
  211. return $path;
  212. }
  213. throw new TemplateException("Template not found at path `{$path}`.");
  214. }
  215. }
  216. ?>