view.php 12 KB

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