PageRenderTime 20ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/system/classes/kohana/view.php

https://gitlab.com/ken3/bluSky
PHP | 346 lines | 141 code | 31 blank | 174 comment | 9 complexity | 28557f821ca723ebcdee3834b451ea9d 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. // Display the exception message
  214. Kohana_Exception::handler($e);
  215. return '';
  216. }
  217. }
  218. /**
  219. * Sets the view filename.
  220. *
  221. * $view->set_filename($file);
  222. *
  223. * @param string $file view filename
  224. * @return View
  225. * @throws View_Exception
  226. */
  227. public function set_filename($file)
  228. {
  229. if (($path = Kohana::find_file('views', $file)) === FALSE)
  230. {
  231. throw new View_Exception('The requested view :file could not be found', array(
  232. ':file' => $file,
  233. ));
  234. }
  235. // Store the file path locally
  236. $this->_file = $path;
  237. return $this;
  238. }
  239. /**
  240. * Assigns a variable by name. Assigned values will be available as a
  241. * variable within the view file:
  242. *
  243. * // This value can be accessed as $foo within the view
  244. * $view->set('foo', 'my value');
  245. *
  246. * You can also use an array to set several values at once:
  247. *
  248. * // Create the values $food and $beverage in the view
  249. * $view->set(array('food' => 'bread', 'beverage' => 'water'));
  250. *
  251. * @param string $key variable name or an array of variables
  252. * @param mixed $value value
  253. * @return $this
  254. */
  255. public function set($key, $value = NULL)
  256. {
  257. if (is_array($key))
  258. {
  259. foreach ($key as $name => $value)
  260. {
  261. $this->_data[$name] = $value;
  262. }
  263. }
  264. else
  265. {
  266. $this->_data[$key] = $value;
  267. }
  268. return $this;
  269. }
  270. /**
  271. * Assigns a value by reference. The benefit of binding is that values can
  272. * be altered without re-setting them. It is also possible to bind variables
  273. * before they have values. Assigned values will be available as a
  274. * variable within the view file:
  275. *
  276. * // This reference can be accessed as $ref within the view
  277. * $view->bind('ref', $bar);
  278. *
  279. * @param string $key variable name
  280. * @param mixed $value referenced variable
  281. * @return $this
  282. */
  283. public function bind($key, & $value)
  284. {
  285. $this->_data[$key] =& $value;
  286. return $this;
  287. }
  288. /**
  289. * Renders the view object to a string. Global and local data are merged
  290. * and extracted to create local variables within the view file.
  291. *
  292. * $output = $view->render();
  293. *
  294. * [!!] Global variables with the same key name as local variables will be
  295. * overwritten by the local variable.
  296. *
  297. * @param string $file view filename
  298. * @return string
  299. * @throws View_Exception
  300. * @uses View::capture
  301. */
  302. public function render($file = NULL)
  303. {
  304. if ($file !== NULL)
  305. {
  306. $this->set_filename($file);
  307. }
  308. if (empty($this->_file))
  309. {
  310. throw new View_Exception('You must set the file to use within your view before rendering');
  311. }
  312. // Combine local and global data and capture the output
  313. return View::capture($this->_file, $this->_data);
  314. }
  315. } // End View