PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php

https://gitlab.com/dzakiafif/cokelatklasik
PHP | 243 lines | 106 code | 34 blank | 103 comment | 12 complexity | efb3bf90fe8661db376ae70a4a9f200c MD5 | raw file
  1. <?php
  2. namespace Illuminate\Session\Middleware;
  3. use Closure;
  4. use Carbon\Carbon;
  5. use Illuminate\Http\Request;
  6. use Illuminate\Session\SessionManager;
  7. use Illuminate\Session\SessionInterface;
  8. use Symfony\Component\HttpFoundation\Cookie;
  9. use Illuminate\Session\CookieSessionHandler;
  10. use Symfony\Component\HttpFoundation\Response;
  11. class StartSession
  12. {
  13. /**
  14. * The session manager.
  15. *
  16. * @var \Illuminate\Session\SessionManager
  17. */
  18. protected $manager;
  19. /**
  20. * Indicates if the session was handled for the current request.
  21. *
  22. * @var bool
  23. */
  24. protected $sessionHandled = false;
  25. /**
  26. * Create a new session middleware.
  27. *
  28. * @param \Illuminate\Session\SessionManager $manager
  29. * @return void
  30. */
  31. public function __construct(SessionManager $manager)
  32. {
  33. $this->manager = $manager;
  34. }
  35. /**
  36. * Handle an incoming request.
  37. *
  38. * @param \Illuminate\Http\Request $request
  39. * @param \Closure $next
  40. * @return mixed
  41. */
  42. public function handle($request, Closure $next)
  43. {
  44. $this->sessionHandled = true;
  45. // If a session driver has been configured, we will need to start the session here
  46. // so that the data is ready for an application. Note that the Laravel sessions
  47. // do not make use of PHP "native" sessions in any way since they are crappy.
  48. if ($this->sessionConfigured()) {
  49. $session = $this->startSession($request);
  50. $request->setSession($session);
  51. }
  52. $response = $next($request);
  53. // Again, if the session has been configured we will need to close out the session
  54. // so that the attributes may be persisted to some storage medium. We will also
  55. // add the session identifier cookie to the application response headers now.
  56. if ($this->sessionConfigured()) {
  57. $this->storeCurrentUrl($request, $session);
  58. $this->collectGarbage($session);
  59. $this->addCookieToResponse($response, $session);
  60. }
  61. return $response;
  62. }
  63. /**
  64. * Perform any final actions for the request lifecycle.
  65. *
  66. * @param \Illuminate\Http\Request $request
  67. * @param \Symfony\Component\HttpFoundation\Response $response
  68. * @return void
  69. */
  70. public function terminate($request, $response)
  71. {
  72. if ($this->sessionHandled && $this->sessionConfigured() && !$this->usingCookieSessions()) {
  73. $this->manager->driver()->save();
  74. }
  75. }
  76. /**
  77. * Start the session for the given request.
  78. *
  79. * @param \Illuminate\Http\Request $request
  80. * @return \Illuminate\Session\SessionInterface
  81. */
  82. protected function startSession(Request $request)
  83. {
  84. with($session = $this->getSession($request))->setRequestOnHandler($request);
  85. $session->start();
  86. return $session;
  87. }
  88. /**
  89. * Get the session implementation from the manager.
  90. *
  91. * @param \Illuminate\Http\Request $request
  92. * @return \Illuminate\Session\SessionInterface
  93. */
  94. public function getSession(Request $request)
  95. {
  96. $session = $this->manager->driver();
  97. $session->setId($request->cookies->get($session->getName()));
  98. return $session;
  99. }
  100. /**
  101. * Store the current URL for the request if necessary.
  102. *
  103. * @param \Illuminate\Http\Request $request
  104. * @param \Illuminate\Session\SessionInterface $session
  105. * @return void
  106. */
  107. protected function storeCurrentUrl(Request $request, $session)
  108. {
  109. if ($request->method() === 'GET' && $request->route() && !$request->ajax()) {
  110. $session->setPreviousUrl($request->fullUrl());
  111. }
  112. }
  113. /**
  114. * Remove the garbage from the session if necessary.
  115. *
  116. * @param \Illuminate\Session\SessionInterface $session
  117. * @return void
  118. */
  119. protected function collectGarbage(SessionInterface $session)
  120. {
  121. $config = $this->manager->getSessionConfig();
  122. // Here we will see if this request hits the garbage collection lottery by hitting
  123. // the odds needed to perform garbage collection on any given request. If we do
  124. // hit it, we'll call this handler to let it delete all the expired sessions.
  125. if ($this->configHitsLottery($config)) {
  126. $session->getHandler()->gc($this->getSessionLifetimeInSeconds());
  127. }
  128. }
  129. /**
  130. * Determine if the configuration odds hit the lottery.
  131. *
  132. * @param array $config
  133. * @return bool
  134. */
  135. protected function configHitsLottery(array $config)
  136. {
  137. return mt_rand(1, $config['lottery'][1]) <= $config['lottery'][0];
  138. }
  139. /**
  140. * Add the session cookie to the application response.
  141. *
  142. * @param \Symfony\Component\HttpFoundation\Response $response
  143. * @param \Illuminate\Session\SessionInterface $session
  144. * @return void
  145. */
  146. protected function addCookieToResponse(Response $response, SessionInterface $session)
  147. {
  148. if ($this->usingCookieSessions()) {
  149. $this->manager->driver()->save();
  150. }
  151. if ($this->sessionIsPersistent($config = $this->manager->getSessionConfig())) {
  152. $response->headers->setCookie(new Cookie(
  153. $session->getName(), $session->getId(), $this->getCookieExpirationDate(),
  154. $config['path'], $config['domain'], array_get($config, 'secure', false)
  155. ));
  156. }
  157. }
  158. /**
  159. * Get the session lifetime in seconds.
  160. *
  161. * @return int
  162. */
  163. protected function getSessionLifetimeInSeconds()
  164. {
  165. return array_get($this->manager->getSessionConfig(), 'lifetime') * 60;
  166. }
  167. /**
  168. * Get the cookie lifetime in seconds.
  169. *
  170. * @return int
  171. */
  172. protected function getCookieExpirationDate()
  173. {
  174. $config = $this->manager->getSessionConfig();
  175. return $config['expire_on_close'] ? 0 : Carbon::now()->addMinutes($config['lifetime']);
  176. }
  177. /**
  178. * Determine if a session driver has been configured.
  179. *
  180. * @return bool
  181. */
  182. protected function sessionConfigured()
  183. {
  184. return !is_null(array_get($this->manager->getSessionConfig(), 'driver'));
  185. }
  186. /**
  187. * Determine if the configured session driver is persistent.
  188. *
  189. * @param array|null $config
  190. * @return bool
  191. */
  192. protected function sessionIsPersistent(array $config = null)
  193. {
  194. $config = $config ?: $this->manager->getSessionConfig();
  195. return !in_array($config['driver'], [null, 'array']);
  196. }
  197. /**
  198. * Determine if the session is using cookie sessions.
  199. *
  200. * @return bool
  201. */
  202. protected function usingCookieSessions()
  203. {
  204. if (!$this->sessionConfigured()) {
  205. return false;
  206. }
  207. return $this->manager->driver()->getHandler() instanceof CookieSessionHandler;
  208. }
  209. }