PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Http/RedirectResponse.php

https://gitlab.com/ealexis.t/trends
PHP | 200 lines | 88 code | 27 blank | 85 comment | 3 complexity | bb50238125b9f7a91a440d70c27431e5 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Http;
  3. use BadMethodCallException;
  4. use Illuminate\Support\Str;
  5. use Illuminate\Support\MessageBag;
  6. use Illuminate\Support\ViewErrorBag;
  7. use Illuminate\Session\Store as SessionStore;
  8. use Illuminate\Contracts\Support\MessageProvider;
  9. use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile;
  10. use Symfony\Component\HttpFoundation\RedirectResponse as BaseRedirectResponse;
  11. class RedirectResponse extends BaseRedirectResponse
  12. {
  13. use ResponseTrait;
  14. /**
  15. * The request instance.
  16. *
  17. * @var \Illuminate\Http\Request
  18. */
  19. protected $request;
  20. /**
  21. * The session store implementation.
  22. *
  23. * @var \Illuminate\Session\Store
  24. */
  25. protected $session;
  26. /**
  27. * Flash a piece of data to the session.
  28. *
  29. * @param string|array $key
  30. * @param mixed $value
  31. * @return \Illuminate\Http\RedirectResponse
  32. */
  33. public function with($key, $value = null)
  34. {
  35. $key = is_array($key) ? $key : [$key => $value];
  36. foreach ($key as $k => $v) {
  37. $this->session->flash($k, $v);
  38. }
  39. return $this;
  40. }
  41. /**
  42. * Add multiple cookies to the response.
  43. *
  44. * @param array $cookies
  45. * @return $this
  46. */
  47. public function withCookies(array $cookies)
  48. {
  49. foreach ($cookies as $cookie) {
  50. $this->headers->setCookie($cookie);
  51. }
  52. return $this;
  53. }
  54. /**
  55. * Flash an array of input to the session.
  56. *
  57. * @param array $input
  58. * @return $this
  59. */
  60. public function withInput(array $input = null)
  61. {
  62. $input = $input ?: $this->request->input();
  63. $this->session->flashInput($data = array_filter($input, $callback = function (&$value) use (&$callback) {
  64. if (is_array($value)) {
  65. $value = array_filter($value, $callback);
  66. }
  67. return ! $value instanceof SymfonyUploadedFile;
  68. }));
  69. return $this;
  70. }
  71. /**
  72. * Flash an array of input to the session.
  73. *
  74. * @param mixed string
  75. * @return $this
  76. */
  77. public function onlyInput()
  78. {
  79. return $this->withInput($this->request->only(func_get_args()));
  80. }
  81. /**
  82. * Flash an array of input to the session.
  83. *
  84. * @param mixed string
  85. * @return \Illuminate\Http\RedirectResponse
  86. */
  87. public function exceptInput()
  88. {
  89. return $this->withInput($this->request->except(func_get_args()));
  90. }
  91. /**
  92. * Flash a container of errors to the session.
  93. *
  94. * @param \Illuminate\Contracts\Support\MessageProvider|array|string $provider
  95. * @param string $key
  96. * @return $this
  97. */
  98. public function withErrors($provider, $key = 'default')
  99. {
  100. $value = $this->parseErrors($provider);
  101. $this->session->flash(
  102. 'errors', $this->session->get('errors', new ViewErrorBag)->put($key, $value)
  103. );
  104. return $this;
  105. }
  106. /**
  107. * Parse the given errors into an appropriate value.
  108. *
  109. * @param \Illuminate\Contracts\Support\MessageProvider|array|string $provider
  110. * @return \Illuminate\Support\MessageBag
  111. */
  112. protected function parseErrors($provider)
  113. {
  114. if ($provider instanceof MessageProvider) {
  115. return $provider->getMessageBag();
  116. }
  117. return new MessageBag((array) $provider);
  118. }
  119. /**
  120. * Get the request instance.
  121. *
  122. * @return \Illuminate\Http\Request|null
  123. */
  124. public function getRequest()
  125. {
  126. return $this->request;
  127. }
  128. /**
  129. * Set the request instance.
  130. *
  131. * @param \Illuminate\Http\Request $request
  132. * @return void
  133. */
  134. public function setRequest(Request $request)
  135. {
  136. $this->request = $request;
  137. }
  138. /**
  139. * Get the session store implementation.
  140. *
  141. * @return \Illuminate\Session\Store|null
  142. */
  143. public function getSession()
  144. {
  145. return $this->session;
  146. }
  147. /**
  148. * Set the session store implementation.
  149. *
  150. * @param \Illuminate\Session\Store $session
  151. * @return void
  152. */
  153. public function setSession(SessionStore $session)
  154. {
  155. $this->session = $session;
  156. }
  157. /**
  158. * Dynamically bind flash data in the session.
  159. *
  160. * @param string $method
  161. * @param array $parameters
  162. * @return $this
  163. *
  164. * @throws \BadMethodCallException
  165. */
  166. public function __call($method, $parameters)
  167. {
  168. if (Str::startsWith($method, 'with')) {
  169. return $this->with(Str::snake(substr($method, 4)), $parameters[0]);
  170. }
  171. throw new BadMethodCallException("Method [$method] does not exist on Redirect.");
  172. }
  173. }