view.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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 cache content of loaded view files.
  35. *
  36. * @var array
  37. */
  38. public static $cache = array();
  39. /**
  40. * The Laravel view loader event name.
  41. *
  42. * @var string
  43. */
  44. const loader = 'laravel.view.loader';
  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. // In order to allow developers to load views outside of the normal loading
  74. // conventions, we'll allow for a raw path to be given in place of the
  75. // typical view name, giving total freedom on view loading.
  76. if (starts_with($view, 'path: '))
  77. {
  78. $this->path = substr($view, 6);
  79. }
  80. else
  81. {
  82. $this->path = $this->path($view);
  83. }
  84. // If a session driver has been specified, we will bind an instance of the
  85. // validation error message container to every view. If an error instance
  86. // exists in the session, we will use that instance.
  87. if ( ! isset($this->data['errors']))
  88. {
  89. if (Session::started() and Session::has('errors'))
  90. {
  91. $this->data['errors'] = Session::get('errors');
  92. }
  93. else
  94. {
  95. $this->data['errors'] = new Messages;
  96. }
  97. }
  98. }
  99. /**
  100. * Determine if the given view exists.
  101. *
  102. * @param string $view
  103. * @param boolean $return_path
  104. * @return string|bool
  105. */
  106. public static function exists($view, $return_path = false)
  107. {
  108. list($bundle, $view) = Bundle::parse($view);
  109. $view = str_replace('.', '/', $view);
  110. // We delegate the determination of view paths to the view loader event
  111. // so that the developer is free to override and manage the loading
  112. // of views in any way they see fit for their application.
  113. $path = Event::first(static::loader, array($bundle, $view));
  114. if ( ! is_null($path))
  115. {
  116. return $return_path ? $path : true;
  117. }
  118. return false;
  119. }
  120. /**
  121. * Get the path to a given view on disk.
  122. *
  123. * @param string $view
  124. * @return string
  125. */
  126. protected function path($view)
  127. {
  128. if ($path = $this->exists($view,true))
  129. {
  130. return $path;
  131. }
  132. throw new \Exception("View [$view] doesn't exist.");
  133. }
  134. /**
  135. * Get the path to a view using the default folder convention.
  136. *
  137. * @param string $bundle
  138. * @param string $view
  139. * @param string $directory
  140. * @return string
  141. */
  142. public static function file($bundle, $view, $directory)
  143. {
  144. $directory = str_finish($directory, DS);
  145. // Views may have either the default PHP file extension of the "Blade"
  146. // extension, so we will need to check for both in the view path
  147. // and return the first one we find for the given view.
  148. if (file_exists($path = $directory.$view.EXT))
  149. {
  150. return $path;
  151. }
  152. elseif (file_exists($path = $directory.$view.BLADE_EXT))
  153. {
  154. return $path;
  155. }
  156. }
  157. /**
  158. * Create a new view instance.
  159. *
  160. * <code>
  161. * // Create a new view instance
  162. * $view = View::make('home.index');
  163. *
  164. * // Create a new view instance of a bundle's view
  165. * $view = View::make('admin::home.index');
  166. *
  167. * // Create a new view instance with bound data
  168. * $view = View::make('home.index', array('name' => 'Taylor'));
  169. * </code>
  170. *
  171. * @param string $view
  172. * @param array $data
  173. * @return View
  174. */
  175. public static function make($view, $data = array())
  176. {
  177. return new static($view, $data);
  178. }
  179. /**
  180. * Create a new view instance of a named view.
  181. *
  182. * <code>
  183. * // Create a new named view instance
  184. * $view = View::of('profile');
  185. *
  186. * // Create a new named view instance with bound data
  187. * $view = View::of('profile', array('name' => 'Taylor'));
  188. * </code>
  189. *
  190. * @param string $name
  191. * @param array $data
  192. * @return View
  193. */
  194. public static function of($name, $data = array())
  195. {
  196. return new static(static::$names[$name], $data);
  197. }
  198. /**
  199. * Assign a name to a view.
  200. *
  201. * <code>
  202. * // Assign a name to a view
  203. * View::name('partials.profile', 'profile');
  204. *
  205. * // Resolve an instance of a named view
  206. * $view = View::of('profile');
  207. * </code>
  208. *
  209. * @param string $view
  210. * @param string $name
  211. * @return void
  212. */
  213. public static function name($view, $name)
  214. {
  215. static::$names[$name] = $view;
  216. }
  217. /**
  218. * Register a view composer with the Event class.
  219. *
  220. * <code>
  221. * // Register a composer for the "home.index" view
  222. * View::composer('home.index', function($view)
  223. * {
  224. * $view['title'] = 'Home';
  225. * });
  226. * </code>
  227. *
  228. * @param string|array $view
  229. * @param Closure $composer
  230. * @return void
  231. */
  232. public static function composer($views, $composer)
  233. {
  234. $views = (array) $views;
  235. foreach ($views as $view)
  236. {
  237. Event::listen("laravel.composing: {$view}", $composer);
  238. }
  239. }
  240. /**
  241. * Get the rendered contents of a partial from a loop.
  242. *
  243. * @param string $view
  244. * @param array $data
  245. * @param string $iterator
  246. * @param string $empty
  247. * @return string
  248. */
  249. public static function render_each($view, array $data, $iterator, $empty = 'raw|')
  250. {
  251. $result = '';
  252. // If is actually data in the array, we will loop through the data and
  253. // append an instance of the partial view to the final result HTML,
  254. // passing in the iterated value of the data array.
  255. if (count($data) > 0)
  256. {
  257. foreach ($data as $key => $value)
  258. {
  259. $with = array('key' => $key, $iterator => $value);
  260. $result .= render($view, $with);
  261. }
  262. }
  263. // If there is no data in the array, we will render the contents of
  264. // the "empty" view. Alternative, the "empty view" can be a raw
  265. // string that is prefixed with "raw|" for convenience.
  266. else
  267. {
  268. if (starts_with($empty, 'raw|'))
  269. {
  270. $result = substr($empty, 4);
  271. }
  272. else
  273. {
  274. $result = render($empty);
  275. }
  276. }
  277. return $result;
  278. }
  279. /**
  280. * Get the evaluated string content of the view.
  281. *
  282. * @return string
  283. */
  284. public function render()
  285. {
  286. Event::fire("laravel.composing: {$this->view}", array($this));
  287. // If there are listeners to the view engine event, we'll pass them
  288. // the view so they can render it according to their needs, which
  289. // allows easy attachment of other view parsers.
  290. if (Event::listeners(static::engine))
  291. {
  292. $result = Event::first(static::engine, array($this));
  293. if ($result !== false) return $result;
  294. }
  295. return $this->get();
  296. }
  297. /**
  298. * Get the evaluated contents of the view.
  299. *
  300. * @return string
  301. */
  302. public function get()
  303. {
  304. $__data = $this->data();
  305. // The contents of each view file is cached in an array for the
  306. // request since partial views may be rendered inside of for
  307. // loops which could incur performance penalties.
  308. $__contents = $this->load();
  309. ob_start() and extract($__data, EXTR_SKIP);
  310. // We'll include the view contents for parsing within a catcher
  311. // so we can avoid any WSOD errors. If an exception occurs we
  312. // will throw it out to the exception handler.
  313. try
  314. {
  315. eval('?>'.$__contents);
  316. }
  317. // If we caught an exception, we'll silently flush the output
  318. // buffer so that no partially rendered views get thrown out
  319. // to the client and confuse the user with junk.
  320. catch (\Exception $e)
  321. {
  322. ob_get_clean(); throw $e;
  323. }
  324. return ob_get_clean();
  325. }
  326. /**
  327. * Get the contents of the view file from disk.
  328. *
  329. * @return string
  330. */
  331. protected function load()
  332. {
  333. if (isset(static::$cache[$this->path]))
  334. {
  335. return static::$cache[$this->path];
  336. }
  337. else
  338. {
  339. return static::$cache[$this->path] = file_get_contents($this->path);
  340. }
  341. }
  342. /**
  343. * Get the array of view data for the view instance.
  344. *
  345. * The shared view data will be combined with the view data.
  346. *
  347. * @return array
  348. */
  349. public function data()
  350. {
  351. $data = array_merge($this->data, static::$shared);
  352. // All nested views and responses are evaluated before the main view.
  353. // This allows the assets used by nested views to be added to the
  354. // asset container before the main view is evaluated.
  355. foreach ($data as $key => $value)
  356. {
  357. if ($value instanceof View or $value instanceof Response)
  358. {
  359. $data[$key] = $value->render();
  360. }
  361. }
  362. return $data;
  363. }
  364. /**
  365. * Add a view instance to the view data.
  366. *
  367. * <code>
  368. * // Add a view instance to a view's data
  369. * $view = View::make('foo')->nest('footer', 'partials.footer');
  370. *
  371. * // Equivalent functionality using the "with" method
  372. * $view = View::make('foo')->with('footer', View::make('partials.footer'));
  373. * </code>
  374. *
  375. * @param string $key
  376. * @param string $view
  377. * @param array $data
  378. * @return View
  379. */
  380. public function nest($key, $view, $data = array())
  381. {
  382. return $this->with($key, static::make($view, $data));
  383. }
  384. /**
  385. * Add a key / value pair to the view data.
  386. *
  387. * Bound data will be available to the view as variables.
  388. *
  389. * @param string $key
  390. * @param mixed $value
  391. * @return View
  392. */
  393. public function with($key, $value = null)
  394. {
  395. if (is_array($key))
  396. {
  397. $this->data = array_merge($this->data, $key);
  398. }
  399. else
  400. {
  401. $this->data[$key] = $value;
  402. }
  403. return $this;
  404. }
  405. /**
  406. * Add a key / value pair to the shared view data.
  407. *
  408. * Shared view data is accessible to every view created by the application.
  409. *
  410. * @param string $key
  411. * @param mixed $value
  412. * @return View
  413. */
  414. public function shares($key, $value)
  415. {
  416. static::share($key, $value);
  417. return $this;
  418. }
  419. /**
  420. * Add a key / value pair to the shared view data.
  421. *
  422. * Shared view data is accessible to every view created by the application.
  423. *
  424. * @param string $key
  425. * @param mixed $value
  426. * @return void
  427. */
  428. public static function share($key, $value)
  429. {
  430. static::$shared[$key] = $value;
  431. }
  432. /**
  433. * Implementation of the ArrayAccess offsetExists method.
  434. */
  435. public function offsetExists($offset)
  436. {
  437. return array_key_exists($offset, $this->data);
  438. }
  439. /**
  440. * Implementation of the ArrayAccess offsetGet method.
  441. */
  442. public function offsetGet($offset)
  443. {
  444. if (isset($this[$offset])) return $this->data[$offset];
  445. }
  446. /**
  447. * Implementation of the ArrayAccess offsetSet method.
  448. */
  449. public function offsetSet($offset, $value)
  450. {
  451. $this->data[$offset] = $value;
  452. }
  453. /**
  454. * Implementation of the ArrayAccess offsetUnset method.
  455. */
  456. public function offsetUnset($offset)
  457. {
  458. unset($this->data[$offset]);
  459. }
  460. /**
  461. * Magic Method for handling dynamic data access.
  462. */
  463. public function __get($key)
  464. {
  465. return $this->data[$key];
  466. }
  467. /**
  468. * Magic Method for handling the dynamic setting of data.
  469. */
  470. public function __set($key, $value)
  471. {
  472. $this->data[$key] = $value;
  473. }
  474. /**
  475. * Magic Method for checking dynamically-set data.
  476. */
  477. public function __isset($key)
  478. {
  479. return isset($this->data[$key]);
  480. }
  481. /**
  482. * Get the evaluated string content of the view.
  483. *
  484. * @return string
  485. */
  486. public function __toString()
  487. {
  488. return $this->render();
  489. }
  490. }