Skip to content
Permalink
Browse files

Fix Session to work properly in event-loop environment with multiple …

…workers
  • Loading branch information...
samdark committed Sep 11, 2019
1 parent 7a685f9 commit 4876355e6a3cf220eb281d49768aa7ac1921f792
Showing with 24 additions and 2 deletions.
  1. +14 −2 src/Session/Session.php
  2. +5 −0 src/Session/SessionInterface.php
  3. +5 −0 src/Session/SessionMiddleware.php
@@ -7,6 +7,8 @@
*/
class Session implements SessionInterface
{
private $sessionId;
private const DEFAULT_OPTIONS = [
'use_cookies' => 1,
'cookie_secure' => 1,
@@ -70,8 +72,13 @@ public function open(): void
return;
}
if ($this->sessionId !== null) {
session_id($this->sessionId);
}
try {
session_start($this->options);
$this->sessionId = session_id();
} catch (\Throwable $e) {
throw new SessionException('Failed to start session', $e->getCode(), $e);
}
@@ -84,8 +91,7 @@ public function isActive(): bool
public function getId(): ?string
{
$id = session_id();
return $id === '' ? null : $id;
return $this->sessionId === '' ? null : $this->sessionId;
}
public function regenerateId(): void
@@ -146,11 +152,17 @@ public function destroy(): void
{
if ($this->isActive()) {
session_destroy();
$this->sessionId = null;
}
}
public function getCookieParameters(): array
{
return session_get_cookie_params();
}
public function setId(string $sessionId): void
{
$this->sessionId = $sessionId;
}
}
@@ -41,6 +41,11 @@ public function isActive(): bool;
*/
public function getId(): ?string;
/**
* @param string $sessionId set session ID
*/
public function setId(string $sessionId): void;
/**
* Regenerate session ID keeping data
*/
@@ -18,6 +18,11 @@ public function __construct(SessionInterface $session)
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$requestSessionId = $this->getSidFromRequest($request);
if ($requestSessionId !== null) {
$this->session->setId($requestSessionId);
}
try {
$response = $handler->handle($request);
} catch (\Throwable $e) {

0 comments on commit 4876355

Please sign in to comment.
You can’t perform that action at this time.