Просмотр файла vendor/laravel/framework/src/Illuminate/Cookie/CookieJar.php

Размер файла: 6.21Kb
  1. <?php
  2.  
  3. namespace Illuminate\Cookie;
  4.  
  5. use Illuminate\Contracts\Cookie\QueueingFactory as JarContract;
  6. use Illuminate\Support\Arr;
  7. use Illuminate\Support\InteractsWithTime;
  8. use Illuminate\Support\Traits\Macroable;
  9. use Symfony\Component\HttpFoundation\Cookie;
  10.  
  11. class CookieJar implements JarContract
  12. {
  13. use InteractsWithTime, Macroable;
  14.  
  15. /**
  16. * The default path (if specified).
  17. *
  18. * @var string
  19. */
  20. protected $path = '/';
  21.  
  22. /**
  23. * The default domain (if specified).
  24. *
  25. * @var string
  26. */
  27. protected $domain;
  28.  
  29. /**
  30. * The default secure setting (defaults to null).
  31. *
  32. * @var bool|null
  33. */
  34. protected $secure;
  35.  
  36. /**
  37. * The default SameSite option (defaults to lax).
  38. *
  39. * @var string
  40. */
  41. protected $sameSite = 'lax';
  42.  
  43. /**
  44. * All of the cookies queued for sending.
  45. *
  46. * @var \Symfony\Component\HttpFoundation\Cookie[]
  47. */
  48. protected $queued = [];
  49.  
  50. /**
  51. * Create a new cookie instance.
  52. *
  53. * @param string $name
  54. * @param string $value
  55. * @param int $minutes
  56. * @param string|null $path
  57. * @param string|null $domain
  58. * @param bool|null $secure
  59. * @param bool $httpOnly
  60. * @param bool $raw
  61. * @param string|null $sameSite
  62. * @return \Symfony\Component\HttpFoundation\Cookie
  63. */
  64. public function make($name, $value, $minutes = 0, $path = null, $domain = null, $secure = null, $httpOnly = true, $raw = false, $sameSite = null)
  65. {
  66. [$path, $domain, $secure, $sameSite] = $this->getPathAndDomain($path, $domain, $secure, $sameSite);
  67.  
  68. $time = ($minutes == 0) ? 0 : $this->availableAt($minutes * 60);
  69.  
  70. return new Cookie($name, $value, $time, $path, $domain, $secure, $httpOnly, $raw, $sameSite);
  71. }
  72.  
  73. /**
  74. * Create a cookie that lasts "forever" (five years).
  75. *
  76. * @param string $name
  77. * @param string $value
  78. * @param string|null $path
  79. * @param string|null $domain
  80. * @param bool|null $secure
  81. * @param bool $httpOnly
  82. * @param bool $raw
  83. * @param string|null $sameSite
  84. * @return \Symfony\Component\HttpFoundation\Cookie
  85. */
  86. public function forever($name, $value, $path = null, $domain = null, $secure = null, $httpOnly = true, $raw = false, $sameSite = null)
  87. {
  88. return $this->make($name, $value, 2628000, $path, $domain, $secure, $httpOnly, $raw, $sameSite);
  89. }
  90.  
  91. /**
  92. * Expire the given cookie.
  93. *
  94. * @param string $name
  95. * @param string|null $path
  96. * @param string|null $domain
  97. * @return \Symfony\Component\HttpFoundation\Cookie
  98. */
  99. public function forget($name, $path = null, $domain = null)
  100. {
  101. return $this->make($name, null, -2628000, $path, $domain);
  102. }
  103.  
  104. /**
  105. * Determine if a cookie has been queued.
  106. *
  107. * @param string $key
  108. * @param string|null $path
  109. * @return bool
  110. */
  111. public function hasQueued($key, $path = null)
  112. {
  113. return ! is_null($this->queued($key, null, $path));
  114. }
  115.  
  116. /**
  117. * Get a queued cookie instance.
  118. *
  119. * @param string $key
  120. * @param mixed $default
  121. * @param string|null $path
  122. * @return \Symfony\Component\HttpFoundation\Cookie|null
  123. */
  124. public function queued($key, $default = null, $path = null)
  125. {
  126. $queued = Arr::get($this->queued, $key, $default);
  127.  
  128. if ($path === null) {
  129. return Arr::last($queued, null, $default);
  130. }
  131.  
  132. return Arr::get($queued, $path, $default);
  133. }
  134.  
  135. /**
  136. * Queue a cookie to send with the next response.
  137. *
  138. * @param array $parameters
  139. * @return void
  140. */
  141. public function queue(...$parameters)
  142. {
  143. if (isset($parameters[0]) && $parameters[0] instanceof Cookie) {
  144. $cookie = $parameters[0];
  145. } else {
  146. $cookie = $this->make(...array_values($parameters));
  147. }
  148.  
  149. if (! isset($this->queued[$cookie->getName()])) {
  150. $this->queued[$cookie->getName()] = [];
  151. }
  152.  
  153. $this->queued[$cookie->getName()][$cookie->getPath()] = $cookie;
  154. }
  155.  
  156. /**
  157. * Queue a cookie to expire with the next response.
  158. *
  159. * @param string $name
  160. * @param string|null $path
  161. * @param string|null $domain
  162. * @return void
  163. */
  164. public function expire($name, $path = null, $domain = null)
  165. {
  166. $this->queue($this->forget($name, $path, $domain));
  167. }
  168.  
  169. /**
  170. * Remove a cookie from the queue.
  171. *
  172. * @param string $name
  173. * @param string|null $path
  174. * @return void
  175. */
  176. public function unqueue($name, $path = null)
  177. {
  178. if ($path === null) {
  179. unset($this->queued[$name]);
  180.  
  181. return;
  182. }
  183.  
  184. unset($this->queued[$name][$path]);
  185.  
  186. if (empty($this->queued[$name])) {
  187. unset($this->queued[$name]);
  188. }
  189. }
  190.  
  191. /**
  192. * Get the path and domain, or the default values.
  193. *
  194. * @param string $path
  195. * @param string $domain
  196. * @param bool|null $secure
  197. * @param string|null $sameSite
  198. * @return array
  199. */
  200. protected function getPathAndDomain($path, $domain, $secure = null, $sameSite = null)
  201. {
  202. return [$path ?: $this->path, $domain ?: $this->domain, is_bool($secure) ? $secure : $this->secure, $sameSite ?: $this->sameSite];
  203. }
  204.  
  205. /**
  206. * Set the default path and domain for the jar.
  207. *
  208. * @param string $path
  209. * @param string $domain
  210. * @param bool $secure
  211. * @param string|null $sameSite
  212. * @return $this
  213. */
  214. public function setDefaultPathAndDomain($path, $domain, $secure = false, $sameSite = null)
  215. {
  216. [$this->path, $this->domain, $this->secure, $this->sameSite] = [$path, $domain, $secure, $sameSite];
  217.  
  218. return $this;
  219. }
  220.  
  221. /**
  222. * Get the cookies which have been queued for the next request.
  223. *
  224. * @return \Symfony\Component\HttpFoundation\Cookie[]
  225. */
  226. public function getQueuedCookies()
  227. {
  228. return Arr::flatten($this->queued);
  229. }
  230.  
  231. /**
  232. * Flush the cookies which have been queued for the next request.
  233. *
  234. * @return $this
  235. */
  236. public function flushQueuedCookies()
  237. {
  238. $this->queued = [];
  239.  
  240. return $this;
  241. }
  242. }