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

/fuel/core/classes/viewmodel.php

https://bitbucket.org/arkross/venus
PHP | 306 lines | 130 code | 45 blank | 131 comment | 9 complexity | 4bd0934526380ba7a4180d550256410f MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. <?php
  2. /**
  3. * Part of the Fuel framework.
  4. *
  5. * @package Fuel
  6. * @version 1.0
  7. * @author Fuel Development Team
  8. * @license MIT License
  9. * @copyright 2010 - 2011 Fuel Development Team
  10. * @link http://fuelphp.com
  11. */
  12. namespace Fuel\Core;
  13. /**
  14. * ViewModel
  15. *
  16. * @package Fuel
  17. * @subpackage Core
  18. * @category Core
  19. * @author Jelmer Schreuder
  20. */
  21. abstract class ViewModel
  22. {
  23. /**
  24. * This method is deprecated...use forge() instead.
  25. *
  26. * @deprecated until 1.2
  27. */
  28. public static function factory($viewmodel, $method = 'view')
  29. {
  30. logger(\Fuel::L_WARNING, 'This method is deprecated. Please use a forge() instead.', __METHOD__);
  31. return static::forge($viewmodel, $method);
  32. }
  33. /**
  34. * Factory for fetching the ViewModel
  35. *
  36. * @param string ViewModel classname without View_ prefix or full classname
  37. * @param string Method to execute
  38. * @return ViewModel
  39. */
  40. public static function forge($viewmodel, $method = 'view', $auto_filter = null)
  41. {
  42. $namespace = \Request::active() ? ucfirst(\Request::active()->module) : '';
  43. $class = $namespace.'\\View_'.ucfirst(str_replace(array('/', DS), '_', $viewmodel));
  44. if ( ! class_exists($class))
  45. {
  46. if ( ! class_exists($class = $viewmodel))
  47. {
  48. throw new \OutOfBoundsException('ViewModel "View_'.ucfirst(str_replace(array('/', DS), '_', $viewmodel)).'" could not be found.');
  49. }
  50. }
  51. return new $class($method, $auto_filter);
  52. }
  53. /**
  54. * @var string method to execute when rendering
  55. */
  56. protected $_method;
  57. /**
  58. * @var string|View view name, after instantiation a View object
  59. * @deprecated until v1.2
  60. */
  61. protected $_template;
  62. /**
  63. * @var string|View view name, after instantiation a View object
  64. */
  65. protected $_view;
  66. /**
  67. * @var bool whether or not to use auto filtering
  68. */
  69. protected $_auto_filter;
  70. /**
  71. * @var Request active request during ViewModel creation for proper context
  72. */
  73. protected $_active_request;
  74. protected function __construct($method, $auto_filter = null)
  75. {
  76. $this->_auto_filter = $auto_filter;
  77. class_exists('Request', false) and $this->_active_request = \Request::active();
  78. // @TODO Remove in 1.2. This is for backwards compat only.
  79. empty($this->_view) and $this->_view = $this->_template;
  80. if (empty($this->_view))
  81. {
  82. // Take the class name and guess the view name
  83. $class = get_class($this);
  84. $this->_view = strtolower(str_replace('_', DS, preg_replace('#^([a-z0-9_]*\\\\)?(View_)?#i', '', $class)));
  85. }
  86. $this->set_view();
  87. // @TODO Remove in 1.2. This is for backwards compat only.
  88. if ( ! empty($this->_template))
  89. {
  90. logger(\Fuel::L_WARNING, '$this->_template is deprecated. Please use a $this->_view instead.', __METHOD__);
  91. $this->_view = $this->_template;
  92. }
  93. $this->_method = $method;
  94. $this->before();
  95. }
  96. /**
  97. * Must return a View object or something compatible
  98. *
  99. * @return Object any object on which the template vars can be set and which has a toString method
  100. * @deprecated until 1.2
  101. */
  102. protected function set_template()
  103. {
  104. logger(\Fuel::L_WARNING, 'This method is deprecated. Please use a $this->set_view() instead.', __METHOD__);
  105. return $this->set_view();
  106. }
  107. /**
  108. * Must return a View object or something compatible
  109. *
  110. * @return Object any object on which the template vars can be set and which has a toString method
  111. */
  112. protected function set_view()
  113. {
  114. $this->_view = \View::forge($this->_view);
  115. }
  116. /**
  117. * Returns the active request object.
  118. *
  119. * @return Request
  120. */
  121. protected function request()
  122. {
  123. return $this->_active_request;
  124. }
  125. /**
  126. * Executed before the view method
  127. */
  128. public function before() {}
  129. /**
  130. * The default view method
  131. * Should set all expected variables upon itself
  132. */
  133. public function view() {}
  134. /**
  135. * Executed after the view method
  136. */
  137. public function after() {}
  138. /**
  139. * Fetches an existing value from the template
  140. *
  141. * @return mixed
  142. */
  143. public function & __get($name)
  144. {
  145. return $this->get($name);
  146. }
  147. /**
  148. * Gets a variable from the template
  149. *
  150. * @param string
  151. */
  152. public function & get($key, $default = null)
  153. {
  154. if (is_null($default) and func_num_args() === 1)
  155. {
  156. return $this->_view->get($key);
  157. }
  158. return $this->_view->get($key, $default);
  159. }
  160. /**
  161. * Sets and sanitizes a variable on the template
  162. *
  163. * @param string
  164. * @param mixed
  165. */
  166. public function __set($key, $value)
  167. {
  168. return $this->set($key, $value);
  169. }
  170. /**
  171. * Sets a variable on the template
  172. *
  173. * @param string
  174. * @param mixed
  175. * @param bool|null
  176. */
  177. public function set($key, $value, $filter = null)
  178. {
  179. is_null($filter) and $filter = $this->_auto_filter;
  180. $this->_view->set($key, $value, $filter);
  181. return $this;
  182. }
  183. /**
  184. * Magic method, determines if a variable is set.
  185. *
  186. * isset($view->foo);
  187. *
  188. * @param string variable name
  189. * @return boolean
  190. */
  191. public function __isset($key)
  192. {
  193. return isset($this->_view->$key);
  194. }
  195. /**
  196. * Assigns a value by reference. The benefit of binding is that values can
  197. * be altered without re-setting them. It is also possible to bind variables
  198. * before they have values. Assigned values will be available as a
  199. * variable within the view file:
  200. *
  201. * $this->bind('ref', $bar);
  202. *
  203. * @param string variable name
  204. * @param mixed referenced variable
  205. * @param bool Whether to filter the var on output
  206. * @return $this
  207. */
  208. public function bind($key, &$value, $filter = null)
  209. {
  210. $this->_view->bind($key, $value, $filter);
  211. return $this;
  212. }
  213. /**
  214. * Change auto filter setting
  215. *
  216. * @param null|bool change setting (bool) or get the current setting (null)
  217. * @return void|bool returns current setting or nothing when it is changed
  218. */
  219. public function auto_filter($setting = null)
  220. {
  221. if (func_num_args() == 0)
  222. {
  223. return $this->_view->auto_filter();
  224. }
  225. return $this->_view->auto_filter($setting);
  226. }
  227. /**
  228. * Add variables through method and after() and create template as a string
  229. */
  230. public function render()
  231. {
  232. if (class_exists('Request', false))
  233. {
  234. $current_request = Request::active();
  235. Request::active($this->_active_request);
  236. }
  237. $this->{$this->_method}();
  238. $this->after();
  239. $return = $this->_view->render();
  240. if (class_exists('Request', false))
  241. {
  242. Request::active($current_request);
  243. }
  244. return $return;
  245. }
  246. /**
  247. * Auto-render on toString
  248. */
  249. public function __toString()
  250. {
  251. try
  252. {
  253. return $this->render();
  254. }
  255. catch (\Exception $e)
  256. {
  257. \Error::exception_handler($e);
  258. return '';
  259. }
  260. }
  261. }