PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/core/classes/viewmodel.php

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