view.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. * The view names for the active module will be searched first, followed by
  67. * the view names for the application directory, followed by the view names
  68. * for all other modules.
  69. *
  70. * @param string $name
  71. * @param array $data
  72. * @return View
  73. */
  74. private static function of($name, $data = array())
  75. {
  76. $modules = array_unique(array_merge(array(ACTIVE_MODULE, 'application'), Config::get('application.modules')));
  77. foreach ($modules as $module)
  78. {
  79. static::load_composers($module);
  80. foreach (static::$composers[$module] as $key => $value)
  81. {
  82. if ($name === $value or (isset($value['name']) and $name === $value['name']))
  83. {
  84. return new static($key, $data);
  85. }
  86. }
  87. }
  88. throw new \Exception("Named view [$name] is not defined.");
  89. }
  90. /**
  91. * Parse a view identifier and get the module, path, and view name.
  92. *
  93. * @param string $view
  94. * @return array
  95. */
  96. private static function parse($view)
  97. {
  98. $module = (strpos($view, '::') !== false) ? substr($view, 0, strpos($view, ':')) : 'application';
  99. $path = ($module == 'application') ? VIEW_PATH : MODULE_PATH.$module.'/views/';
  100. if ($module != 'application') $view = substr($view, strpos($view, ':') + 2);
  101. return array($module, $path, $view);
  102. }
  103. /**
  104. * Call the composer for the view instance.
  105. *
  106. * @return void
  107. */
  108. private function compose()
  109. {
  110. static::load_composers($this->module);
  111. if (isset(static::$composers[$this->module][$this->view]))
  112. {
  113. $composer = static::$composers[$this->module][$this->view];
  114. if ( ! is_null($composer = $this->find_composer_handler($composer)))
  115. {
  116. call_user_func($composer, $this);
  117. }
  118. }
  119. }
  120. /**
  121. * Find the composer handler / function in a composer definition.
  122. *
  123. * If the composer value itself is callable, it will be returned, otherwise the
  124. * first callable value in the composer array will be returned.
  125. *
  126. * @param mixed $composer
  127. * @return Closure
  128. */
  129. private function find_composer_handler($composer)
  130. {
  131. if (is_string($composer)) return;
  132. if (is_callable($composer)) return $composer;
  133. foreach ($composer as $value)
  134. {
  135. if (is_callable($value)) return $value;
  136. }
  137. }
  138. /**
  139. * Load the view composers for a given module.
  140. *
  141. * @param string $module
  142. * @return void
  143. */
  144. private static function load_composers($module)
  145. {
  146. if (isset(static::$composers[$module])) return;
  147. $composers = ($module == 'application') ? APP_PATH.'composers'.EXT : MODULE_PATH.$module.'/composers'.EXT;
  148. static::$composers[$module] = (file_exists($composers)) ? require $composers : array();
  149. }
  150. /**
  151. * Get the parsed content of the view.
  152. *
  153. * @return string
  154. */
  155. public function get()
  156. {
  157. $view = str_replace('.', '/', $this->view);
  158. if ( ! file_exists($this->path.$view.EXT))
  159. {
  160. throw new \Exception("View [$view] does not exist.");
  161. }
  162. foreach ($this->data as &$data)
  163. {
  164. if ($data instanceof View or $data instanceof Response) $data = (string) $data;
  165. }
  166. extract($this->data, EXTR_SKIP);
  167. ob_start();
  168. try { include $this->path.$view.EXT; } catch (\Exception $e) { Error::handle($e); }
  169. return ob_get_clean();
  170. }
  171. /**
  172. * Add a view instance to the view data.
  173. *
  174. * @param string $key
  175. * @param string $view
  176. * @param array $data
  177. * @return View
  178. */
  179. public function partial($key, $view, $data = array())
  180. {
  181. return $this->bind($key, new static($view, $data));
  182. }
  183. /**
  184. * Add a key / value pair to the view data.
  185. *
  186. * @param string $key
  187. * @param mixed $value
  188. * @return View
  189. */
  190. public function bind($key, $value)
  191. {
  192. $this->data[$key] = $value;
  193. return $this;
  194. }
  195. /**
  196. * Magic Method for handling the dynamic creation of named views.
  197. */
  198. public static function __callStatic($method, $parameters)
  199. {
  200. if (strpos($method, 'of_') === 0)
  201. {
  202. return static::of(substr($method, 3), Arr::get($parameters, 0, array()));
  203. }
  204. }
  205. /**
  206. * Magic Method for getting items from the view data.
  207. */
  208. public function __get($key)
  209. {
  210. return $this->data[$key];
  211. }
  212. /**
  213. * Magic Method for setting items in the view data.
  214. */
  215. public function __set($key, $value)
  216. {
  217. $this->bind($key, $value);
  218. }
  219. /**
  220. * Magic Method for determining if an item is in the view data.
  221. */
  222. public function __isset($key)
  223. {
  224. return array_key_exists($key, $this->data);
  225. }
  226. /**
  227. * Magic Method for removing an item from the view data.
  228. */
  229. public function __unset($key)
  230. {
  231. unset($this->data[$key]);
  232. }
  233. /**
  234. * Get the parsed content of the View.
  235. */
  236. public function __toString()
  237. {
  238. return $this->get();
  239. }
  240. }