88use Symfony \Bridge \PsrHttpMessage \Factory \PsrHttpFactory ;
99use Symfony \Bridge \PsrHttpMessage \HttpFoundationFactoryInterface ;
1010use 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 ;
1511use Symfony \Component \HttpKernel \HttpKernelInterface ;
16- use Symfony \Component \HttpKernel \KernelInterface ;
1712use Symfony \Component \HttpKernel \TerminableInterface ;
1813use 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}
0 commit comments