view.php 7.9 KB

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