view.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php namespace Laravel;
  2. /**
  3. * The view composer class is responsible for calling the composer on a view and
  4. * searching through the view composers for a given view name. It is injected
  5. * into the View_Factory and View instances themselves, and is managed as a singleton
  6. * by the application IoC container.
  7. */
  8. class View_Composer {
  9. /**
  10. * The view composers.
  11. *
  12. * @var array
  13. */
  14. protected $composers;
  15. /**
  16. * Create a new view composer instance.
  17. *
  18. * @param array $composers
  19. * @return void
  20. */
  21. public function __construct($composers)
  22. {
  23. $this->composers = $composers;
  24. }
  25. /**
  26. * Find the key for a view by name.
  27. *
  28. * @param string $name
  29. * @return string
  30. */
  31. public function name($name)
  32. {
  33. foreach ($this->composers as $key => $value)
  34. {
  35. if ($name === $value or (isset($value['name']) and $name === $value['name'])) { return $key; }
  36. }
  37. }
  38. /**
  39. * Call the composer for the view instance.
  40. *
  41. * @param View $view
  42. * @return void
  43. */
  44. public function compose(View $view)
  45. {
  46. if (isset($this->composers[$view->view]))
  47. {
  48. foreach ((array) $this->composers[$view->view] as $key => $value)
  49. {
  50. if ($value instanceof \Closure) return call_user_func($value, $view);
  51. }
  52. }
  53. }
  54. }
  55. /**
  56. * The view factory class is responsible for the instantiation of Views. It is typically
  57. * access through the application instance from a route or controller, and is managed
  58. * as a singleton by the application IoC container.
  59. */
  60. class View_Factory {
  61. /**
  62. * The directory containing the views.
  63. *
  64. * @var string
  65. */
  66. protected $path;
  67. /**
  68. * The view composer instance.
  69. *
  70. * @var View_Composer
  71. */
  72. protected $composer;
  73. /**
  74. * Create a new view factory instance.
  75. *
  76. * @param array $composers
  77. * @param string $path
  78. * @return void
  79. */
  80. public function __construct($path, View_Composer $composer)
  81. {
  82. $this->path = $path;
  83. $this->composer = $composer;
  84. }
  85. /**
  86. * Create a new view instance.
  87. *
  88. * @param string $view
  89. * @param array $data
  90. * @return View
  91. */
  92. public function make($view, $data = array())
  93. {
  94. return new View($view, $data, $this->path($view), $this->composer, $this);
  95. }
  96. /**
  97. * Create a new view instance from a view name.
  98. *
  99. * @param string $name
  100. * @param array $data
  101. * @return View
  102. */
  103. protected function of($name, $data = array())
  104. {
  105. if ( ! is_null($view = $this->composer->name($name)))
  106. {
  107. return new View($view, $data, $this->path($view), $this->composer, $this);
  108. }
  109. throw new \Exception("Named view [$name] is not defined.");
  110. }
  111. /**
  112. * Get the path to a given view on disk.
  113. *
  114. * @param string $view
  115. * @return string
  116. */
  117. protected function path($view)
  118. {
  119. return $this->path.str_replace('.', '/', $view).EXT;
  120. }
  121. /**
  122. * Magic Method for handling the dynamic creation of named views.
  123. */
  124. public function __call($method, $parameters)
  125. {
  126. if (strpos($method, 'of_') === 0)
  127. {
  128. return $this->of(substr($method, 3), Arr::get($parameters, 0, array()));
  129. }
  130. }
  131. }
  132. /**
  133. * The view class is returned by the View Factory "make" method, and is the primary
  134. * class for working with individual views. It provides methods for binding data to
  135. * views as well as evaluating and rendering their contents.
  136. */
  137. class View {
  138. /**
  139. * The name of the view.
  140. *
  141. * @var string
  142. */
  143. public $view;
  144. /**
  145. * The view data.
  146. *
  147. * @var array
  148. */
  149. public $data;
  150. /**
  151. * The path to the view on disk.
  152. *
  153. * @var string
  154. */
  155. protected $path;
  156. /**
  157. * The view composer instance.
  158. *
  159. * @var View_Composer
  160. */
  161. protected $composer;
  162. /**
  163. * The view factory instance, which is used to create sub-views.
  164. *
  165. * @var View_Factory
  166. */
  167. protected $factory;
  168. /**
  169. * Create a new view instance.
  170. *
  171. * @param string $view
  172. * @param array $data
  173. * @param string $path
  174. * @param View_Composer $composer
  175. * @param View_Factory $factory
  176. * @return void
  177. */
  178. public function __construct($view, $data, $path, View_Composer $composer, View_Factory $factory)
  179. {
  180. $this->view = $view;
  181. $this->data = $data;
  182. $this->path = $path;
  183. $this->factory = $factory;
  184. $this->composer = $composer;
  185. if ( ! file_exists($this->path))
  186. {
  187. throw new \Exception('View ['.$this->path.'] does not exist.');
  188. }
  189. }
  190. /**
  191. * Get the evaluated string content of the view.
  192. *
  193. * If the view has a composer, it will be executed. All sub-views and responses will
  194. * also be evaluated and converted to their string values.
  195. *
  196. * @return string
  197. */
  198. public function render()
  199. {
  200. $this->composer->compose($this);
  201. foreach ($this->data as &$data)
  202. {
  203. if ($data instanceof View or $data instanceof Response) $data = $data->render();
  204. }
  205. ob_start() and extract($this->data, EXTR_SKIP);
  206. try { include $this->path; } catch (\Exception $e) { ob_get_clean(); throw $e; }
  207. return ob_get_clean();
  208. }
  209. /**
  210. * Add a view instance to the view data.
  211. *
  212. * @param string $key
  213. * @param string $view
  214. * @param array $data
  215. * @return View
  216. */
  217. public function partial($key, $view, $data = array())
  218. {
  219. return $this->with($key, $this->factory->make($view, $data));
  220. }
  221. /**
  222. * Add a key / value pair to the view data.
  223. *
  224. * Bound data will be available to the view as variables.
  225. *
  226. * @param string $key
  227. * @param mixed $value
  228. * @return View
  229. */
  230. public function with($key, $value)
  231. {
  232. $this->data[$key] = $value;
  233. return $this;
  234. }
  235. /**
  236. * Magic Method for getting items from the view data.
  237. */
  238. public function __get($key)
  239. {
  240. return $this->data[$key];
  241. }
  242. /**
  243. * Magic Method for setting items in the view data.
  244. */
  245. public function __set($key, $value)
  246. {
  247. $this->with($key, $value);
  248. }
  249. /**
  250. * Magic Method for determining if an item is in the view data.
  251. */
  252. public function __isset($key)
  253. {
  254. return array_key_exists($key, $this->data);
  255. }
  256. /**
  257. * Magic Method for removing an item from the view data.
  258. */
  259. public function __unset($key)
  260. {
  261. unset($this->data[$key]);
  262. }
  263. }