diff --git a/UPGRADE-5.4.md b/UPGRADE-5.4.md index c5f3095ffcf21..79a26bd52ca84 100644 --- a/UPGRADE-5.4.md +++ b/UPGRADE-5.4.md @@ -5,3 +5,8 @@ FrameworkBundle --------------- * Deprecate the `AdapterInterface` autowiring alias, use `CacheItemPoolInterface` instead + +HttpKernel +---------- + + * Deprecate `AbstractTestSessionListener::getSession` inject a session in the request instead diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/test.php b/src/Symfony/Bundle/FrameworkBundle/Resources/config/test.php index 29f3fa815c836..61e4052521329 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/test.php +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/test.php @@ -38,7 +38,6 @@ ->set('test.session.listener', TestSessionListener::class) ->args([ service_locator([ - 'session_factory' => service('session.factory')->ignoreOnInvalid(), 'session' => service('.session.do-not-use')->ignoreOnInvalid(), ]), ]) diff --git a/src/Symfony/Component/HttpKernel/CHANGELOG.md b/src/Symfony/Component/HttpKernel/CHANGELOG.md index 1a5f67ba1db83..ed36ac8cbfbaa 100644 --- a/src/Symfony/Component/HttpKernel/CHANGELOG.md +++ b/src/Symfony/Component/HttpKernel/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +5.4 +--- + + * Deprecate `AbstractTestSessionListener::getSession` inject a session in the request instead + 5.3 --- diff --git a/src/Symfony/Component/HttpKernel/EventListener/AbstractTestSessionListener.php b/src/Symfony/Component/HttpKernel/EventListener/AbstractTestSessionListener.php index 0c88187d1e931..8fa667ced3c8f 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/AbstractTestSessionListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/AbstractTestSessionListener.php @@ -46,7 +46,9 @@ public function onKernelRequest(RequestEvent $event) } // bootstrap the session - if (!$session = $this->getSession()) { + if ($event->getRequest()->hasSession()) { + $session = $event->getRequest()->getSession(); + } elseif (!$session = $this->getSession()) { return; } @@ -100,7 +102,7 @@ public function onKernelResponse(ResponseEvent $event) public static function getSubscribedEvents(): array { return [ - KernelEvents::REQUEST => ['onKernelRequest', 192], + KernelEvents::REQUEST => ['onKernelRequest', 127], // AFTER SessionListener KernelEvents::RESPONSE => ['onKernelResponse', -128], ]; } @@ -108,6 +110,8 @@ public static function getSubscribedEvents(): array /** * Gets the session object. * + * @deprecated since Symfony 5.4, will be removed in 6.0. + * * @return SessionInterface|null A SessionInterface instance or null if no session is available */ abstract protected function getSession(); diff --git a/src/Symfony/Component/HttpKernel/EventListener/TestSessionListener.php b/src/Symfony/Component/HttpKernel/EventListener/TestSessionListener.php index 6d4b36ace352c..ceac3dde8102b 100644 --- a/src/Symfony/Component/HttpKernel/EventListener/TestSessionListener.php +++ b/src/Symfony/Component/HttpKernel/EventListener/TestSessionListener.php @@ -31,16 +31,17 @@ public function __construct(ContainerInterface $container, array $sessionOptions parent::__construct($sessionOptions); } + /** + * @deprecated since Symfony 5.4, will be removed in 6.0. + */ protected function getSession(): ?SessionInterface { + trigger_deprecation('symfony/http-kernel', '5.4', '"%s" is deprecated and will be removed in 6.0, inject a session in the request instead.', __METHOD__); + if ($this->container->has('session')) { return $this->container->get('session'); } - if ($this->container->has('session_factory')) { - return $this->container->get('session_factory')->createSession(); - } - return null; } } diff --git a/src/Symfony/Component/HttpKernel/Tests/EventListener/TestSessionListenerTest.php b/src/Symfony/Component/HttpKernel/Tests/EventListener/TestSessionListenerTest.php index 016f7901c667a..abb13bcb10408 100644 --- a/src/Symfony/Component/HttpKernel/Tests/EventListener/TestSessionListenerTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/EventListener/TestSessionListenerTest.php @@ -19,7 +19,6 @@ use Symfony\Component\HttpKernel\Event\RequestEvent; use Symfony\Component\HttpKernel\Event\ResponseEvent; use Symfony\Component\HttpKernel\EventListener\AbstractTestSessionListener; -use Symfony\Component\HttpKernel\EventListener\SessionListener; use Symfony\Component\HttpKernel\EventListener\TestSessionListener; use Symfony\Component\HttpKernel\HttpKernelInterface; @@ -99,6 +98,7 @@ public function testEmptySessionWithNewSessionIdDoesSendCookie() $kernel = $this->createMock(HttpKernelInterface::class); $request = Request::create('/', 'GET', [], ['MOCKSESSID' => '123']); + $request->setSession($this->getSession()); $event = new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST); $this->listener->onKernelRequest($event); @@ -118,6 +118,7 @@ public function testSessionWithNewSessionIdAndNewCookieDoesNotSendAnotherCookie( $kernel = $this->createMock(HttpKernelInterface::class); $request = Request::create('/', 'GET', [], ['MOCKSESSID' => '123']); + $request->setSession($this->getSession()); $event = new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST); $this->listener->onKernelRequest($event);