8
8
use Symfony \Bridge \PsrHttpMessage \Factory \PsrHttpFactory ;
9
9
use Symfony \Bridge \PsrHttpMessage \HttpFoundationFactoryInterface ;
10
10
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 ;
15
11
use Symfony \Component \HttpKernel \HttpKernelInterface ;
16
- use Symfony \Component \HttpKernel \KernelInterface ;
17
12
use Symfony \Component \HttpKernel \TerminableInterface ;
18
13
use Symfony \Component \Runtime \RunnerInterface ;
19
14
@@ -27,33 +22,12 @@ class Runner implements RunnerInterface
27
22
private $ httpMessageFactory ;
28
23
private $ psrFactory ;
29
24
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 )
39
26
{
40
27
$ this ->kernel = $ kernel ;
41
28
$ this ->psrFactory = new Psr7 \Factory \Psr17Factory ();
42
29
$ this ->httpFoundationFactory = $ httpFoundationFactory ?? new HttpFoundationFactory ();
43
30
$ 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 ();
57
31
}
58
32
59
33
public function run (): int
@@ -64,66 +38,17 @@ public function run(): int
64
38
while ($ request = $ worker ->waitRequest ()) {
65
39
try {
66
40
$ sfRequest = $ this ->httpFoundationFactory ->createRequest ($ request );
67
- $ sfResponse = $ this ->handle ($ sfRequest );
41
+ $ sfResponse = $ this ->kernel -> handle ($ sfRequest );
68
42
$ worker ->respond ($ this ->httpMessageFactory ->createResponse ($ sfResponse ));
69
43
70
44
if ($ this ->kernel instanceof TerminableInterface) {
71
45
$ this ->kernel ->terminate ($ sfRequest , $ sfResponse );
72
46
}
73
47
} catch (\Throwable $ e ) {
74
48
$ 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()
83
49
}
84
50
}
85
51
86
52
return 0 ;
87
53
}
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
- }
129
54
}
0 commit comments