/developer_helper/view.php

https://github.com/dimkalinux/punbb_extensions · PHP · 344 lines · 122 code · 29 blank · 193 comment · 8 complexity · d1a6fb8de29157e72bf1b2c847213fb7 MD5 · raw file

  1. <?php
  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-2009 Kohana Team
  11. * @license http://kohanaphp.com/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 view filename
  23. * @param array 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 filename
  38. * @param array 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 and maintain references
  48. extract(View::$_global_data, 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 variable name or an array of variables
  74. * @param mixed 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 variable name
  98. * @param mixed 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 view filename
  116. * @param array 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 variable name
  141. * @return mixed
  142. * @throws Kohana_Exception
  143. */
  144. public function & __get($key)
  145. {
  146. if (isset($this->_data[$key]))
  147. {
  148. return $this->_data[$key];
  149. }
  150. elseif (isset(View::$_global_data[$key]))
  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. message ('View variable is not set: '.$key);
  159. }
  160. }
  161. /**
  162. * Magic method, calls [View::set] with the same parameters.
  163. *
  164. * $view->foo = 'something';
  165. *
  166. * @param string variable name
  167. * @param mixed value
  168. * @return void
  169. */
  170. public function __set($key, $value)
  171. {
  172. $this->set($key, $value);
  173. }
  174. /**
  175. * Magic method, determines if a variable is set.
  176. *
  177. * isset($view->foo);
  178. *
  179. * [!!] `NULL` variables are not considered to be set by [isset](http://php.net/isset).
  180. *
  181. * @param string variable name
  182. * @return boolean
  183. */
  184. public function __isset($key)
  185. {
  186. return (isset($this->_data[$key]) OR isset(View::$_global_data[$key]));
  187. }
  188. /**
  189. * Magic method, unsets a given variable.
  190. *
  191. * unset($view->foo);
  192. *
  193. * @param string variable name
  194. * @return void
  195. */
  196. public function __unset($key)
  197. {
  198. unset($this->_data[$key], View::$_global_data[$key]);
  199. }
  200. /**
  201. * Magic method, returns the output of [View::render].
  202. *
  203. * @return string
  204. * @uses View::render
  205. */
  206. public function __toString()
  207. {
  208. // try
  209. // {
  210. return $this->render();
  211. /* }
  212. catch (Exception $e)
  213. {
  214. // Display the exception message
  215. Kohana::exception_handler($e);
  216. return '';
  217. }
  218. */
  219. }
  220. /**
  221. * Sets the view filename.
  222. *
  223. * $view->set_filename($file);
  224. *
  225. * @param string view filename
  226. * @return View
  227. * @throws Kohana_View_Exception
  228. */
  229. public function set_filename($file)
  230. {
  231. $this->_file = $file.'.php';
  232. return $this;
  233. }
  234. /**
  235. * Assigns a variable by name. Assigned values will be available as a
  236. * variable within the view file:
  237. *
  238. * // This value can be accessed as $foo within the view
  239. * $view->set('foo', 'my value');
  240. *
  241. * You can also use an array to set several values at once:
  242. *
  243. * // Create the values $food and $beverage in the view
  244. * $view->set(array('food' => 'bread', 'beverage' => 'water'));
  245. *
  246. * @param string variable name or an array of variables
  247. * @param mixed value
  248. * @return $this
  249. */
  250. public function set($key, $value = NULL)
  251. {
  252. if (is_array($key))
  253. {
  254. foreach ($key as $name => $value)
  255. {
  256. $this->_data[$name] = $value;
  257. }
  258. }
  259. else
  260. {
  261. $this->_data[$key] = $value;
  262. }
  263. return $this;
  264. }
  265. /**
  266. * Assigns a value by reference. The benefit of binding is that values can
  267. * be altered without re-setting them. It is also possible to bind variables
  268. * before they have values. Assigned values will be available as a
  269. * variable within the view file:
  270. *
  271. * // This reference can be accessed as $ref within the view
  272. * $view->bind('ref', $bar);
  273. *
  274. * @param string variable name
  275. * @param mixed referenced variable
  276. * @return $this
  277. */
  278. public function bind($key, & $value)
  279. {
  280. $this->_data[$key] =& $value;
  281. return $this;
  282. }
  283. /**
  284. * Renders the view object to a string. Global and local data are merged
  285. * and extracted to create local variables within the view file.
  286. *
  287. * $output = $view->render();
  288. *
  289. * [!!] Global variables with the same key name as local variables will be
  290. * overwritten by the local variable.
  291. *
  292. * @param string view filename
  293. * @return string
  294. * @throws Kohana_View_Exception
  295. * @uses View::capture
  296. */
  297. public function render($file = NULL)
  298. {
  299. if ($file !== NULL)
  300. {
  301. $this->set_filename($file);
  302. }
  303. if (empty($this->_file))
  304. {
  305. message ('You must set the file to use within your view before rendering');
  306. }
  307. // Combine local and global data and capture the output
  308. return View::capture($this->_file, $this->_data);
  309. }
  310. } // End View
  311. class View extends Kohana_View
  312. {
  313. public static $instance;
  314. }