From 8b8022d45bea62a9a63f582946b12eefe2e7f488 Mon Sep 17 00:00:00 2001 From: Ulrich Date: Thu, 15 Dec 2022 11:08:55 +0100 Subject: [PATCH 1/2] fix(flashbag): fixed addFlash in ControllerTrait for Symfony 6 --- src/Bundle/Controller/ControllerTrait.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Bundle/Controller/ControllerTrait.php b/src/Bundle/Controller/ControllerTrait.php index bdec112d0..c1c37c7d9 100644 --- a/src/Bundle/Controller/ControllerTrait.php +++ b/src/Bundle/Controller/ControllerTrait.php @@ -18,11 +18,13 @@ use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\BinaryFileResponse; +use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\ResponseHeaderBag; +use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface; use Symfony\Component\HttpFoundation\StreamedResponse; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\HttpKernelInterface; @@ -156,13 +158,19 @@ protected function file($file, string $fileName = null, string $disposition = Re * * @final */ - protected function addFlash(string $type, $message) + protected function addFlash(string $type, mixed $message): void { - if (!$this->container->has('session')) { - throw new \LogicException('You can not use the addFlash method if sessions are disabled. Enable them in "config/packages/framework.yaml".'); + try { + $session = $this->container->get('request_stack')->getSession(); + } catch (SessionNotFoundException $e) { + throw new \LogicException('You cannot use the addFlash method if sessions are disabled. Enable them in "config/packages/framework.yaml".', 0, $e); } - $this->container->get('session')->getFlashBag()->add($type, $message); + if (!$session instanceof FlashBagAwareSessionInterface) { + trigger_deprecation('symfony/framework-bundle', '6.2', 'Calling "addFlash()" method when the session does not implement %s is deprecated.', FlashBagAwareSessionInterface::class); + } + + $session->getFlashBag()->add($type, $message); } /** From ef28368414c94db92ca9c05db3bde7f6e7129134 Mon Sep 17 00:00:00 2001 From: Ulrich Date: Thu, 2 Feb 2023 14:16:26 +0100 Subject: [PATCH 2/2] fix(flashbag): removed potential BC from addFlash function --- src/Bundle/Controller/ControllerTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bundle/Controller/ControllerTrait.php b/src/Bundle/Controller/ControllerTrait.php index c1c37c7d9..0076a8e7d 100644 --- a/src/Bundle/Controller/ControllerTrait.php +++ b/src/Bundle/Controller/ControllerTrait.php @@ -158,7 +158,7 @@ protected function file($file, string $fileName = null, string $disposition = Re * * @final */ - protected function addFlash(string $type, mixed $message): void + protected function addFlash(string $type, $message) { try { $session = $this->container->get('request_stack')->getSession();