view.php 7.1 KB

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