PageRenderTime 34ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/lithium/template/view/adapter/File.php

https://bitbucket.org/thesyncim/front-biarq
PHP | 180 lines | 77 code | 23 blank | 80 comment | 4 complexity | 751961383f34031761a37daac440c104 MD5 | raw file
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2011, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\template\view\adapter;
  9. use lithium\util\String;
  10. use lithium\core\Libraries;
  11. use lithium\template\TemplateException;
  12. /**
  13. * The File adapter implements both template loading and rendering, and uses the
  14. * `lithium\template\view\Stream` class or `lithium\template\view\Compiler` class to auto-escape
  15. * template output with short tags (i.e. `<?=`).
  16. *
  17. * For more information about implementing your own template loaders or renderers, see the
  18. * `lithium\template\View` class.
  19. *
  20. * @see lithium\template\View
  21. * @see lithium\template\view\Compiler
  22. */
  23. class File extends \lithium\template\view\Renderer implements \ArrayAccess {
  24. /**
  25. * These configuration variables will automatically be assigned to their corresponding protected
  26. * properties when the object is initialized.
  27. *
  28. * @var array
  29. */
  30. protected $_autoConfig = array(
  31. 'classes' => 'merge', 'request', 'response', 'context',
  32. 'strings', 'handlers', 'view', 'compile', 'paths'
  33. );
  34. /**
  35. * Boolean flag indicating whether templates should be pre-compiled before inclusion. For more
  36. * information on template compilation, see `view\Compiler`.
  37. *
  38. * @see lithium\template\view\Compiler
  39. * @var boolean
  40. */
  41. protected $_compile = true;
  42. /**
  43. * An array containing the variables currently in the scope of the template. These values are
  44. * manipulable using array syntax against the template object, i.e. `$this['foo'] = 'bar'`
  45. * inside your template files.
  46. *
  47. * @var array
  48. */
  49. protected $_data = array();
  50. /**
  51. * Variables that have been set from a view/element/layout/etc. that should be available to the
  52. * same rendering context.
  53. *
  54. * @var array Key/value pairs of variables
  55. */
  56. protected $_vars = array();
  57. protected $_paths = array();
  58. /**
  59. * `File`'s dependencies. These classes are used by the output handlers to generate URLs
  60. * for dynamic resources and static assets, as well as compiling the templates.
  61. *
  62. * @see Renderer::$_handlers
  63. * @var array
  64. */
  65. protected $_classes = array(
  66. 'compiler' => 'lithium\template\view\Compiler',
  67. 'router' => 'lithium\net\http\Router',
  68. 'media' => 'lithium\net\http\Media'
  69. );
  70. public function __construct(array $config = array()) {
  71. $defaults = array(
  72. 'classes' => array(), 'compile' => true, 'extract' => true, 'paths' => array()
  73. );
  74. parent::__construct($config + $defaults);
  75. }
  76. /**
  77. * Renders content from a template file provided by `template()`.
  78. *
  79. * @param string $template
  80. * @param array|string $data
  81. * @param array $options
  82. * @return string
  83. */
  84. public function render($template, $data = array(), array $options = array()) {
  85. $defaults = array('context' => array());
  86. $options += $defaults;
  87. $this->_context = $options['context'] + $this->_context;
  88. $this->_data = (array) $data + $this->_vars;
  89. $template__ = $template;
  90. unset($options, $template, $defaults, $data);
  91. if ($this->_config['extract']) {
  92. extract($this->_data, EXTR_OVERWRITE);
  93. } elseif ($this->_view) {
  94. extract((array) $this->_view->outputFilters, EXTR_OVERWRITE);
  95. }
  96. ob_start();
  97. include $template__;
  98. return ob_get_clean();
  99. }
  100. /**
  101. * Returns a template file name
  102. *
  103. * @param string $type
  104. * @param array $params
  105. * @return string
  106. */
  107. public function template($type, array $params) {
  108. $library = Libraries::get(isset($params['library']) ? $params['library'] : true);
  109. $params['library'] = $library['path'];
  110. $path = $this->_paths($type, $params);
  111. if ($this->_compile) {
  112. $compiler = $this->_classes['compiler'];
  113. $path = $compiler::template($path);
  114. }
  115. return $path;
  116. }
  117. /**
  118. * Allows checking to see if a value is set in template data, i.e. `$this['foo']` in templates.
  119. *
  120. * @param string $offset The key / variable name to check.
  121. * @return boolean Returns `true` if the value is set, otherwise `false`.
  122. */
  123. public function offsetExists($offset) {
  124. return array_key_exists($offset, $this->_data);
  125. }
  126. public function offsetGet($offset) {
  127. return isset($this->_data[$offset]) ? $this->_data[$offset] : null;
  128. }
  129. public function offsetSet($offset, $value) {
  130. $this->_data[$offset] = $value;
  131. }
  132. public function offsetUnset($offset) {
  133. unset($this->_data[$offset]);
  134. }
  135. /**
  136. * Searches one or more path templates for a matching template file, and returns the file name.
  137. *
  138. * @param string $type
  139. * @param array $params The set of options keys to be interpolated into the path templates
  140. * when searching for the correct file to load.
  141. * @return string Returns the first template file found. Throws an exception if no templates
  142. * are available.
  143. */
  144. protected function _paths($type, array $params) {
  145. if (!isset($this->_paths[$type])) {
  146. throw new TemplateException("Invalid template type '{$type}'.");
  147. }
  148. foreach ((array) $this->_paths[$type] as $path) {
  149. if (!file_exists($path = String::insert($path, $params))) {
  150. continue;
  151. }
  152. return $path;
  153. }
  154. throw new TemplateException("Template not found at path `{$path}`.");
  155. }
  156. }
  157. ?>