view.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php namespace System;
  2. class View {
  3. /**
  4. * The name of the view.
  5. *
  6. * @var string
  7. */
  8. public $view;
  9. /**
  10. * The view data.
  11. *
  12. * @var array
  13. */
  14. public $data = array();
  15. /**
  16. * The module that contains the view.
  17. *
  18. * @var string
  19. */
  20. public $module;
  21. /**
  22. * The path to the view.
  23. *
  24. * @var string
  25. */
  26. public $path;
  27. /**
  28. * The defined view composers.
  29. *
  30. * @var array
  31. */
  32. private static $composers;
  33. /**
  34. * The defined view names.
  35. *
  36. * @var array
  37. */
  38. private static $names;
  39. /**
  40. * Create a new view instance.
  41. *
  42. * @param string $view
  43. * @param array $data
  44. * @return void
  45. */
  46. public function __construct($view, $data = array())
  47. {
  48. $this->data = $data;
  49. list($this->module, $this->path, $this->view) = static::parse($view);
  50. $this->compose();
  51. }
  52. /**
  53. * Create a new view instance.
  54. *
  55. * @param string $view
  56. * @param array $data
  57. * @return View
  58. */
  59. public static function make($view, $data = array())
  60. {
  61. return new static($view, $data);
  62. }
  63. /**
  64. * Create a new view instance from a view name.
  65. *
  66. * @param string $name
  67. * @param array $data
  68. * @return View
  69. */
  70. private static function of($name, $data = array())
  71. {
  72. // Search the active module first, then the application module, then all other modules.
  73. $modules = array_unique(array_merge(array(ACTIVE_MODULE, 'application'), Config::get('application.modules')));
  74. foreach ($modules as $module)
  75. {
  76. static::load_composers($module);
  77. if ( ! is_null($view = static::find_view_for_name($name, static::$composers[$module])))
  78. {
  79. return new static($view, $data);
  80. }
  81. }
  82. throw new \Exception("Named view [$name] is not defined.");
  83. }
  84. /**
  85. * Find the view for a given name in an array of composers.
  86. *
  87. * @param string $name
  88. * @param array $composers
  89. * @return string
  90. */
  91. private static function find_view_for_name($name, $composers)
  92. {
  93. foreach ($composers as $key => $value)
  94. {
  95. if (is_string($value) and $value == $name)
  96. {
  97. return $key;
  98. }
  99. elseif (is_array($value) and isset($value['name']) and $value['name'] == $name)
  100. {
  101. return $key;
  102. }
  103. }
  104. }
  105. /**
  106. * Parse a view identifier and get the module, path, and view name.
  107. *
  108. * @param string $view
  109. * @return array
  110. */
  111. private static function parse($view)
  112. {
  113. // Check for a module qualifier. If a module name is present, we need to extract it from
  114. // the view name, otherwise, we will use "application" as the module.
  115. $module = (strpos($view, '::') !== false) ? substr($view, 0, strpos($view, ':')) : 'application';
  116. $path = ($module == 'application') ? VIEW_PATH : MODULE_PATH.$module.'/views/';
  117. if ($module != 'application')
  118. {
  119. $view = substr($view, strpos($view, ':') + 2);
  120. }
  121. return array($module, $path, $view);
  122. }
  123. /**
  124. * Call the composer for the view instance.
  125. *
  126. * @return void
  127. */
  128. private function compose()
  129. {
  130. static::load_composers($this->module);
  131. if (isset(static::$composers[$this->module][$this->view]))
  132. {
  133. $composer = static::$composers[$this->module][$this->view];
  134. if ( ! is_null($composer = $this->find_composer_handler($composer)))
  135. {
  136. call_user_func($composer, $this);
  137. }
  138. }
  139. }
  140. /**
  141. * Find the composer handler / function in a composer definition.
  142. *
  143. * If the composer value itself is callable, it will be returned, otherwise the
  144. * first callable value in the composer array will be returned.
  145. *
  146. * @param mixed $composer
  147. * @return Closure
  148. */
  149. private function find_composer_handler($composer)
  150. {
  151. if (is_string($composer)) return;
  152. if (is_callable($composer)) return $composer;
  153. foreach ($composer as $value)
  154. {
  155. if (is_callable($value)) return $value;
  156. }
  157. }
  158. /**
  159. * Load the view composers for a given module.
  160. *
  161. * @param string $module
  162. * @return void
  163. */
  164. private static function load_composers($module)
  165. {
  166. if (isset(static::$composers[$module])) return;
  167. $composers = ($module == 'application') ? APP_PATH.'composers'.EXT : MODULE_PATH.$module.'/composers'.EXT;
  168. static::$composers[$module] = (file_exists($composers)) ? require $composers : array();
  169. }
  170. /**
  171. * Get the parsed content of the view.
  172. *
  173. * @return string
  174. */
  175. public function get()
  176. {
  177. $view = str_replace('.', '/', $this->view);
  178. if ( ! file_exists($this->path.$view.EXT))
  179. {
  180. throw new \Exception("View [$view] does not exist.");
  181. }
  182. $this->get_sub_views();
  183. extract($this->data, EXTR_SKIP);
  184. ob_start();
  185. try { include $this->path.$view.EXT; } catch (\Exception $e) { Error::handle($e); }
  186. return ob_get_clean();
  187. }
  188. /**
  189. * Evaluate all of the view and response instances that are bound to the view.
  190. *
  191. * @return void
  192. */
  193. private function get_sub_views()
  194. {
  195. foreach ($this->data as &$data)
  196. {
  197. if ($data instanceof View or $data instanceof Response)
  198. {
  199. $data = (string) $data;
  200. }
  201. }
  202. }
  203. /**
  204. * Add a view instance to the view data.
  205. *
  206. * @param string $key
  207. * @param string $view
  208. * @param array $data
  209. * @return View
  210. */
  211. public function partial($key, $view, $data = array())
  212. {
  213. return $this->bind($key, new static($view, $data));
  214. }
  215. /**
  216. * Add a key / value pair to the view data.
  217. *
  218. * @param string $key
  219. * @param mixed $value
  220. * @return View
  221. */
  222. public function bind($key, $value)
  223. {
  224. $this->data[$key] = $value;
  225. return $this;
  226. }
  227. /**
  228. * Magic Method for handling the dynamic creation of named views.
  229. */
  230. public static function __callStatic($method, $parameters)
  231. {
  232. if (strpos($method, 'of_') === 0)
  233. {
  234. return static::of(substr($method, 3), Arr::get($parameters, 0, array()));
  235. }
  236. }
  237. /**
  238. * Magic Method for getting items from the view data.
  239. */
  240. public function __get($key)
  241. {
  242. return $this->data[$key];
  243. }
  244. /**
  245. * Magic Method for setting items in the view data.
  246. */
  247. public function __set($key, $value)
  248. {
  249. $this->bind($key, $value);
  250. }
  251. /**
  252. * Magic Method for determining if an item is in the view data.
  253. */
  254. public function __isset($key)
  255. {
  256. return array_key_exists($key, $this->data);
  257. }
  258. /**
  259. * Magic Method for removing an item from the view data.
  260. */
  261. public function __unset($key)
  262. {
  263. unset($this->data[$key]);
  264. }
  265. /**
  266. * Get the parsed content of the View.
  267. */
  268. public function __toString()
  269. {
  270. return $this->get();
  271. }
  272. }