PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/category_tool/fuel/core/classes/viewmodel.php

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