view.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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 event
  106. // so that the developer is free to override and manage the loading
  107. // of views in any way they see fit for their application.
  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. // Views may have either the default PHP fiel extension of the "Blade"
  126. // extension, so we will need to check for both in the view path
  127. // and return the first one we find for the given view.
  128. if (file_exists($path = $root.$view.EXT))
  129. {
  130. return $path;
  131. }
  132. elseif (file_exists($path = $root.$view.BLADE_EXT))
  133. {
  134. return $path;
  135. }
  136. }
  137. /**
  138. * Create a new view instance.
  139. *
  140. * <code>
  141. * // Create a new view instance
  142. * $view = View::make('home.index');
  143. *
  144. * // Create a new view instance of a bundle's view
  145. * $view = View::make('admin::home.index');
  146. *
  147. * // Create a new view instance with bound data
  148. * $view = View::make('home.index', array('name' => 'Taylor'));
  149. * </code>
  150. *
  151. * @param string $view
  152. * @param array $data
  153. * @return View
  154. */
  155. public static function make($view, $data = array())
  156. {
  157. return new static($view, $data);
  158. }
  159. /**
  160. * Create a new view instance of a named view.
  161. *
  162. * <code>
  163. * // Create a new named view instance
  164. * $view = View::of('profile');
  165. *
  166. * // Create a new named view instance with bound data
  167. * $view = View::of('profile', array('name' => 'Taylor'));
  168. * </code>
  169. *
  170. * @param string $name
  171. * @param array $data
  172. * @return View
  173. */
  174. public static function of($name, $data = array())
  175. {
  176. return new static(static::$names[$name], $data);
  177. }
  178. /**
  179. * Assign a name to a view.
  180. *
  181. * <code>
  182. * // Assign a name to a view
  183. * View::name('partials.profile', 'profile');
  184. *
  185. * // Resolve an instance of a named view
  186. * $view = View::of('profile');
  187. * </code>
  188. *
  189. * @param string $view
  190. * @param string $name
  191. * @return void
  192. */
  193. public static function name($view, $name)
  194. {
  195. static::$names[$name] = $view;
  196. }
  197. /**
  198. * Register a view composer with the Event class.
  199. *
  200. * <code>
  201. * // Register a composer for the "home.index" view
  202. * View::composer('home.index', function($view)
  203. * {
  204. * $view['title'] = 'Home';
  205. * });
  206. * </code>
  207. *
  208. * @param string $view
  209. * @param Closure $composer
  210. * @return void
  211. */
  212. public static function composer($view, $composer)
  213. {
  214. Event::listen("laravel.composing: {$view}", $composer);
  215. }
  216. /**
  217. * Register a new root path for a bundle.
  218. *
  219. * @param string $bundle
  220. * @param string $path
  221. * @return void
  222. */
  223. public static function search($bundle, $path)
  224. {
  225. static::$paths[$bundle][] = $path;
  226. }
  227. /**
  228. * Register a new valid view extension.
  229. *
  230. * @param string $extension
  231. * @return void
  232. */
  233. public static function extension($extension)
  234. {
  235. static::$extensions[] = $extension;
  236. static::$extensions = array_unique(static::$extensions);
  237. }
  238. /**
  239. * Get the evaluated string content of the view.
  240. *
  241. * @return string
  242. */
  243. public function render()
  244. {
  245. // To allow bundles or other pieces of the application to modify the
  246. // view before it is rendered, we'll fire an event, passing in the
  247. // view instance so it can modified.
  248. $composer = "laravel.composing: {$this->view}";
  249. Event::fire($composer, array($this));
  250. // If there are listeners to the view engine event, we'll pass them
  251. // the view so they can render it according to their needs, which
  252. // allows easy attachment of other view parsers.
  253. if (Event::listeners(static::engine))
  254. {
  255. $result = Event::first(static::engine, array($this));
  256. if ($result !== false) return $result;
  257. }
  258. return $this->get();
  259. }
  260. /**
  261. * Get the evaluated contents of the view.
  262. *
  263. * @return string
  264. */
  265. public function get()
  266. {
  267. $__data = $this->data();
  268. ob_start() and extract($__data, EXTR_SKIP);
  269. // We'll include the view contents for parsing within a catcher
  270. // so we can avoid any WSOD errors. If an exception occurs we
  271. // will throw it out to the exception handler.
  272. try
  273. {
  274. include $this->path;
  275. }
  276. // If we caught an exception, we'll silently flush the output
  277. // buffer so that no partially rendered views get thrown out
  278. // to the client and confuse the user.
  279. catch (\Exception $e)
  280. {
  281. ob_get_clean(); throw $e;
  282. }
  283. return ob_get_clean();
  284. }
  285. /**
  286. * Get the array of view data for the view instance.
  287. *
  288. * The shared view data will be combined with the view data for the instance.
  289. *
  290. * @return array
  291. */
  292. public function data()
  293. {
  294. $data = array_merge($this->data, static::$shared);
  295. // All nested views and responses are evaluated before the main view.
  296. // This allows the assets used by nested views to be added to the
  297. // asset container before the main view is evaluated.
  298. foreach ($data as &$value)
  299. {
  300. if ($value instanceof View or $value instanceof Response)
  301. {
  302. $value = $value->render();
  303. }
  304. }
  305. return $data;
  306. }
  307. /**
  308. * Add a view instance to the view data.
  309. *
  310. * <code>
  311. * // Add a view instance to a view's data
  312. * $view = View::make('foo')->nest('footer', 'partials.footer');
  313. *
  314. * // Equivalent functionality using the "with" method
  315. * $view = View::make('foo')->with('footer', View::make('partials.footer'));
  316. * </code>
  317. *
  318. * @param string $key
  319. * @param string $view
  320. * @param array $data
  321. * @return View
  322. */
  323. public function nest($key, $view, $data = array())
  324. {
  325. return $this->with($key, static::make($view, $data));
  326. }
  327. /**
  328. * Add a key / value pair to the view data.
  329. *
  330. * Bound data will be available to the view as variables.
  331. *
  332. * @param string $key
  333. * @param mixed $value
  334. * @return View
  335. */
  336. public function with($key, $value)
  337. {
  338. $this->data[$key] = $value;
  339. return $this;
  340. }
  341. /**
  342. * Add a key / value pair to the shared view data.
  343. *
  344. * Shared view data is accessible to every view created by the application.
  345. *
  346. * @param string $key
  347. * @param mixed $value
  348. * @return void
  349. */
  350. public static function share($key, $value)
  351. {
  352. static::$shared[$key] = $value;
  353. }
  354. /**
  355. * Implementation of the ArrayAccess offsetExists method.
  356. */
  357. public function offsetExists($offset)
  358. {
  359. return array_key_exists($offset, $this->data);
  360. }
  361. /**
  362. * Implementation of the ArrayAccess offsetGet method.
  363. */
  364. public function offsetGet($offset)
  365. {
  366. if (isset($this[$offset])) return $this->data[$offset];
  367. }
  368. /**
  369. * Implementation of the ArrayAccess offsetSet method.
  370. */
  371. public function offsetSet($offset, $value)
  372. {
  373. $this->data[$offset] = $value;
  374. }
  375. /**
  376. * Implementation of the ArrayAccess offsetUnset method.
  377. */
  378. public function offsetUnset($offset)
  379. {
  380. unset($this->data[$offset]);
  381. }
  382. /**
  383. * Magic Method for handling dynamic data access.
  384. */
  385. public function __get($key)
  386. {
  387. return $this->data[$key];
  388. }
  389. /**
  390. * Magic Method for handling the dynamic setting of data.
  391. */
  392. public function __set($key, $value)
  393. {
  394. $this->data[$key] = $value;
  395. }
  396. /**
  397. * Magic Method for checking dynamically-set data.
  398. */
  399. public function __isset($key)
  400. {
  401. return isset($this->data[$key]);
  402. }
  403. /**
  404. * Get the evaluated string content of the view.
  405. *
  406. * @return string
  407. */
  408. public function __toString()
  409. {
  410. return $this->render();
  411. }
  412. }