PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/system/classes/Kohana/View.php

https://gitlab.com/lincoln.coutinho/ApplicationKohanaFramework
PHP | 351 lines | 141 code | 31 blank | 179 comment | 9 complexity | d2a119d7279578f2c83c3060420dc032 MD5 | raw file
  1. <?php defined('SYSPATH') OR die('No direct script access.');
  2. /**
  3. * Acts as an object wrapper for HTML pages with embedded PHP, called "views".
  4. * Variables can be assigned with the view object and referenced locally within
  5. * the view.
  6. *
  7. * @package Kohana
  8. * @category Base
  9. * @author Kohana Team
  10. * @copyright (c) 2008-2012 Kohana Team
  11. * @license http://kohanaframework.org/license
  12. */
  13. class Kohana_View {
  14. // Array of global variables
  15. protected static $_global_data = array();
  16. /**
  17. * Returns a new View object. If you do not define the "file" parameter,
  18. * you must call [View::set_filename].
  19. *
  20. * $view = View::factory($file);
  21. *
  22. * @param string $file view filename
  23. * @param array $data array of values
  24. * @return View
  25. */
  26. public static function factory($file = NULL, array $data = NULL)
  27. {
  28. return new View($file, $data);
  29. }
  30. /**
  31. * Captures the output that is generated when a view is included.
  32. * The view data will be extracted to make local variables. This method
  33. * is static to prevent object scope resolution.
  34. *
  35. * $output = View::capture($file, $data);
  36. *
  37. * @param string $kohana_view_filename filename
  38. * @param array $kohana_view_data variables
  39. * @return string
  40. */
  41. protected static function capture($kohana_view_filename, array $kohana_view_data)
  42. {
  43. // Import the view variables to local namespace
  44. extract($kohana_view_data, EXTR_SKIP);
  45. if (View::$_global_data)
  46. {
  47. // Import the global view variables to local namespace
  48. extract(View::$_global_data, EXTR_SKIP | EXTR_REFS);
  49. }
  50. // Capture the view output
  51. ob_start();
  52. try
  53. {
  54. // Load the view within the current scope
  55. include $kohana_view_filename;
  56. }
  57. catch (Exception $e)
  58. {
  59. // Delete the output buffer
  60. ob_end_clean();
  61. // Re-throw the exception
  62. throw $e;
  63. }
  64. // Get the captured output and close the buffer
  65. return ob_get_clean();
  66. }
  67. /**
  68. * Sets a global variable, similar to [View::set], except that the
  69. * variable will be accessible to all views.
  70. *
  71. * View::set_global($name, $value);
  72. *
  73. * @param string $key variable name or an array of variables
  74. * @param mixed $value value
  75. * @return void
  76. */
  77. public static function set_global($key, $value = NULL)
  78. {
  79. if (is_array($key))
  80. {
  81. foreach ($key as $key2 => $value)
  82. {
  83. View::$_global_data[$key2] = $value;
  84. }
  85. }
  86. else
  87. {
  88. View::$_global_data[$key] = $value;
  89. }
  90. }
  91. /**
  92. * Assigns a global variable by reference, similar to [View::bind], except
  93. * that the variable will be accessible to all views.
  94. *
  95. * View::bind_global($key, $value);
  96. *
  97. * @param string $key variable name
  98. * @param mixed $value referenced variable
  99. * @return void
  100. */
  101. public static function bind_global($key, & $value)
  102. {
  103. View::$_global_data[$key] =& $value;
  104. }
  105. // View filename
  106. protected $_file;
  107. // Array of local variables
  108. protected $_data = array();
  109. /**
  110. * Sets the initial view filename and local data. Views should almost
  111. * always only be created using [View::factory].
  112. *
  113. * $view = new View($file);
  114. *
  115. * @param string $file view filename
  116. * @param array $data array of values
  117. * @return void
  118. * @uses View::set_filename
  119. */
  120. public function __construct($file = NULL, array $data = NULL)
  121. {
  122. if ($file !== NULL)
  123. {
  124. $this->set_filename($file);
  125. }
  126. if ($data !== NULL)
  127. {
  128. // Add the values to the current data
  129. $this->_data = $data + $this->_data;
  130. }
  131. }
  132. /**
  133. * Magic method, searches for the given variable and returns its value.
  134. * Local variables will be returned before global variables.
  135. *
  136. * $value = $view->foo;
  137. *
  138. * [!!] If the variable has not yet been set, an exception will be thrown.
  139. *
  140. * @param string $key variable name
  141. * @return mixed
  142. * @throws Kohana_Exception
  143. */
  144. public function & __get($key)
  145. {
  146. if (array_key_exists($key, $this->_data))
  147. {
  148. return $this->_data[$key];
  149. }
  150. elseif (array_key_exists($key, View::$_global_data))
  151. {
  152. return View::$_global_data[$key];
  153. }
  154. else
  155. {
  156. throw new Kohana_Exception('View variable is not set: :var',
  157. array(':var' => $key));
  158. }
  159. }
  160. /**
  161. * Magic method, calls [View::set] with the same parameters.
  162. *
  163. * $view->foo = 'something';
  164. *
  165. * @param string $key variable name
  166. * @param mixed $value value
  167. * @return void
  168. */
  169. public function __set($key, $value)
  170. {
  171. $this->set($key, $value);
  172. }
  173. /**
  174. * Magic method, determines if a variable is set.
  175. *
  176. * isset($view->foo);
  177. *
  178. * [!!] `NULL` variables are not considered to be set by [isset](http://php.net/isset).
  179. *
  180. * @param string $key variable name
  181. * @return boolean
  182. */
  183. public function __isset($key)
  184. {
  185. return (isset($this->_data[$key]) OR isset(View::$_global_data[$key]));
  186. }
  187. /**
  188. * Magic method, unsets a given variable.
  189. *
  190. * unset($view->foo);
  191. *
  192. * @param string $key variable name
  193. * @return void
  194. */
  195. public function __unset($key)
  196. {
  197. unset($this->_data[$key], View::$_global_data[$key]);
  198. }
  199. /**
  200. * Magic method, returns the output of [View::render].
  201. *
  202. * @return string
  203. * @uses View::render
  204. */
  205. public function __toString()
  206. {
  207. try
  208. {
  209. return $this->render();
  210. }
  211. catch (Exception $e)
  212. {
  213. /**
  214. * Display the exception message.
  215. *
  216. * We use this method here because it's impossible to throw an
  217. * exception from __toString().
  218. */
  219. $error_response = Kohana_Exception::_handler($e);
  220. return $error_response->body();
  221. }
  222. }
  223. /**
  224. * Sets the view filename.
  225. *
  226. * $view->set_filename($file);
  227. *
  228. * @param string $file view filename
  229. * @return View
  230. * @throws View_Exception
  231. */
  232. public function set_filename($file)
  233. {
  234. if (($path = Kohana::find_file('views', $file)) === FALSE)
  235. {
  236. throw new View_Exception('The requested view :file could not be found', array(
  237. ':file' => $file,
  238. ));
  239. }
  240. // Store the file path locally
  241. $this->_file = $path;
  242. return $this;
  243. }
  244. /**
  245. * Assigns a variable by name. Assigned values will be available as a
  246. * variable within the view file:
  247. *
  248. * // This value can be accessed as $foo within the view
  249. * $view->set('foo', 'my value');
  250. *
  251. * You can also use an array to set several values at once:
  252. *
  253. * // Create the values $food and $beverage in the view
  254. * $view->set(array('food' => 'bread', 'beverage' => 'water'));
  255. *
  256. * @param string $key variable name or an array of variables
  257. * @param mixed $value value
  258. * @return $this
  259. */
  260. public function set($key, $value = NULL)
  261. {
  262. if (is_array($key))
  263. {
  264. foreach ($key as $name => $value)
  265. {
  266. $this->_data[$name] = $value;
  267. }
  268. }
  269. else
  270. {
  271. $this->_data[$key] = $value;
  272. }
  273. return $this;
  274. }
  275. /**
  276. * Assigns a value by reference. The benefit of binding is that values can
  277. * be altered without re-setting them. It is also possible to bind variables
  278. * before they have values. Assigned values will be available as a
  279. * variable within the view file:
  280. *
  281. * // This reference can be accessed as $ref within the view
  282. * $view->bind('ref', $bar);
  283. *
  284. * @param string $key variable name
  285. * @param mixed $value referenced variable
  286. * @return $this
  287. */
  288. public function bind($key, & $value)
  289. {
  290. $this->_data[$key] =& $value;
  291. return $this;
  292. }
  293. /**
  294. * Renders the view object to a string. Global and local data are merged
  295. * and extracted to create local variables within the view file.
  296. *
  297. * $output = $view->render();
  298. *
  299. * [!!] Global variables with the same key name as local variables will be
  300. * overwritten by the local variable.
  301. *
  302. * @param string $file view filename
  303. * @return string
  304. * @throws View_Exception
  305. * @uses View::capture
  306. */
  307. public function render($file = NULL)
  308. {
  309. if ($file !== NULL)
  310. {
  311. $this->set_filename($file);
  312. }
  313. if (empty($this->_file))
  314. {
  315. throw new View_Exception('You must set the file to use within your view before rendering');
  316. }
  317. // Combine local and global data and capture the output
  318. return View::capture($this->_file, $this->_data);
  319. }
  320. }