Skip to content

[HttpFoundation] Make SessionHandlerProxy implement SessionIdInterface #50626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Symfony/Component/HttpFoundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ CHANGELOG
* Add `UriSigner` from the HttpKernel component
* Add `partitioned` flag to `Cookie` (CHIPS Cookie)
* Add argument `bool $flush = true` to `Response::send()`
* Make `MongoDbSessionHandler` instantiable with the mongodb extension directly
* Make `MongoDbSessionHandler` instantiable with the mongodb extension directly
* Make `SessionHandlerProxy` implement `SessionIdInterface`

6.3
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\HttpFoundation\Exception;

class SessionIdCreationException extends \RuntimeException
{
public function __construct(string $message = 'Could not create a session id.', int $code = 0, \Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

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

use Symfony\Component\HttpFoundation\Exception\SessionIdCreationException;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\StrictSessionHandler;

/**
* @author Drak <drak@zikula.org>
*/
class SessionHandlerProxy extends AbstractProxy implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
class SessionHandlerProxy extends AbstractProxy implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface, \SessionIdInterface
{
protected $handler;

Expand Down Expand Up @@ -73,4 +74,17 @@ public function updateTimestamp(#[\SensitiveParameter] string $sessionId, string
{
return $this->handler instanceof \SessionUpdateTimestampHandlerInterface ? $this->handler->updateTimestamp($sessionId, $data) : $this->write($sessionId, $data);
}

public function create_sid(): string
{
if ($this->handler instanceof \SessionIdInterface) {
return $this->handler->create_sid();
}

if (!$id = session_create_id()) {
throw new SessionIdCreationException();
}

return $id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,26 @@ public static function provideNativeSessionStorageHandler()
[new SessionHandlerProxy(new StrictSessionHandler(new \SessionHandler()))],
];
}

public function testCreateSid()
{
$mock = $this->createMock(SessionIdSessionHandler::class);
$mock->expects($this->once())
->method('create_sid')
->willReturn('a-valid-session-identifier');

$proxy = new SessionHandlerProxy($mock);
$this->assertSame('a-valid-session-identifier', $proxy->create_sid());

$this->proxy->create_sid();
$this->addToAssertionCount(1);
}
}

abstract class TestSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
{
}

abstract class SessionIdSessionHandler implements \SessionHandlerInterface, \SessionIdInterface
{
}