PageRenderTime 26ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/Slim/View.php

https://gitlab.com/albertkeba/service
PHP | 282 lines | 101 code | 25 blank | 156 comment | 10 complexity | f36f854a594ff088ab3229e665ca3533 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.4.2
  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. } else {
  143. return $this->data->all();
  144. }
  145. }
  146. /**
  147. * DEPRECATION WARNING! This method will be removed in the next major point release
  148. *
  149. * Set data for view
  150. */
  151. public function setData()
  152. {
  153. $args = func_get_args();
  154. if (count($args) === 1 && is_array($args[0])) {
  155. $this->data->replace($args[0]);
  156. } elseif (count($args) === 2) {
  157. // Ensure original behavior is maintained. DO NOT invoke stored Closures.
  158. if (is_object($args[1]) && method_exists($args[1], '__invoke')) {
  159. $this->data->set($args[0], $this->data->protect($args[1]));
  160. } else {
  161. $this->data->set($args[0], $args[1]);
  162. }
  163. } else {
  164. throw new \InvalidArgumentException('Cannot set View data with provided arguments. Usage: `View::setData( $key, $value );` or `View::setData([ key => value, ... ]);`');
  165. }
  166. }
  167. /**
  168. * DEPRECATION WARNING! This method will be removed in the next major point release
  169. *
  170. * Append data to view
  171. * @param array $data
  172. */
  173. public function appendData($data)
  174. {
  175. if (!is_array($data)) {
  176. throw new \InvalidArgumentException('Cannot append view data. Expected array argument.');
  177. }
  178. $this->data->replace($data);
  179. }
  180. /********************************************************************************
  181. * Resolve template paths
  182. *******************************************************************************/
  183. /**
  184. * Set the base directory that contains view templates
  185. * @param string $directory
  186. * @throws \InvalidArgumentException If directory is not a directory
  187. */
  188. public function setTemplatesDirectory($directory)
  189. {
  190. $this->templatesDirectory = rtrim($directory, DIRECTORY_SEPARATOR);
  191. }
  192. /**
  193. * Get templates base directory
  194. * @return string
  195. */
  196. public function getTemplatesDirectory()
  197. {
  198. return $this->templatesDirectory;
  199. }
  200. /**
  201. * Get fully qualified path to template file using templates base directory
  202. * @param string $file The template file pathname relative to templates base directory
  203. * @return string
  204. */
  205. public function getTemplatePathname($file)
  206. {
  207. return $this->templatesDirectory . DIRECTORY_SEPARATOR . ltrim($file, DIRECTORY_SEPARATOR);
  208. }
  209. /********************************************************************************
  210. * Rendering
  211. *******************************************************************************/
  212. /**
  213. * Display template
  214. *
  215. * This method echoes the rendered template to the current output buffer
  216. *
  217. * @param string $template Pathname of template file relative to templates directory
  218. * @param array $data Any additonal data to be passed to the template.
  219. */
  220. public function display($template, $data = null)
  221. {
  222. echo $this->fetch($template, $data);
  223. }
  224. /**
  225. * Return the contents of a rendered template file
  226. *
  227. * @param string $template The template pathname, relative to the template base directory
  228. * @param array $data Any additonal data to be passed to the template.
  229. * @return string The rendered template
  230. */
  231. public function fetch($template, $data = null)
  232. {
  233. return $this->render($template, $data);
  234. }
  235. /**
  236. * Render a template file
  237. *
  238. * NOTE: This method should be overridden by custom view subclasses
  239. *
  240. * @param string $template The template pathname, relative to the template base directory
  241. * @param array $data Any additonal data to be passed to the template.
  242. * @return string The rendered template
  243. * @throws \RuntimeException If resolved template pathname is not a valid file
  244. */
  245. protected function render($template, $data = null)
  246. {
  247. $templatePathname = $this->getTemplatePathname($template);
  248. if (!is_file($templatePathname)) {
  249. throw new \RuntimeException("View cannot render `$template` because the template does not exist");
  250. }
  251. $data = array_merge($this->data->all(), (array) $data);
  252. extract($data);
  253. ob_start();
  254. require $templatePathname;
  255. return ob_get_clean();
  256. }
  257. }