view.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php namespace Laravel; use Closure, ArrayAccess;
  2. class View implements ArrayAccess {
  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;
  15. /**
  16. * The path to the view on disk.
  17. *
  18. * @var string
  19. */
  20. public $path;
  21. /**
  22. * All of the shared view data.
  23. *
  24. * @var array
  25. */
  26. public static $shared = array();
  27. /**
  28. * All of the registered view names.
  29. *
  30. * @var array
  31. */
  32. public static $names = array();
  33. /**
  34. * The Laravel view engine event name.
  35. *
  36. * @var string
  37. */
  38. const engine = 'laravel.view.engine';
  39. /**
  40. * Create a new view instance.
  41. *
  42. * <code>
  43. * // Create a new view instance
  44. * $view = new View('home.index');
  45. *
  46. * // Create a new view instance of a bundle's view
  47. * $view = new View('admin::home.index');
  48. *
  49. * // Create a new view instance with bound data
  50. * $view = new View('home.index', array('name' => 'Taylor'));
  51. * </code>
  52. *
  53. * @param string $view
  54. * @param array $data
  55. * @return void
  56. */
  57. public function __construct($view, $data = array())
  58. {
  59. $this->view = $view;
  60. $this->data = $data;
  61. $this->path = $this->path($view);
  62. // If a session driver has been specified, we will bind an instance of the
  63. // validation error message container to every view. If an error instance
  64. // exists in the session, we will use that instance.
  65. if ( ! isset($this->data['errors']))
  66. {
  67. if (Session::started() and Session::has('errors'))
  68. {
  69. $this->data['errors'] = Session::get('errors');
  70. }
  71. else
  72. {
  73. $this->data['errors'] = new Messages;
  74. }
  75. }
  76. }
  77. /**
  78. * Get the path to a given view on disk.
  79. *
  80. * @param string $view
  81. * @return string
  82. */
  83. protected function path($view)
  84. {
  85. $view = str_replace('.', '/', $view);
  86. $root = Bundle::path(Bundle::name($view)).'views/';
  87. // We need to make sure that the view exists. If it doesn't, we will
  88. // throw an exception since there is not any point in going further.
  89. // If it does, we can just return the full view path.
  90. $path = $root.Bundle::element($view).EXT;
  91. if (file_exists($path))
  92. {
  93. return $path;
  94. }
  95. throw new \Exception("View [$view] doesn't exist.");
  96. }
  97. /**
  98. * Create a new view instance.
  99. *
  100. * <code>
  101. * // Create a new view instance
  102. * $view = View::make('home.index');
  103. *
  104. * // Create a new view instance of a bundle's view
  105. * $view = View::make('admin::home.index');
  106. *
  107. * // Create a new view instance with bound data
  108. * $view = View::make('home.index', array('name' => 'Taylor'));
  109. * </code>
  110. *
  111. * @param string $view
  112. * @param array $data
  113. * @return View
  114. */
  115. public static function make($view, $data = array())
  116. {
  117. return new static($view, $data);
  118. }
  119. /**
  120. * Create a new view instance of a named view.
  121. *
  122. * <code>
  123. * // Create a new named view instance
  124. * $view = View::of('profile');
  125. *
  126. * // Create a new named view instance with bound data
  127. * $view = View::of('profile', array('name' => 'Taylor'));
  128. * </code>
  129. *
  130. * @param string $name
  131. * @param array $data
  132. * @return View
  133. */
  134. public static function of($name, $data = array())
  135. {
  136. return new static(static::$names[$name], $data);
  137. }
  138. /**
  139. * Assign a name to a view.
  140. *
  141. * <code>
  142. * // Assign a name to a view
  143. * View::name('partials.profile', 'profile');
  144. *
  145. * // Resolve an instance of a named view
  146. * $view = View::of('profile');
  147. * </code>
  148. *
  149. * @param string $view
  150. * @param string $name
  151. * @return void
  152. */
  153. public static function name($view, $name)
  154. {
  155. static::$names[$name] = $view;
  156. }
  157. /**
  158. * Register a view composer with the Event class.
  159. *
  160. * <code>
  161. * // Register a composer for the "home.index" view
  162. * View::composer('home.index', function($view)
  163. * {
  164. * $view['title'] = 'Home';
  165. * });
  166. * </code>
  167. *
  168. * @param string $view
  169. * @param Closure $composer
  170. * @return void
  171. */
  172. public static function composer($view, $composer)
  173. {
  174. Event::listen("laravel.composing: {$view}", $composer);
  175. }
  176. /**
  177. * Get the evaluated string content of the view.
  178. *
  179. * @return string
  180. */
  181. public function render()
  182. {
  183. // To allow bundles or other pieces of the application to modify the
  184. // view before it is rendered, we'll fire an event, passing in the
  185. // view instance so it can modified.
  186. $composer = "laravel.composing: {$this->view}";
  187. Event::fire($composer, array($this));
  188. // If there are listeners to the view engine event, we'll pass them
  189. // the view so they can render it according to their needs, which
  190. // allows easy attachment of other view parsers.
  191. if (Event::listeners(static::engine))
  192. {
  193. return Event::first(static::engine, array($this));
  194. }
  195. else
  196. {
  197. return $this->get();
  198. }
  199. }
  200. /**
  201. * Get the evaluated contents of the view.
  202. *
  203. * @return string
  204. */
  205. public function get()
  206. {
  207. $__data = $this->data();
  208. ob_start() and extract($__data, EXTR_SKIP);
  209. // We'll include the view contents for parsing within a catcher
  210. // so we can avoid any WSOD errors. If an exception occurs we
  211. // will throw it out to the exception handler.
  212. try
  213. {
  214. include $this->path;
  215. }
  216. // If we caught an exception, we'll silently flush the output
  217. // buffer so that no partially rendered views get thrown out
  218. // to the client and confuse the user.
  219. catch (\Exception $e)
  220. {
  221. ob_get_clean(); throw $e;
  222. }
  223. return ob_get_clean();
  224. }
  225. /**
  226. * Get the array of view data for the view instance.
  227. *
  228. * The shared view data will be combined with the view data for the instance.
  229. *
  230. * @return array
  231. */
  232. public function data()
  233. {
  234. $data = array_merge($this->data, static::$shared);
  235. // All nested views and responses are evaluated before the main view.
  236. // This allows the assets used by nested views to be added to the
  237. // asset container before the main view is evaluated.
  238. foreach ($data as &$value)
  239. {
  240. if ($value instanceof View or $value instanceof Response)
  241. {
  242. $value = $value->render();
  243. }
  244. }
  245. return $data;
  246. }
  247. /**
  248. * Add a view instance to the view data.
  249. *
  250. * <code>
  251. * // Add a view instance to a view's data
  252. * $view = View::make('foo')->nest('footer', 'partials.footer');
  253. *
  254. * // Equivalent functionality using the "with" method
  255. * $view = View::make('foo')->with('footer', View::make('partials.footer'));
  256. * </code>
  257. *
  258. * @param string $key
  259. * @param string $view
  260. * @param array $data
  261. * @return View
  262. */
  263. public function nest($key, $view, $data = array())
  264. {
  265. return $this->with($key, static::make($view, $data));
  266. }
  267. /**
  268. * Add a key / value pair to the view data.
  269. *
  270. * Bound data will be available to the view as variables.
  271. *
  272. * @param string $key
  273. * @param mixed $value
  274. * @return View
  275. */
  276. public function with($key, $value)
  277. {
  278. $this->data[$key] = $value;
  279. return $this;
  280. }
  281. /**
  282. * Add a key / value pair to the shared view data.
  283. *
  284. * Shared view data is accessible to every view created by the application.
  285. *
  286. * @param string $key
  287. * @param mixed $value
  288. * @return void
  289. */
  290. public static function share($key, $value)
  291. {
  292. static::$shared[$key] = $value;
  293. }
  294. /**
  295. * Implementation of the ArrayAccess offsetExists method.
  296. */
  297. public function offsetExists($offset)
  298. {
  299. return array_key_exists($offset, $this->data);
  300. }
  301. /**
  302. * Implementation of the ArrayAccess offsetGet method.
  303. */
  304. public function offsetGet($offset)
  305. {
  306. if (isset($this[$offset])) return $this->data[$offset];
  307. }
  308. /**
  309. * Implementation of the ArrayAccess offsetSet method.
  310. */
  311. public function offsetSet($offset, $value)
  312. {
  313. $this->data[$offset] = $value;
  314. }
  315. /**
  316. * Implementation of the ArrayAccess offsetUnset method.
  317. */
  318. public function offsetUnset($offset)
  319. {
  320. unset($this->data[$offset]);
  321. }
  322. /**
  323. * Magic Method for handling dynamic data access.
  324. */
  325. public function __get($key)
  326. {
  327. return $this->data[$key];
  328. }
  329. /**
  330. * Magic Method for handling the dynamic setting of data.
  331. */
  332. public function __set($key, $value)
  333. {
  334. $this->data[$key] = $value;
  335. }
  336. /**
  337. * Magic Method for checking dynamically-set data.
  338. */
  339. public function __isset($key)
  340. {
  341. return isset($this->data[$key]);
  342. }
  343. /**
  344. * Get the evaluated string content of the view.
  345. *
  346. * @return string
  347. */
  348. public function __toString()
  349. {
  350. return $this->render();
  351. }
  352. }