view.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. * Register a new root path for a bundle.
  194. *
  195. * @param string $bundle
  196. * @param string $path
  197. * @return void
  198. */
  199. public static function search($bundle, $path)
  200. {
  201. static::$paths[$bundle][] = $path;
  202. }
  203. /**
  204. * Register a new valid view extension.
  205. *
  206. * @param string $extension
  207. * @return void
  208. */
  209. public static function extension($extension)
  210. {
  211. static::$extensions[] = $extension;
  212. static::$extensions = array_unique(static::$extensions);
  213. }
  214. /**
  215. * Get the evaluated string content of the view.
  216. *
  217. * @return string
  218. */
  219. public function render()
  220. {
  221. // To allow bundles or other pieces of the application to modify the
  222. // view before it is rendered, we'll fire an event, passing in the
  223. // view instance so it can modified.
  224. $composer = "laravel.composing: {$this->view}";
  225. Event::fire($composer, array($this));
  226. // If there are listeners to the view engine event, we'll pass them
  227. // the view so they can render it according to their needs, which
  228. // allows easy attachment of other view parsers.
  229. if (Event::listeners(static::engine))
  230. {
  231. $result = Event::first(static::engine, array($this));
  232. if ($result !== false) return $result;
  233. }
  234. return $this->get();
  235. }
  236. /**
  237. * Get the evaluated contents of the view.
  238. *
  239. * @return string
  240. */
  241. public function get()
  242. {
  243. $__data = $this->data();
  244. ob_start() and extract($__data, EXTR_SKIP);
  245. // We'll include the view contents for parsing within a catcher
  246. // so we can avoid any WSOD errors. If an exception occurs we
  247. // will throw it out to the exception handler.
  248. try
  249. {
  250. include $this->path;
  251. }
  252. // If we caught an exception, we'll silently flush the output
  253. // buffer so that no partially rendered views get thrown out
  254. // to the client and confuse the user.
  255. catch (\Exception $e)
  256. {
  257. ob_get_clean(); throw $e;
  258. }
  259. return ob_get_clean();
  260. }
  261. /**
  262. * Get the array of view data for the view instance.
  263. *
  264. * The shared view data will be combined with the view data for the instance.
  265. *
  266. * @return array
  267. */
  268. public function data()
  269. {
  270. $data = array_merge($this->data, static::$shared);
  271. // All nested views and responses are evaluated before the main view.
  272. // This allows the assets used by nested views to be added to the
  273. // asset container before the main view is evaluated.
  274. foreach ($data as &$value)
  275. {
  276. if ($value instanceof View or $value instanceof Response)
  277. {
  278. $value = $value->render();
  279. }
  280. }
  281. return $data;
  282. }
  283. /**
  284. * Add a view instance to the view data.
  285. *
  286. * <code>
  287. * // Add a view instance to a view's data
  288. * $view = View::make('foo')->nest('footer', 'partials.footer');
  289. *
  290. * // Equivalent functionality using the "with" method
  291. * $view = View::make('foo')->with('footer', View::make('partials.footer'));
  292. * </code>
  293. *
  294. * @param string $key
  295. * @param string $view
  296. * @param array $data
  297. * @return View
  298. */
  299. public function nest($key, $view, $data = array())
  300. {
  301. return $this->with($key, static::make($view, $data));
  302. }
  303. /**
  304. * Add a key / value pair to the view data.
  305. *
  306. * Bound data will be available to the view as variables.
  307. *
  308. * @param string $key
  309. * @param mixed $value
  310. * @return View
  311. */
  312. public function with($key, $value)
  313. {
  314. $this->data[$key] = $value;
  315. return $this;
  316. }
  317. /**
  318. * Add a key / value pair to the shared view data.
  319. *
  320. * Shared view data is accessible to every view created by the application.
  321. *
  322. * @param string $key
  323. * @param mixed $value
  324. * @return void
  325. */
  326. public static function share($key, $value)
  327. {
  328. static::$shared[$key] = $value;
  329. }
  330. /**
  331. * Implementation of the ArrayAccess offsetExists method.
  332. */
  333. public function offsetExists($offset)
  334. {
  335. return array_key_exists($offset, $this->data);
  336. }
  337. /**
  338. * Implementation of the ArrayAccess offsetGet method.
  339. */
  340. public function offsetGet($offset)
  341. {
  342. if (isset($this[$offset])) return $this->data[$offset];
  343. }
  344. /**
  345. * Implementation of the ArrayAccess offsetSet method.
  346. */
  347. public function offsetSet($offset, $value)
  348. {
  349. $this->data[$offset] = $value;
  350. }
  351. /**
  352. * Implementation of the ArrayAccess offsetUnset method.
  353. */
  354. public function offsetUnset($offset)
  355. {
  356. unset($this->data[$offset]);
  357. }
  358. /**
  359. * Magic Method for handling dynamic data access.
  360. */
  361. public function __get($key)
  362. {
  363. return $this->data[$key];
  364. }
  365. /**
  366. * Magic Method for handling the dynamic setting of data.
  367. */
  368. public function __set($key, $value)
  369. {
  370. $this->data[$key] = $value;
  371. }
  372. /**
  373. * Magic Method for checking dynamically-set data.
  374. */
  375. public function __isset($key)
  376. {
  377. return isset($this->data[$key]);
  378. }
  379. /**
  380. * Get the evaluated string content of the view.
  381. *
  382. * @return string
  383. */
  384. public function __toString()
  385. {
  386. return $this->render();
  387. }
  388. }