view.php 8.7 KB

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