Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 06b6e73

Browse files
committed
[HttpFoundation] Make SessionHandlerProxy implement SessionIdInterface
1 parent 52a9292 commit 06b6e73

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

src/Symfony/Component/HttpFoundation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Make `HeaderBag::getDate()`, `Response::getDate()`, `getExpires()` and `getLastModified()` return a `DateTimeImmutable`
8+
* Make `SessionHandlerProxy` implement `SessionIdInterface`
89

910
6.3
1011
---
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpFoundation\Exception;
13+
14+
class SessionIdCreationException extends \RuntimeException
15+
{
16+
public function __construct(string $message = 'Could not create a session id.', int $code = 0, \Throwable $previous = null)
17+
{
18+
parent::__construct($message, $code, $previous);
19+
}
20+
}

src/Symfony/Component/HttpFoundation/Session/Storage/Proxy/SessionHandlerProxy.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@
1111

1212
namespace Symfony\Component\HttpFoundation\Session\Storage\Proxy;
1313

14+
use Symfony\Component\HttpFoundation\Exception\SessionIdCreationException;
1415
use Symfony\Component\HttpFoundation\Session\Storage\Handler\StrictSessionHandler;
1516

1617
/**
1718
* @author Drak <[email protected]>
1819
*/
19-
class SessionHandlerProxy extends AbstractProxy implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
20+
class SessionHandlerProxy extends AbstractProxy implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface, \SessionIdInterface
2021
{
2122
protected $handler;
2223

@@ -73,4 +74,17 @@ public function updateTimestamp(#[\SensitiveParameter] string $sessionId, string
7374
{
7475
return $this->handler instanceof \SessionUpdateTimestampHandlerInterface ? $this->handler->updateTimestamp($sessionId, $data) : $this->write($sessionId, $data);
7576
}
77+
78+
public function create_sid(): string
79+
{
80+
if ($this->handler instanceof \SessionIdInterface) {
81+
return $this->handler->create_sid();
82+
}
83+
84+
if (!$id = session_create_id()) {
85+
throw new SessionIdCreationException();
86+
}
87+
88+
return $id;
89+
}
7690
}

src/Symfony/Component/HttpFoundation/Tests/Session/Storage/Proxy/SessionHandlerProxyTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,26 @@ public static function provideNativeSessionStorageHandler()
179179
[new SessionHandlerProxy(new StrictSessionHandler(new \SessionHandler()))],
180180
];
181181
}
182+
183+
public function testCreateSid()
184+
{
185+
$mock = $this->createMock(SessionIdSessionHandler::class);
186+
$mock->expects($this->once())
187+
->method('create_sid')
188+
->willReturn('a-valid-session-identifier');
189+
190+
$proxy = new SessionHandlerProxy($mock);
191+
$this->assertSame('a-valid-session-identifier', $proxy->create_sid());
192+
193+
$this->proxy->create_sid();
194+
$this->addToAssertionCount(1);
195+
}
182196
}
183197

184198
abstract class TestSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
185199
{
186200
}
201+
202+
abstract class SessionIdSessionHandler implements \SessionHandlerInterface, \SessionIdInterface
203+
{
204+
}

0 commit comments

Comments
 (0)