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

/FSN/library/Slim/View.php

https://gitlab.com/r.collas/site_central
PHP | 282 lines | 100 code | 26 blank | 156 comment | 9 complexity | dbbfdf9e5acb0ec85b46d9967540d2d8 MD5 | raw file
  1. <?php
  2. /**
  3. * Slim - a micro PHP 5 framework
  4. *
  5. * @author Josh Lockhart <info@slimframework.com>
  6. * @copyright 2011 Josh Lockhart
  7. * @link http://www.slimframework.com
  8. * @license http://www.slimframework.com/license
  9. * @version 2.6.1
  10. * @package Slim
  11. *
  12. * MIT LICENSE
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining
  15. * a copy of this software and associated documentation files (the
  16. * "Software"), to deal in the Software without restriction, including
  17. * without limitation the rights to use, copy, modify, merge, publish,
  18. * distribute, sublicense, and/or sell copies of the Software, and to
  19. * permit persons to whom the Software is furnished to do so, subject to
  20. * the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be
  23. * included in all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. */
  33. namespace Slim;
  34. /**
  35. * View
  36. *
  37. * The view is responsible for rendering a template. The view
  38. * should subclass \Slim\View and implement this interface:
  39. *
  40. * public render(string $template);
  41. *
  42. * This method should render the specified template and return
  43. * the resultant string.
  44. *
  45. * @package Slim
  46. * @author Josh Lockhart
  47. * @since 1.0.0
  48. */
  49. class View
  50. {
  51. /**
  52. * Data available to the view templates
  53. * @var \Slim\Helper\Set
  54. */
  55. protected $data;
  56. /**
  57. * Path to templates base directory (without trailing slash)
  58. * @var string
  59. */
  60. protected $templatesDirectory;
  61. /**
  62. * Constructor
  63. */
  64. public function __construct()
  65. {
  66. $this->data = new \Slim\Helper\Set();
  67. }
  68. /********************************************************************************
  69. * Data methods
  70. *******************************************************************************/
  71. /**
  72. * Does view data have value with key?
  73. * @param string $key
  74. * @return boolean
  75. */
  76. public function has($key)
  77. {
  78. return $this->data->has($key);
  79. }
  80. /**
  81. * Return view data value with key
  82. * @param string $key
  83. * @return mixed
  84. */
  85. public function get($key)
  86. {
  87. return $this->data->get($key);
  88. }
  89. /**
  90. * Set view data value with key
  91. * @param string $key
  92. * @param mixed $value
  93. */
  94. public function set($key, $value)
  95. {
  96. $this->data->set($key, $value);
  97. }
  98. /**
  99. * Set view data value as Closure with key
  100. * @param string $key
  101. * @param mixed $value
  102. */
  103. public function keep($key, \Closure $value)
  104. {
  105. $this->data->keep($key, $value);
  106. }
  107. /**
  108. * Return view data
  109. * @return array
  110. */
  111. public function all()
  112. {
  113. return $this->data->all();
  114. }
  115. /**
  116. * Replace view data
  117. * @param array $data
  118. */
  119. public function replace(array $data)
  120. {
  121. $this->data->replace($data);
  122. }
  123. /**
  124. * Clear view data
  125. */
  126. public function clear()
  127. {
  128. $this->data->clear();
  129. }
  130. /********************************************************************************
  131. * Legacy data methods
  132. *******************************************************************************/
  133. /**
  134. * DEPRECATION WARNING! This method will be removed in the next major point release
  135. *
  136. * Get data from view
  137. */
  138. public function getData($key = null)
  139. {
  140. if (!is_null($key)) {
  141. return isset($this->data[$key]) ? $this->data[$key] : null;
  142. }
  143. return $this->data->all();
  144. }
  145. /**
  146. * DEPRECATION WARNING! This method will be removed in the next major point release
  147. *
  148. * Set data for view
  149. */
  150. public function setData()
  151. {
  152. $args = func_get_args();
  153. if (count($args) === 1 && is_array($args[0])) {
  154. $this->data->replace($args[0]);
  155. } elseif (count($args) === 2) {
  156. // Ensure original behavior is maintained. DO NOT invoke stored Closures.
  157. if (is_object($args[1]) && method_exists($args[1], '__invoke')) {
  158. $this->data->set($args[0], $this->data->protect($args[1]));
  159. } else {
  160. $this->data->set($args[0], $args[1]);
  161. }
  162. } else {
  163. throw new \InvalidArgumentException('Cannot set View data with provided arguments. Usage: `View::setData( $key, $value );` or `View::setData([ key => value, ... ]);`');
  164. }
  165. }
  166. /**
  167. * DEPRECATION WARNING! This method will be removed in the next major point release
  168. *
  169. * Append data to view
  170. * @param array $data
  171. */
  172. public function appendData($data)
  173. {
  174. if (!is_array($data)) {
  175. throw new \InvalidArgumentException('Cannot append view data. Expected array argument.');
  176. }
  177. $this->data->replace($data);
  178. }
  179. /********************************************************************************
  180. * Resolve template paths
  181. *******************************************************************************/
  182. /**
  183. * Set the base directory that contains view templates
  184. * @param string $directory
  185. * @throws \InvalidArgumentException If directory is not a directory
  186. */
  187. public function setTemplatesDirectory($directory)
  188. {
  189. $this->templatesDirectory = rtrim($directory, DIRECTORY_SEPARATOR);
  190. }
  191. /**
  192. * Get templates base directory
  193. * @return string
  194. */
  195. public function getTemplatesDirectory()
  196. {
  197. return $this->templatesDirectory;
  198. }
  199. /**
  200. * Get fully qualified path to template file using templates base directory
  201. * @param string $file The template file pathname relative to templates base directory
  202. * @return string
  203. */
  204. public function getTemplatePathname($file)
  205. {
  206. return $this->templatesDirectory . DIRECTORY_SEPARATOR . ltrim($file, DIRECTORY_SEPARATOR);
  207. }
  208. /********************************************************************************
  209. * Rendering
  210. *******************************************************************************/
  211. /**
  212. * Display template
  213. *
  214. * This method echoes the rendered template to the current output buffer
  215. *
  216. * @param string $template Pathname of template file relative to templates directory
  217. * @param array $data Any additonal data to be passed to the template.
  218. */
  219. public function display($template, $data = null)
  220. {
  221. echo $this->fetch($template, $data);
  222. }
  223. /**
  224. * Return the contents of a rendered template file
  225. *
  226. * @param string $template The template pathname, relative to the template base directory
  227. * @param array $data Any additonal data to be passed to the template.
  228. * @return string The rendered template
  229. */
  230. public function fetch($template, $data = null)
  231. {
  232. return $this->render($template, $data);
  233. }
  234. /**
  235. * Render a template file
  236. *
  237. * NOTE: This method should be overridden by custom view subclasses
  238. *
  239. * @param string $template The template pathname, relative to the template base directory
  240. * @param array $data Any additonal data to be passed to the template.
  241. * @return string The rendered template
  242. * @throws \RuntimeException If resolved template pathname is not a valid file
  243. */
  244. protected function render($template, $data = null)
  245. {
  246. $templatePathname = $this->getTemplatePathname($template);
  247. if (!is_file($templatePathname)) {
  248. throw new \RuntimeException("View cannot render `$template` because the template does not exist");
  249. }
  250. $data = array_merge($this->data->all(), (array) $data);
  251. extract($data);
  252. ob_start();
  253. require $templatePathname;
  254. return ob_get_clean();
  255. }
  256. }