view.php 9.2 KB

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