Skip to content

Commit 7a43bb5

Browse files
Improved the session middleware
1 parent 63b68ca commit 7a43bb5

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

src/Middlewares/SessionMiddleware.php

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,33 +58,36 @@ public function process(Request $request, RequestHandler $handler): ResponseInte
5858
{
5959
if ($request instanceof HttpRequest) {
6060
if (!$request->getRequest()->hasSession(true)) {
61-
// This variable prevents calling `$this->getSession()` twice in case the Request (and the below factory) is cloned
62-
$sess = null;
63-
$request->getRequest()->setSessionFactory(function () use (&$sess, $request) {
64-
if (!$sess) {
65-
$sess = ($this->session)();
61+
// This variable prevents calling `$this->session` twice in case the Request (and the below factory) is cloned
62+
$session = null;
63+
$request->getRequest()->setSessionFactory(function () use (&$session, $request) {
64+
if (!$session) {
65+
$session = ($this->session)();
6666
}
6767

6868
/*
6969
* For supporting sessions in php runtime with runners like roadrunner or swoole the session
7070
* cookie need read from the cookie bag and set on the session storage.
7171
*/
72-
if ($sess && !$sess->isStarted()) {
73-
$sessionId = $request->getCookieParams()[$sess->getName()] ?? '';
74-
$sess->setId($sessionId);
72+
if (!$session->isStarted() && $sessionId = ($request->getCookieParams()[$session->getName()] ?? '')) {
73+
$session->setId($sessionId);
7574
}
7675

77-
return $sess;
76+
return $session;
7877
});
78+
} else {
79+
$session = $request->getRequest()->getSession();
7980
}
80-
81-
$session = $request->getRequest()->getSession();
8281
} elseif (null === $session = $request->getAttribute(Session::class)) {
8382
$request = $request->withAttribute(static::ATTRIBUTE, $session = ($this->session)());
8483
}
8584

8685
$response = $handler->handle($request);
8786

87+
if (null === $session) {
88+
$session = $request instanceof HttpRequest ? $request->getRequest()->getSession() : $request->getAttribute(Session::class);
89+
}
90+
8891
if ($session->isStarted()) {
8992
/*
9093
* Saves the session, in case it is still open, before sending the response/headers.
@@ -128,24 +131,19 @@ public function process(Request $request, RequestHandler $handler): ResponseInte
128131
SessionUtils::popSessionCookie($sessionName, $sessionId);
129132
$requestSessionCookieId = $request->getCookieParams()[$sessionName] ?? null;
130133

131-
if ($requestSessionCookieId && ($session instanceof Session ? $session->isEmpty() : empty($session->all()))) {
134+
if ($requestSessionCookieId && $session->isEmpty()) {
132135
$cookie = new Cookie(
133136
$sessionName,
134137
null,
135138
1,
136-
$requestSessionCookieId,
137139
$sessionCookiePath,
138140
$sessionCookieDomain,
139141
$sessionCookieSecure,
140142
$sessionCookieHttpOnly,
143+
false,
141144
$sessionCookieSameSite
142145
);
143-
144-
if ($response instanceof Response) {
145-
$response = $response->withCookie($cookie);
146-
} else {
147-
$response = $response->withAddedHeader('Set-Cookie', (string) $cookie);
148-
}
146+
$response = $response instanceof Response ? $response->withCookie($cookie) : $response->withAddedHeader('Set-Cookie', (string) $cookie);
149147
} elseif ($sessionId !== $requestSessionCookieId) {
150148
$expire = 0;
151149
$lifetime = $sessionOptions['cookie_lifetime'] ?? null;

0 commit comments

Comments
 (0)