view.php 8.5 KB

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