view.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <?php namespace Laravel;
  2. use Closure;
  3. class View {
  4. /**
  5. * The name of the view.
  6. *
  7. * @var string
  8. */
  9. public $view;
  10. /**
  11. * The view data.
  12. *
  13. * @var array
  14. */
  15. public $data;
  16. /**
  17. * The path to the view on disk.
  18. *
  19. * @var string
  20. */
  21. protected $path;
  22. /**
  23. * All of the view composers for the application.
  24. *
  25. * @var array
  26. */
  27. protected static $composers;
  28. /**
  29. * Create a new view instance.
  30. *
  31. * @param string $view
  32. * @param array $data
  33. * @return void
  34. */
  35. public function __construct($view, $data = array())
  36. {
  37. $this->view = $view;
  38. $this->data = $data;
  39. $this->path = $this->path($view);
  40. // If a session driver has been specified, we will bind an instance of
  41. // the validation error message container to every view. If an errors
  42. // instance exists in the session, we will use that instance.
  43. //
  44. // This makes the implementation of the Post/Redirect/Get pattern very
  45. // convenient since each view can assume it has a message container.
  46. if (Config::$items['session']['driver'] !== '' and IoC::core('session')->started())
  47. {
  48. $this->data['errors'] = IoC::core('session')->get('errors', function()
  49. {
  50. return new Messages;
  51. });
  52. }
  53. }
  54. /**
  55. * Get the path to a given view on disk.
  56. *
  57. * @param string $view
  58. * @return string
  59. */
  60. protected function path($view)
  61. {
  62. $view = str_replace('.', '/', $view);
  63. // Application views take priority over system views, so we will return the
  64. // application version of the view if both exists. Also, we need to check
  65. // for Blade views using the Blade extension. The Blade extension gives
  66. // an easy method of determining if the view should be compiled using
  67. // the Blade compiler or if the view is plain PHP.
  68. foreach (array(EXT, BLADE_EXT) as $extension)
  69. {
  70. if (file_exists($path = VIEW_PATH.$view.$extension))
  71. {
  72. return $path;
  73. }
  74. elseif (file_exists($path = SYS_VIEW_PATH.$view.$extension))
  75. {
  76. return $path;
  77. }
  78. }
  79. throw new \RuntimeException("View [$view] does not exist.");
  80. }
  81. /**
  82. * Create a new view instance.
  83. *
  84. * The name of the view given to this method should correspond to a view
  85. * within your application views directory. Dots or slashes may used to
  86. * reference views within sub-directories.
  87. *
  88. * <code>
  89. * // Create a new view instance
  90. * $view = View::make('home.index');
  91. *
  92. * // Create a new view instance with bound data
  93. * $view = View::make('home.index', array('name' => 'Taylor'));
  94. * </code>
  95. *
  96. * @param string $view
  97. * @param array $data
  98. * @return View
  99. */
  100. public static function make($view, $data = array())
  101. {
  102. return new static($view, $data);
  103. }
  104. /**
  105. * Create a new view instance from a view name.
  106. *
  107. * View names are defined in the application composers file.
  108. *
  109. * <code>
  110. * // Create an instance of the "layout" named view
  111. * $view = View::of('layout');
  112. *
  113. * // Create an instance of the "layout" view with bound data
  114. * $view = View::of('layout', array('name' => 'Taylor'));
  115. * </code>
  116. *
  117. * @param string $name
  118. * @param array $data
  119. * @return View
  120. */
  121. public static function of($name, $data = array())
  122. {
  123. if ( ! is_null($view = static::name($name)))
  124. {
  125. return static::make($view, $data);
  126. }
  127. throw new \OutOfBoundsException("Named view [$name] is not defined.");
  128. }
  129. /**
  130. * Find the key for a view by name.
  131. *
  132. * The view "key" is the string that should be passed into the "make" method and
  133. * should correspond with the location of the view within the application views
  134. * directory, such as "home.index" or "home/index".
  135. *
  136. * @param string $name
  137. * @return string
  138. */
  139. protected static function name($name)
  140. {
  141. static::composers();
  142. foreach (static::$composers as $key => $value)
  143. {
  144. if ($name === $value or $name === Arr::get((array) $value, 'name'))
  145. {
  146. return $key;
  147. }
  148. }
  149. }
  150. /**
  151. * Call the composer for the view instance.
  152. *
  153. * @param View $view
  154. * @return void
  155. */
  156. protected static function compose(View $view)
  157. {
  158. static::composers();
  159. if (isset(static::$composers[$view->view]))
  160. {
  161. foreach ((array) static::$composers[$view->view] as $key => $value)
  162. {
  163. if ($value instanceof Closure) return call_user_func($value, $view);
  164. }
  165. }
  166. }
  167. /**
  168. * Load the view composers for the application.
  169. *
  170. * For better testing flexiblity, we load the composers from the IoC container.
  171. *
  172. * @return void
  173. */
  174. protected static function composers()
  175. {
  176. static::$composers = require APP_PATH.'composers'.EXT;
  177. }
  178. /**
  179. * Get the evaluated string content of the view.
  180. *
  181. * @return string
  182. */
  183. public function render()
  184. {
  185. static::compose($this);
  186. // All nested views and responses are evaluated before the main view.
  187. // This allows the assets used by the nested views to be added to the
  188. // asset container before the main view is evaluated and dumps the
  189. // links to the assets.
  190. foreach ($this->data as &$data)
  191. {
  192. if ($data instanceof View or $data instanceof Response)
  193. {
  194. $data = $data->render();
  195. }
  196. }
  197. ob_start() and extract($this->data, EXTR_SKIP);
  198. // If the view is Bladed, we need to check the view for modifications
  199. // and get the path to the compiled view file. Otherwise, we'll just
  200. // use the regular path to the view.
  201. $view = (strpos($this->path, BLADE_EXT) !== false) ? $this->compile() : $this->path;
  202. try { include $view; } catch (\Exception $e) { ob_get_clean(); throw $e; }
  203. return ob_get_clean();
  204. }
  205. /**
  206. * Compile the Bladed view and return the path to the compiled view.
  207. *
  208. * @return string
  209. */
  210. protected function compile()
  211. {
  212. // For simplicity, compiled views are stored in a single directory by
  213. // the MD5 hash of their name. This allows us to avoid recreating the
  214. // entire view directory structure within the compiled views directory.
  215. $compiled = STORAGE_PATH.'views/'.md5($this->view);
  216. // The view will only be re-compiled if the view has been modified
  217. // since the last compiled version of the view was created or no
  218. // compiled view exists. Otherwise, the path will be returned
  219. // without re-compiling.
  220. if ( ! file_exists($compiled) or (filemtime($this->path) > filemtime($compiled)))
  221. {
  222. file_put_contents($compiled, Blade::compile($this->path));
  223. }
  224. return $compiled;
  225. }
  226. /**
  227. * Add a view instance to the view data.
  228. *
  229. * <code>
  230. * // Add a view instance to a view's data
  231. * $view = View::make('foo')->nest('footer', 'partials.footer');
  232. *
  233. * // Equivalent functionality using the "with" method
  234. * $view = View::make('foo')->with('footer', View::make('partials.footer'));
  235. *
  236. * // Bind a view instance with data
  237. * $view = View::make('foo')->nest('footer', 'partials.footer', array('name' => 'Taylor'));
  238. * </code>
  239. *
  240. * @param string $key
  241. * @param string $view
  242. * @param array $data
  243. * @return View
  244. */
  245. public function nest($key, $view, $data = array())
  246. {
  247. return $this->with($key, static::make($view, $data));
  248. }
  249. /**
  250. * Add a key / value pair to the view data.
  251. *
  252. * Bound data will be available to the view as variables.
  253. *
  254. * @param string $key
  255. * @param mixed $value
  256. * @return View
  257. */
  258. public function with($key, $value)
  259. {
  260. $this->data[$key] = $value;
  261. return $this;
  262. }
  263. /**
  264. * Magic Method for getting items from the view data.
  265. */
  266. public function __get($key)
  267. {
  268. return $this->data[$key];
  269. }
  270. /**
  271. * Magic Method for setting items in the view data.
  272. */
  273. public function __set($key, $value)
  274. {
  275. $this->with($key, $value);
  276. }
  277. /**
  278. * Magic Method for determining if an item is in the view data.
  279. */
  280. public function __isset($key)
  281. {
  282. return array_key_exists($key, $this->data);
  283. }
  284. /**
  285. * Magic Method for removing an item from the view data.
  286. */
  287. public function __unset($key)
  288. {
  289. unset($this->data[$key]);
  290. }
  291. /**
  292. * Get the evaluated string content of the view.
  293. *
  294. * @return string
  295. */
  296. public function __toString()
  297. {
  298. return $this->render();
  299. }
  300. /**
  301. * Magic Method for handling the dynamic creation of named views.
  302. *
  303. * <code>
  304. * // Create an instance of the "layout" named view
  305. * $view = View::of_layout();
  306. *
  307. * // Create an instance of a named view with data
  308. * $view = View::of_layout(array('name' => 'Taylor'));
  309. * </code>
  310. */
  311. public static function __callStatic($method, $parameters)
  312. {
  313. if (strpos($method, 'of_') === 0)
  314. {
  315. return static::of(substr($method, 3), Arr::get($parameters, 0, array()));
  316. }
  317. throw new \BadMethodCallException("Method [$method] is not defined on the View class.");
  318. }
  319. }