view.php 8.4 KB

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