Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbe9fc75

Browse files
Fix session cookie handling in cli context
1 parent53d9b10 commitbe9fc75

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

‎src/Symfony/Component/HttpKernel/EventListener/AbstractSessionListener.php‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
usePsr\Container\ContainerInterface;
1515
useSymfony\Component\EventDispatcher\EventSubscriberInterface;
16+
useSymfony\Component\HttpFoundation\Cookie;
1617
useSymfony\Component\HttpFoundation\Session\Session;
1718
useSymfony\Component\HttpFoundation\Session\SessionInterface;
1819
useSymfony\Component\HttpKernel\Event\FilterResponseEvent;
@@ -115,6 +116,34 @@ public function onKernelResponse(FilterResponseEvent $event)
115116
* it is saved will just restart it.
116117
*/
117118
$session->save();
119+
120+
/*
121+
* Write cookie to request object for cli context based runner like roadrunner,
122+
* where php can not write the cookie.
123+
*/
124+
if (\PHP_SAPI ==='cli') {
125+
$cookieParams =session_get_cookie_params();
126+
127+
$expire =0;
128+
$lifetime =$cookieParams['lifetime'] ??null;
129+
if ($lifetime) {
130+
$expire =time() +$lifetime;
131+
}
132+
133+
$response->headers->setCookie(
134+
Cookie::create(
135+
$session->getName(),
136+
$session->getId(),
137+
$expire,
138+
$cookieParams['path'] ??'/',
139+
$cookieParams['domain'] ??null,
140+
$cookieParams['secure'] ??null,
141+
$cookieParams['httponly'] ??true,
142+
false,
143+
$cookieParams['samesite'] ?? Cookie::SAMESITE_LAX
144+
)
145+
);
146+
}
118147
}
119148
}
120149

‎src/Symfony/Component/HttpKernel/Tests/EventListener/SessionListenerTest.php‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,32 @@ public function testResponseIsStillPublicIfSessionStartedAndHeaderPresent()
120120
$this->assertFalse($response->headers->has(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER));
121121
}
122122

123+
publicfunctiontestSessionSaveAndResponseHasSessionCookie()
124+
{
125+
$session =$this->getMockBuilder(Session::class)->disableOriginalConstructor()->getMock();
126+
$session->expects($this->exactly(2))->method('getUsageIndex')->will($this->onConsecutiveCalls(0,1));
127+
$session->expects($this->exactly(1))->method('getId')->willReturn('123456');
128+
$session->expects($this->exactly(1))->method('getName')->willReturn('PHPSESSID');
129+
$session->expects($this->exactly(1))->method('save');
130+
$session->expects($this->exactly(1))->method('isStarted')->willReturn(true);
131+
132+
$container =newContainer();
133+
$container->set('initialized_session',$session);
134+
135+
$listener =newSessionListener($container);
136+
$kernel =$this->getMockBuilder(HttpKernelInterface::class)->disableOriginalConstructor()->getMock();
137+
138+
$request =newRequest();
139+
$listener->onKernelRequest(newRequestEvent($kernel,$request, HttpKernelInterface::MASTER_REQUEST));
140+
141+
$response =newResponse();
142+
$listener->onKernelResponse(newResponseEvent($kernel,newRequest(), HttpKernelInterface::MASTER_REQUEST,$response));
143+
144+
$cookies =$response->headers->getCookies();
145+
$this->assertSame('PHPSESSID',$cookies[0]->getName());
146+
$this->assertSame('123456',$cookies[0]->getValue());
147+
}
148+
123149
publicfunctiontestUninitializedSession()
124150
{
125151
$kernel =$this->createMock(HttpKernelInterface::class);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp