View.php 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. <?php
  2. namespace Illuminate\View;
  3. use Exception;
  4. use Throwable;
  5. use ArrayAccess;
  6. use BadMethodCallException;
  7. use Illuminate\Support\Str;
  8. use Illuminate\Support\MessageBag;
  9. use Illuminate\Contracts\View\Engine;
  10. use Illuminate\Support\Traits\Macroable;
  11. use Illuminate\Contracts\Support\Arrayable;
  12. use Illuminate\Contracts\Support\Renderable;
  13. use Illuminate\Contracts\Support\MessageProvider;
  14. use Illuminate\Contracts\View\View as ViewContract;
  15. class View implements ArrayAccess, ViewContract
  16. {
  17. use Macroable {
  18. __call as macroCall;
  19. }
  20. /**
  21. * The view factory instance.
  22. *
  23. * @var \Illuminate\View\Factory
  24. */
  25. protected $factory;
  26. /**
  27. * The engine implementation.
  28. *
  29. * @var \Illuminate\Contracts\View\Engine
  30. */
  31. protected $engine;
  32. /**
  33. * The name of the view.
  34. *
  35. * @var string
  36. */
  37. protected $view;
  38. /**
  39. * The array of view data.
  40. *
  41. * @var array
  42. */
  43. protected $data;
  44. /**
  45. * The path to the view file.
  46. *
  47. * @var string
  48. */
  49. protected $path;
  50. /**
  51. * Create a new view instance.
  52. *
  53. * @param \Illuminate\View\Factory $factory
  54. * @param \Illuminate\Contracts\View\Engine $engine
  55. * @param string $view
  56. * @param string $path
  57. * @param mixed $data
  58. * @return void
  59. */
  60. public function __construct(Factory $factory, Engine $engine, $view, $path, $data = [])
  61. {
  62. $this->view = $view;
  63. $this->path = $path;
  64. $this->engine = $engine;
  65. $this->factory = $factory;
  66. $this->data = $data instanceof Arrayable ? $data->toArray() : (array) $data;
  67. }
  68. /**
  69. * Get the string contents of the view.
  70. *
  71. * @param callable|null $callback
  72. * @return string
  73. *
  74. * @throws \Throwable
  75. */
  76. public function render(callable $callback = null)
  77. {
  78. try {
  79. $contents = $this->renderContents();
  80. $response = isset($callback) ? call_user_func($callback, $this, $contents) : null;
  81. // Once we have the contents of the view, we will flush the sections if we are
  82. // done rendering all views so that there is nothing left hanging over when
  83. // another view gets rendered in the future by the application developer.
  84. $this->factory->flushStateIfDoneRendering();
  85. return ! is_null($response) ? $response : $contents;
  86. } catch (Exception $e) {
  87. $this->factory->flushState();
  88. throw $e;
  89. } catch (Throwable $e) {
  90. $this->factory->flushState();
  91. throw $e;
  92. }
  93. }
  94. /**
  95. * Get the contents of the view instance.
  96. *
  97. * @return string
  98. */
  99. protected function renderContents()
  100. {
  101. // We will keep track of the amount of views being rendered so we can flush
  102. // the section after the complete rendering operation is done. This will
  103. // clear out the sections for any separate views that may be rendered.
  104. $this->factory->incrementRender();
  105. $this->factory->callComposer($this);
  106. $contents = $this->getContents();
  107. // Once we've finished rendering the view, we'll decrement the render count
  108. // so that each sections get flushed out next time a view is created and
  109. // no old sections are staying around in the memory of an environment.
  110. $this->factory->decrementRender();
  111. return $contents;
  112. }
  113. /**
  114. * Get the evaluated contents of the view.
  115. *
  116. * @return string
  117. */
  118. protected function getContents()
  119. {
  120. return $this->engine->get($this->path, $this->gatherData());
  121. }
  122. /**
  123. * Get the data bound to the view instance.
  124. *
  125. * @return array
  126. */
  127. protected function gatherData()
  128. {
  129. $data = array_merge($this->factory->getShared(), $this->data);
  130. foreach ($data as $key => $value) {
  131. if ($value instanceof Renderable) {
  132. $data[$key] = $value->render();
  133. }
  134. }
  135. return $data;
  136. }
  137. /**
  138. * Get the sections of the rendered view.
  139. *
  140. * @return string
  141. */
  142. public function renderSections()
  143. {
  144. return $this->render(function () {
  145. return $this->factory->getSections();
  146. });
  147. }
  148. /**
  149. * Add a piece of data to the view.
  150. *
  151. * @param string|array $key
  152. * @param mixed $value
  153. * @return $this
  154. */
  155. public function with($key, $value = null)
  156. {
  157. if (is_array($key)) {
  158. $this->data = array_merge($this->data, $key);
  159. } else {
  160. $this->data[$key] = $value;
  161. }
  162. return $this;
  163. }
  164. /**
  165. * Add a view instance to the view data.
  166. *
  167. * @param string $key
  168. * @param string $view
  169. * @param array $data
  170. * @return $this
  171. */
  172. public function nest($key, $view, array $data = [])
  173. {
  174. return $this->with($key, $this->factory->make($view, $data));
  175. }
  176. /**
  177. * Add validation errors to the view.
  178. *
  179. * @param \Illuminate\Contracts\Support\MessageProvider|array $provider
  180. * @return $this
  181. */
  182. public function withErrors($provider)
  183. {
  184. $this->with('errors', $this->formatErrors($provider));
  185. return $this;
  186. }
  187. /**
  188. * Format the given message provider into a MessageBag.
  189. *
  190. * @param \Illuminate\Contracts\Support\MessageProvider|array $provider
  191. * @return \Illuminate\Support\MessageBag
  192. */
  193. protected function formatErrors($provider)
  194. {
  195. return $provider instanceof MessageProvider
  196. ? $provider->getMessageBag() : new MessageBag((array) $provider);
  197. }
  198. /**
  199. * Get the name of the view.
  200. *
  201. * @return string
  202. */
  203. public function name()
  204. {
  205. return $this->getName();
  206. }
  207. /**
  208. * Get the name of the view.
  209. *
  210. * @return string
  211. */
  212. public function getName()
  213. {
  214. return $this->view;
  215. }
  216. /**
  217. * Get the array of view data.
  218. *
  219. * @return array
  220. */
  221. public function getData()
  222. {
  223. return $this->data;
  224. }
  225. /**
  226. * Get the path to the view file.
  227. *
  228. * @return string
  229. */
  230. public function getPath()
  231. {
  232. return $this->path;
  233. }
  234. /**
  235. * Set the path to the view.
  236. *
  237. * @param string $path
  238. * @return void
  239. */
  240. public function setPath($path)
  241. {
  242. $this->path = $path;
  243. }
  244. /**
  245. * Get the view factory instance.
  246. *
  247. * @return \Illuminate\View\Factory
  248. */
  249. public function getFactory()
  250. {
  251. return $this->factory;
  252. }
  253. /**
  254. * Get the view's rendering engine.
  255. *
  256. * @return \Illuminate\Contracts\View\Engine
  257. */
  258. public function getEngine()
  259. {
  260. return $this->engine;
  261. }
  262. /**
  263. * Determine if a piece of data is bound.
  264. *
  265. * @param string $key
  266. * @return bool
  267. */
  268. public function offsetExists($key)
  269. {
  270. return array_key_exists($key, $this->data);
  271. }
  272. /**
  273. * Get a piece of bound data to the view.
  274. *
  275. * @param string $key
  276. * @return mixed
  277. */
  278. public function offsetGet($key)
  279. {
  280. return $this->data[$key];
  281. }
  282. /**
  283. * Set a piece of data on the view.
  284. *
  285. * @param string $key
  286. * @param mixed $value
  287. * @return void
  288. */
  289. public function offsetSet($key, $value)
  290. {
  291. $this->with($key, $value);
  292. }
  293. /**
  294. * Unset a piece of data from the view.
  295. *
  296. * @param string $key
  297. * @return void
  298. */
  299. public function offsetUnset($key)
  300. {
  301. unset($this->data[$key]);
  302. }
  303. /**
  304. * Get a piece of data from the view.
  305. *
  306. * @param string $key
  307. * @return mixed
  308. */
  309. public function &__get($key)
  310. {
  311. return $this->data[$key];
  312. }
  313. /**
  314. * Set a piece of data on the view.
  315. *
  316. * @param string $key
  317. * @param mixed $value
  318. * @return void
  319. */
  320. public function __set($key, $value)
  321. {
  322. $this->with($key, $value);
  323. }
  324. /**
  325. * Check if a piece of data is bound to the view.
  326. *
  327. * @param string $key
  328. * @return bool
  329. */
  330. public function __isset($key)
  331. {
  332. return isset($this->data[$key]);
  333. }
  334. /**
  335. * Remove a piece of bound data from the view.
  336. *
  337. * @param string $key
  338. * @return bool
  339. */
  340. public function __unset($key)
  341. {
  342. unset($this->data[$key]);
  343. }
  344. /**
  345. * Dynamically bind parameters to the view.
  346. *
  347. * @param string $method
  348. * @param array $parameters
  349. * @return \Illuminate\View\View
  350. *
  351. * @throws \BadMethodCallException
  352. */
  353. public function __call($method, $parameters)
  354. {
  355. if (static::hasMacro($method)) {
  356. return $this->macroCall($method, $parameters);
  357. }
  358. if (! Str::startsWith($method, 'with')) {
  359. throw new BadMethodCallException(sprintf(
  360. 'Method %s::%s does not exist.', static::class, $method
  361. ));
  362. }
  363. return $this->with(Str::camel(substr($method, 4)), $parameters[0]);
  364. }
  365. /**
  366. * Get the string contents of the view.
  367. *
  368. * @return string
  369. */
  370. public function __toString()
  371. {
  372. return $this->render();
  373. }
  374. }