view.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. * Create a new view instance.
  35. *
  36. * <code>
  37. * // Create a new view instance
  38. * $view = new View('home.index');
  39. *
  40. * // Create a new view instance of a bundle's view
  41. * $view = new View('admin::home.index');
  42. *
  43. * // Create a new view instance with bound data
  44. * $view = new View('home.index', array('name' => 'Taylor'));
  45. * </code>
  46. *
  47. * @param string $view
  48. * @param array $data
  49. * @return void
  50. */
  51. public function __construct($view, $data = array())
  52. {
  53. $this->view = $view;
  54. $this->data = $data;
  55. $this->path = $this->path($view);
  56. // If a session driver has been specified, we will bind an instance of the
  57. // validation error message container to every view. If an errors instance
  58. // exists in the session, we will use that instance.
  59. //
  60. // This makes error display in the view extremely convenient, since the
  61. // developer can always assume they have a message container instance
  62. // available to them in the view.
  63. if ( ! isset($this->data['errors']))
  64. {
  65. if (Session::started() and Session::has('errors'))
  66. {
  67. $this->data['errors'] = Session::get('errors');
  68. }
  69. else
  70. {
  71. $this->data['errors'] = new Messages;
  72. }
  73. }
  74. }
  75. /**
  76. * Get the path to a given view on disk.
  77. *
  78. * @param string $view
  79. * @return string
  80. */
  81. protected function path($view)
  82. {
  83. $view = str_replace('.', '/', $view);
  84. $root = Bundle::path(Bundle::name($view)).'views/';
  85. // Views may have the normal PHP extension or the Blade PHP extension, so
  86. // we need to check if either of them exist in the base views directory
  87. // for the bundle. We'll check for the PHP extension first since that
  88. // is probably the more common of the two.
  89. foreach (array(EXT, BLADE_EXT) as $extension)
  90. {
  91. if (file_exists($path = $root.Bundle::element($view).$extension))
  92. {
  93. return $path;
  94. }
  95. }
  96. throw new \Exception("View [$view] does not exist.");
  97. }
  98. /**
  99. * Create a new view instance.
  100. *
  101. * <code>
  102. * // Create a new view instance
  103. * $view = View::make('home.index');
  104. *
  105. * // Create a new view instance of a bundle's view
  106. * $view = View::make('admin::home.index');
  107. *
  108. * // Create a new view instance with bound data
  109. * $view = View::make('home.index', array('name' => 'Taylor'));
  110. * </code>
  111. *
  112. * @param string $view
  113. * @param array $data
  114. * @return View
  115. */
  116. public static function make($view, $data = array())
  117. {
  118. return new static($view, $data);
  119. }
  120. /**
  121. * Create a new view instance of a named view.
  122. *
  123. * <code>
  124. * // Create a new named view instance
  125. * $view = View::of('profile');
  126. *
  127. * // Create a new named view instance with bound data
  128. * $view = View::of('profile', array('name' => 'Taylor'));
  129. * </code>
  130. *
  131. * @param string $name
  132. * @param array $data
  133. * @return View
  134. */
  135. public static function of($name, $data = array())
  136. {
  137. return new static(static::$names[$name], $data);
  138. }
  139. /**
  140. * Assign a name to a view.
  141. *
  142. * <code>
  143. * // Assign a name to a view
  144. * View::name('partials.profile', 'profile');
  145. *
  146. * // Resolve an instance of a named view
  147. * $view = View::of('profile');
  148. * </code>
  149. *
  150. * @param string $view
  151. * @param string $name
  152. * @return void
  153. */
  154. public static function name($view, $name)
  155. {
  156. static::$names[$name] = $view;
  157. }
  158. /**
  159. * Register a view composer with the Event class.
  160. *
  161. * <code>
  162. * // Register a composer for the "home.index" view
  163. * View::composer('home.index', function($view)
  164. * {
  165. * $view['title'] = 'Home';
  166. * });
  167. * </code>
  168. *
  169. * @param string $view
  170. * @param Closure
  171. * @return void
  172. */
  173. public static function composer($view, $composer)
  174. {
  175. Event::listen("composing: {$view}", $composer);
  176. }
  177. /**
  178. * Get the evaluated string content of the view.
  179. *
  180. * @return string
  181. */
  182. public function render()
  183. {
  184. // To allow bundles or other pieces of the application to modify the
  185. // view before it is rendered, we will fire an event, passing in the
  186. // view instance so it can modified by any of the listeners.
  187. Event::fire("composing: {$this->view}", array($this));
  188. $data = $this->data();
  189. ob_start() and extract($data, EXTR_SKIP);
  190. // If the view is Bladed, we need to check the view for changes and
  191. // get the path to the compiled view file. Otherwise, we'll just
  192. // use the regular path to the view.
  193. //
  194. // Also, if the Blade view has expired or doesn't exist it will be
  195. // re-compiled and placed in the view storage directory. The Blade
  196. // views are re-compiled each time the original view is changed.
  197. if (strpos($this->path, BLADE_EXT) !== false)
  198. {
  199. $this->path = $this->compile();
  200. }
  201. try {include $this->path;} catch(\Exception $e) {ob_get_clean(); throw $e;}
  202. return ob_get_clean();
  203. }
  204. /**
  205. * Get the array of view data for the view instance.
  206. *
  207. * The shared view data will be combined with the view data for the instance.
  208. *
  209. * @return array
  210. */
  211. protected function data()
  212. {
  213. $data = array_merge($this->data, static::$shared);
  214. // All nested views and responses are evaluated before the main view.
  215. // This allows the assets used by nested views to be added to the
  216. // asset container before the main view is evaluated and dumps
  217. // the links to the assets into the HTML.
  218. foreach ($data as &$value)
  219. {
  220. if ($value instanceof View or $value instanceof Response)
  221. {
  222. $value = $value->render();
  223. }
  224. }
  225. return $data;
  226. }
  227. /**
  228. * Get the path to the compiled version of the Blade view.
  229. *
  230. * @return string
  231. */
  232. protected function compile()
  233. {
  234. // Compiled views are stored in the storage directory using the MD5
  235. // hash of their path. This allows us to easily store the views in
  236. // the directory without worrying about re-creating the entire
  237. // application view directory structure.
  238. $compiled = path('storage').'views/'.md5($this->path);
  239. // The view will only be re-compiled if the view has been modified
  240. // since the last compiled version of the view was created or no
  241. // compiled view exists. Otherwise, the path will be returned
  242. // without re-compiling the view.
  243. if ( ! file_exists($compiled) or (filemtime($this->path) > filemtime($compiled)))
  244. {
  245. file_put_contents($compiled, Blade::compile($this->path));
  246. }
  247. return $compiled;
  248. }
  249. /**
  250. * Add a view instance to the view data.
  251. *
  252. * <code>
  253. * // Add a view instance to a view's data
  254. * $view = View::make('foo')->nest('footer', 'partials.footer');
  255. *
  256. * // Equivalent functionality using the "with" method
  257. * $view = View::make('foo')->with('footer', View::make('partials.footer'));
  258. * </code>
  259. *
  260. * @param string $key
  261. * @param string $view
  262. * @param array $data
  263. * @return View
  264. */
  265. public function nest($key, $view, $data = array())
  266. {
  267. return $this->with($key, static::make($view, $data));
  268. }
  269. /**
  270. * Add a key / value pair to the view data.
  271. *
  272. * Bound data will be available to the view as variables.
  273. *
  274. * @param string $key
  275. * @param mixed $value
  276. * @return View
  277. */
  278. public function with($key, $value)
  279. {
  280. $this->data[$key] = $value;
  281. return $this;
  282. }
  283. /**
  284. * Add a key / value pair to the shared view data.
  285. *
  286. * Shared view data is accessible to every view created by the application.
  287. *
  288. * @param string $key
  289. * @param mixed $value
  290. * @return void
  291. */
  292. public static function share($key, $value)
  293. {
  294. static::$shared[$key] = $value;
  295. }
  296. /**
  297. * Implementation of the ArrayAccess offsetExists method.
  298. */
  299. public function offsetExists($offset)
  300. {
  301. return array_key_exists($offset, $this->data);
  302. }
  303. /**
  304. * Implementation of the ArrayAccess offsetGet method.
  305. */
  306. public function offsetGet($offset)
  307. {
  308. if (isset($this[$offset])) return $this->data[$offset];
  309. }
  310. /**
  311. * Implementation of the ArrayAccess offsetSet method.
  312. */
  313. public function offsetSet($offset, $value)
  314. {
  315. $this->data[$offset] = $value;
  316. }
  317. /**
  318. * Implementation of the ArrayAccess offsetUnset method.
  319. */
  320. public function offsetUnset($offset)
  321. {
  322. unset($this->data[$offset]);
  323. }
  324. /**
  325. * Magic Method for handling dynamic data access.
  326. */
  327. public function __get($key)
  328. {
  329. return $this->data[$key];
  330. }
  331. /**
  332. * Magic Method for handling the dynamic setting of data.
  333. */
  334. public function __set($key, $value)
  335. {
  336. $this->data[$key] = $value;
  337. }
  338. /**
  339. * Magic Method for checking dynamically-set data.
  340. */
  341. public function __isset($key)
  342. {
  343. return isset($this->data[$key]);
  344. }
  345. /**
  346. * Get the evaluated string content of the view.
  347. *
  348. * @return string
  349. */
  350. public function __toString()
  351. {
  352. return $this->render();
  353. }
  354. }