Skip to content

Commit 5866523

Browse files
authored
[RoadRunner] Minor refactoring (#49)
* [RoadRunner] Minor refactoring * CS
1 parent 08db29d commit 5866523

File tree

1 file changed

+43
-38
lines changed

1 file changed

+43
-38
lines changed

src/Runner.php

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
1010
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
1111
use Symfony\Component\HttpFoundation\Cookie;
12-
use Symfony\Component\HttpFoundation\Response;
12+
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
13+
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
1314
use Symfony\Component\HttpKernel\KernelInterface;
1415
use Symfony\Component\HttpKernel\TerminableInterface;
1516
use Symfony\Component\Runtime\RunnerInterface;
@@ -50,43 +51,7 @@ public function run(): int
5051
while ($request = $worker->waitRequest()) {
5152
try {
5253
$sfRequest = $this->httpFoundationFactory->createRequest($request);
53-
54-
$sessionName = $this->sessionOptions['name'] ?? \session_name();
55-
$requestSessionId = $sfRequest->cookies->get($sessionName, '');
56-
57-
// TODO invalid session id should be expired: see F at https://github.com/php-runtime/runtime/issues/46
58-
\session_id($requestSessionId);
59-
60-
/** @var Response $sfResponse */
61-
$sfResponse = $this->kernel->handle($sfRequest);
62-
63-
if ($sfRequest->hasSession()) {
64-
$sessionId = \session_id();
65-
// we can not use $session->isStarted() here as this state is not longer available at this time
66-
// TODO session cookie should only be set when persisted by symfony: see E at https://github.com/php-runtime/runtime/issues/46
67-
if ($sessionId && $sessionId !== $requestSessionId) {
68-
$expires = 0;
69-
$lifetime = $this->sessionOptions['cookie_lifetime'] ?? null;
70-
if ($lifetime) {
71-
$expires = time() + $lifetime;
72-
}
73-
74-
$sfResponse->headers->setCookie(
75-
Cookie::create(
76-
$sessionName,
77-
$sessionId,
78-
$expires,
79-
$this->sessionOptions['cookie_path'] ?? '/',
80-
$this->sessionOptions['cookie_domain'] ?? null,
81-
$this->sessionOptions['cookie_secure'] ?? null,
82-
$this->sessionOptions['cookie_httponly'] ?? true,
83-
false,
84-
$this->sessionOptions['cookie_samesite'] ?? Cookie::SAMESITE_LAX,
85-
)
86-
);
87-
}
88-
}
89-
54+
$sfResponse = $this->handle($sfRequest);
9055
$worker->respond($this->httpMessageFactory->createResponse($sfResponse));
9156

9257
if ($this->kernel instanceof TerminableInterface) {
@@ -107,4 +72,44 @@ public function run(): int
10772

10873
return 0;
10974
}
75+
76+
private function handle(SymfonyRequest $request): SymfonyResponse
77+
{
78+
$sessionName = $this->sessionOptions['name'] ?? \session_name();
79+
$requestSessionId = $request->cookies->get($sessionName, '');
80+
81+
// TODO invalid session id should be expired: see F at https://github.com/php-runtime/runtime/issues/46
82+
\session_id($requestSessionId);
83+
84+
$response = $this->kernel->handle($request);
85+
86+
if ($request->hasSession()) {
87+
$sessionId = \session_id();
88+
// we can not use $session->isStarted() here as this state is not longer available at this time
89+
// TODO session cookie should only be set when persisted by symfony: see E at https://github.com/php-runtime/runtime/issues/46
90+
if ($sessionId && $sessionId !== $requestSessionId) {
91+
$expires = 0;
92+
$lifetime = $this->sessionOptions['cookie_lifetime'] ?? null;
93+
if ($lifetime) {
94+
$expires = time() + $lifetime;
95+
}
96+
97+
$response->headers->setCookie(
98+
Cookie::create(
99+
$sessionName,
100+
$sessionId,
101+
$expires,
102+
$this->sessionOptions['cookie_path'] ?? '/',
103+
$this->sessionOptions['cookie_domain'] ?? null,
104+
$this->sessionOptions['cookie_secure'] ?? null,
105+
$this->sessionOptions['cookie_httponly'] ?? true,
106+
false,
107+
$this->sessionOptions['cookie_samesite'] ?? Cookie::SAMESITE_LAX
108+
)
109+
);
110+
}
111+
}
112+
113+
return $response;
114+
}
110115
}

0 commit comments

Comments
 (0)