-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Description
Symfony version(s) affected
6.3.0
Description
The getSession function in the AppVariable class in TwigBridge has a Session
return type. This prevents creating a custom session factory / custom session. Currently the following type error is thrown:
Symfony\Bridge\Twig\AppVariable::getSession(): Return value must be of type ?Symfony\Component\HttpFoundation\Session\Session, App\Framework\CustomSession returned
.
Changing the return type to Symfony\Component\HttpFoundation\Session\SessionInterface
will resolve the error.
How to reproduce
Decorate the 'session.factory' service with a custom session factory in services.yaml:
App\Framework\CustomSessionFactory:
decorates: session.factory
arguments:
$requestStack: '@request_stack'
$storageFactory: '@session.storage.factory'
Create a custom session factory:
<?php
declare(strict_types=1);
namespace App\Framework;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionFactoryInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageFactoryInterface;
class CustomSessionFactory implements SessionFactoryInterface {
public function __construct(private RequestStack $requestStack, private SessionStorageFactoryInterface $storageFactory) {
}
public function createSession(): SessionInterface {
return new CustomSession(
new Session(
$this->storageFactory->createStorage($this->requestStack->getMainRequest())
)
);
}
}
Create a custom session class:
<?php
declare(strict_types=1);
namespace App\Framework;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
class CustomSession implements SessionInterface {
public function __construct(private SessionInterface $session) {
}
// ... ///
}
Render a twig template and the error will be thrown
Possible Solution
Change return type of Symfony\Bridge\Twig\AppVariable::getSession()
from Session
to SessionInterface
Additional Context
No response