view.php 13 KB

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