Skip to content

Commit 27e02dc

Browse files
Remove roadrunner specific session handling (#62)
* Fix removing of session cookie after logout * Remove roadrunner specific session handling * Simplify also the Runner * Remove session reset * Fix php-cs * Increased required versions * Fix tests
1 parent 93bfade commit 27e02dc

File tree

4 files changed

+18
-130
lines changed

4 files changed

+18
-130
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"nyholm/psr7": "^1.4",
1515
"spiral/roadrunner": "^2.0",
1616
"symfony/dependency-injection": "^5.3 || ^6.0",
17-
"symfony/http-kernel": "^5.3 || ^6.0",
17+
"symfony/http-kernel": "^5.4 || ^6.0",
1818
"symfony/psr-http-message-bridge": "^2.1",
1919
"symfony/runtime": "^5.3 || ^6.0"
2020
},

src/Runner.php

Lines changed: 2 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,7 @@
88
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
99
use Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface;
1010
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
11-
use Symfony\Component\HttpFoundation\Cookie;
12-
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
13-
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
14-
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
1511
use Symfony\Component\HttpKernel\HttpKernelInterface;
16-
use Symfony\Component\HttpKernel\KernelInterface;
1712
use Symfony\Component\HttpKernel\TerminableInterface;
1813
use Symfony\Component\Runtime\RunnerInterface;
1914

@@ -27,33 +22,12 @@ class Runner implements RunnerInterface
2722
private $httpMessageFactory;
2823
private $psrFactory;
2924

30-
/**
31-
* @var array<string, mixed>
32-
*/
33-
private $sessionOptions;
34-
35-
/**
36-
* @param HttpKernelInterface|KernelInterface $kernel
37-
*/
38-
public function __construct($kernel, ?HttpFoundationFactoryInterface $httpFoundationFactory = null, ?HttpMessageFactoryInterface $httpMessageFactory = null)
25+
public function __construct(HttpKernelInterface $kernel, ?HttpFoundationFactoryInterface $httpFoundationFactory = null, ?HttpMessageFactoryInterface $httpMessageFactory = null)
3926
{
4027
$this->kernel = $kernel;
4128
$this->psrFactory = new Psr7\Factory\Psr17Factory();
4229
$this->httpFoundationFactory = $httpFoundationFactory ?? new HttpFoundationFactory();
4330
$this->httpMessageFactory = $httpMessageFactory ?? new PsrHttpFactory($this->psrFactory, $this->psrFactory, $this->psrFactory, $this->psrFactory);
44-
45-
if ($kernel instanceof HttpCache) {
46-
$kernel = $kernel->getKernel();
47-
}
48-
49-
if (!$kernel instanceof KernelInterface) {
50-
throw new \InvalidArgumentException(sprintf('Expected argument of type "%s" or "%s", "%s" given.', KernelInterface::class, HttpCache::class, get_class($kernel)));
51-
}
52-
53-
$kernel->boot();
54-
$container = $kernel->getContainer();
55-
$this->sessionOptions = $container->getParameter('session.storage.options');
56-
$kernel->shutdown();
5731
}
5832

5933
public function run(): int
@@ -64,66 +38,17 @@ public function run(): int
6438
while ($request = $worker->waitRequest()) {
6539
try {
6640
$sfRequest = $this->httpFoundationFactory->createRequest($request);
67-
$sfResponse = $this->handle($sfRequest);
41+
$sfResponse = $this->kernel->handle($sfRequest);
6842
$worker->respond($this->httpMessageFactory->createResponse($sfResponse));
6943

7044
if ($this->kernel instanceof TerminableInterface) {
7145
$this->kernel->terminate($sfRequest, $sfResponse);
7246
}
7347
} catch (\Throwable $e) {
7448
$worker->getWorker()->error((string) $e);
75-
} finally {
76-
if (PHP_SESSION_ACTIVE === session_status()) {
77-
session_abort();
78-
}
79-
80-
// reset all session variables to initialize state
81-
$_SESSION = [];
82-
session_id(''); // in this case session_start() will generate us a new session_id()
8349
}
8450
}
8551

8652
return 0;
8753
}
88-
89-
private function handle(SymfonyRequest $request): SymfonyResponse
90-
{
91-
$sessionName = $this->sessionOptions['name'] ?? \session_name();
92-
/** @var string $requestSessionId */
93-
$requestSessionId = $request->cookies->get($sessionName, '');
94-
95-
// TODO invalid session id should be expired: see F at https://github.com/php-runtime/runtime/issues/46
96-
\session_id($requestSessionId);
97-
98-
$response = $this->kernel->handle($request);
99-
100-
if ($request->hasSession()) {
101-
$sessionId = \session_id();
102-
// we can not use $session->isStarted() here as this state is not longer available at this time
103-
// TODO session cookie should only be set when persisted by symfony: see E at https://github.com/php-runtime/runtime/issues/46
104-
if ($sessionId && $sessionId !== $requestSessionId) {
105-
$expires = 0;
106-
$lifetime = $this->sessionOptions['cookie_lifetime'] ?? null;
107-
if ($lifetime) {
108-
$expires = time() + $lifetime;
109-
}
110-
111-
$response->headers->setCookie(
112-
Cookie::create(
113-
$sessionName,
114-
$sessionId,
115-
$expires,
116-
$this->sessionOptions['cookie_path'] ?? '/',
117-
$this->sessionOptions['cookie_domain'] ?? null,
118-
$this->sessionOptions['cookie_secure'] ?? null,
119-
$this->sessionOptions['cookie_httponly'] ?? true,
120-
false,
121-
$this->sessionOptions['cookie_samesite'] ?? Cookie::SAMESITE_LAX
122-
)
123-
);
124-
}
125-
}
126-
127-
return $response;
128-
}
12954
}

src/Runtime.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace Runtime\RoadRunnerSymfonyNyholm;
44

5-
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
6-
use Symfony\Component\HttpKernel\KernelInterface;
5+
use Symfony\Component\HttpKernel\HttpKernelInterface;
76
use Symfony\Component\Runtime\RunnerInterface;
87
use Symfony\Component\Runtime\SymfonyRuntime;
98

@@ -16,9 +15,7 @@ class Runtime extends SymfonyRuntime
1615
{
1716
public function getRunner(?object $application): RunnerInterface
1817
{
19-
if ($application instanceof KernelInterface
20-
|| ($application instanceof HttpCache && $application->getKernel() instanceof KernelInterface)
21-
) {
18+
if ($application instanceof HttpKernelInterface) {
2219
return new Runner($application);
2320
}
2421

tests/RuntimeTest.php

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
namespace Runtime\RoadRunnerSymfonyNyholm;
44

55
use PHPUnit\Framework\TestCase;
6-
use Symfony\Component\DependencyInjection\ContainerInterface;
6+
use Symfony\Component\Console\Application;
77
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
88
use Symfony\Component\HttpKernel\HttpKernelInterface;
99
use Symfony\Component\HttpKernel\KernelInterface;
10-
use Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner;
10+
use Symfony\Component\Runtime\Runner\Symfony\ConsoleApplicationRunner;
1111

1212
/**
1313
* @author Alexander Schranz <[email protected]>
@@ -20,25 +20,6 @@ public function testGetRuntimeHttpKernel(): void
2020
->disableOriginalConstructor()
2121
->getMock();
2222

23-
$kernel->expects($this->once())
24-
->method('boot');
25-
26-
$container = $this->getMockBuilder(ContainerInterface::class)
27-
->disableOriginalConstructor()
28-
->getMock();
29-
30-
$container->expects($this->once())
31-
->method('getParameter')
32-
->with('session.storage.options')
33-
->will($this->returnValue([]));
34-
35-
$kernel->expects($this->once())
36-
->method('getContainer')
37-
->will($this->returnValue($container));
38-
39-
$kernel->expects($this->once())
40-
->method('shutdown');
41-
4223
$runtime = new Runtime();
4324
$runner = $runtime->getRunner($kernel);
4425

@@ -51,48 +32,33 @@ public function testGetRuntimeHttpCache(): void
5132
->disableOriginalConstructor()
5233
->getMock();
5334

54-
$kernel = $this->getMockBuilder(KernelInterface::class)
55-
->disableOriginalConstructor()
56-
->getMock();
57-
58-
$httpCache->expects($this->atLeastOnce())
59-
->method('getKernel')
60-
->will($this->returnValue($kernel));
35+
$runtime = new Runtime();
36+
$runner = $runtime->getRunner($httpCache);
6137

62-
$kernel->expects($this->once())
63-
->method('boot');
38+
$this->assertInstanceOf(Runner::class, $runner);
39+
}
6440

65-
$container = $this->getMockBuilder(ContainerInterface::class)
41+
public function testGetRuntimeHttpKernelInterface(): void
42+
{
43+
$kernel = $this->getMockBuilder(HttpKernelInterface::class)
6644
->disableOriginalConstructor()
6745
->getMock();
6846

69-
$container->expects($this->once())
70-
->method('getParameter')
71-
->with('session.storage.options')
72-
->will($this->returnValue([]));
73-
74-
$kernel->expects($this->once())
75-
->method('getContainer')
76-
->will($this->returnValue($container));
77-
78-
$kernel->expects($this->once())
79-
->method('shutdown');
80-
8147
$runtime = new Runtime();
82-
$runner = $runtime->getRunner($httpCache);
48+
$runner = $runtime->getRunner($kernel);
8349

8450
$this->assertInstanceOf(Runner::class, $runner);
8551
}
8652

87-
public function testGetRuntimeHttpKernelInterface(): void
53+
public function testGetRuntimeApplication(): void
8854
{
89-
$kernel = $this->getMockBuilder(HttpKernelInterface::class)
55+
$kernel = $this->getMockBuilder(Application::class)
9056
->disableOriginalConstructor()
9157
->getMock();
9258

9359
$runtime = new Runtime();
9460
$runner = $runtime->getRunner($kernel);
9561

96-
$this->assertInstanceOf(HttpKernelRunner::class, $runner);
62+
$this->assertInstanceOf(ConsoleApplicationRunner::class, $runner);
9763
}
9864
}

0 commit comments

Comments
 (0)