view.php 6.4 KB

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