From f0a34b77c0528beb1c79526776ff50b615a13eaa Mon Sep 17 00:00:00 2001 From: Martin Lutter Date: Tue, 2 Aug 2022 15:47:57 +0200 Subject: [PATCH 1/3] update to support symfony 6 update composer inject RequestStack, fix service name change getMasterRequest to getMainRequest update composer --- composer.json | 16 ++++++++-------- src/Controller/AjaxcomSymfony4Trait.php | 4 ++-- src/Mutation/Callbacks.php | 17 +++++++++-------- src/Mutation/ChangeUrl.php | 2 +- src/Mutation/FlashMessages.php | 11 ++++++----- src/Resources/config/services.yml | 10 +++++----- 6 files changed, 31 insertions(+), 29 deletions(-) diff --git a/composer.json b/composer.json index 2f45171..d740b34 100644 --- a/composer.json +++ b/composer.json @@ -8,15 +8,15 @@ } }, "require": { - "php": "^7.1|^8.0", + "php": "^8.0", "everlutionsk/ajaxcom-php": "^1.1", - "symfony/framework-bundle": "^3.4.26|^4.1.12|^5.0", - "symfony/http-foundation": "^3.4.26|^4.1.12|^5.0", - "symfony/routing": "^3.4.26|^4.1.12|^5.0", - "symfony/config": "^3.4.26|^4.1.12|^5.0", - "symfony/dependency-injection": "^3.4.26|^4.1.12|^5.0", - "symfony/http-kernel": "^3.4.26|^4.1.12|^5.0", - "symfony/twig-bundle": "^3.4.26|^4.1.12|^5.0" + "symfony/framework-bundle": ">=6.0", + "symfony/http-foundation": ">=6.0", + "symfony/routing": ">=6.0", + "symfony/config": ">=6.0", + "symfony/dependency-injection": ">=6.0", + "symfony/http-kernel": ">=6.0", + "symfony/twig-bundle": ">=6.0" }, "license": "MIT", "authors": [ diff --git a/src/Controller/AjaxcomSymfony4Trait.php b/src/Controller/AjaxcomSymfony4Trait.php index 9d4d8a0..0957c66 100644 --- a/src/Controller/AjaxcomSymfony4Trait.php +++ b/src/Controller/AjaxcomSymfony4Trait.php @@ -74,7 +74,7 @@ public function setAjaxcomRequiredServices( protected function render(string $view, array $parameters = array(), Response $response = null): Response { - $request = $this->requestStack->getMasterRequest(); + $request = $this->requestStack->getMainRequest(); if ($this->isAjax()) { return $this->ajaxcom->handle($view, $parameters); @@ -156,7 +156,7 @@ protected function prependAjaxBlock(string $id): self protected function isAjax(): bool { /** @var Request $request */ - $request = $this->requestStack->getMasterRequest(); + $request = $this->requestStack->getMainRequest(); $isAjax = $request->server->getBoolean(Ajaxcom::AJAX_COM_HEADER, false); return $isAjax; diff --git a/src/Mutation/Callbacks.php b/src/Mutation/Callbacks.php index 4196faf..690d1f1 100644 --- a/src/Mutation/Callbacks.php +++ b/src/Mutation/Callbacks.php @@ -6,6 +6,7 @@ use Everlution\Ajaxcom\Handler; use Everlution\AjaxcomBundle\DataObject\Callback as AjaxCallback; +use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Session\Session; /** @@ -17,34 +18,34 @@ class Callbacks implements MutatorInterface { const SESSION_KEY = 'ajaxcom/callbacks'; - /** @var Session */ - private $session; + /** @var RequestStack */ + private $requestStack; - public function __construct(Session $session) + public function __construct(RequestStack $requestStack) { - $this->session = $session; + $this->requestStack = $requestStack; } public function mutate(Handler $ajax): Handler { /** @var AjaxCallback[] $callbacks */ - $callbacks = $this->session->get(self::SESSION_KEY, []); + $callbacks = $this->requestStack->getSession()->get(self::SESSION_KEY, []); uasort($callbacks, [$this, 'sortByPriority']); foreach ($callbacks as $callback) { $ajax->callback($callback->getFunction(), $callback->getParameters()); } - $this->session->remove(self::SESSION_KEY); + $this->requestStack->getSession()->remove(self::SESSION_KEY); return $ajax; } public function add(AjaxCallback $callback): self { - $callbacks = $this->session->get(self::SESSION_KEY, []); + $callbacks = $this->requestStack->getSession()->get(self::SESSION_KEY, []); $callbacks[] = $callback; - $this->session->set(self::SESSION_KEY, $callbacks); + $this->requestStack->getSession()->set(self::SESSION_KEY, $callbacks); return $this; } diff --git a/src/Mutation/ChangeUrl.php b/src/Mutation/ChangeUrl.php index 1847082..55c6aa8 100644 --- a/src/Mutation/ChangeUrl.php +++ b/src/Mutation/ChangeUrl.php @@ -27,7 +27,7 @@ class ChangeUrl implements MutatorInterface public function __construct(RequestStack $requestStack, UrlGeneratorInterface $router) { $this->router = $router; - $this->request = $requestStack->getMasterRequest(); + $this->request = $requestStack->getMainRequest(); } public function mutate(Handler $ajax): Handler diff --git a/src/Mutation/FlashMessages.php b/src/Mutation/FlashMessages.php index 426c0ef..90e9fdb 100644 --- a/src/Mutation/FlashMessages.php +++ b/src/Mutation/FlashMessages.php @@ -8,6 +8,7 @@ use Everlution\AjaxcomBundle\AjaxcomException; use Everlution\AjaxcomBundle\DataObject\Block; use Everlution\AjaxcomBundle\Service\RenderBlock; +use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; /** @@ -19,8 +20,8 @@ class FlashMessages implements MutatorInterface { /** @var RenderBlock */ private $renderBlock; - /** @var FlashBagInterface */ - private $flashBag; + /** @var RequestStack */ + private $requestStack; /** @var string */ private $flashesTemplate; /** @var string */ @@ -28,12 +29,12 @@ class FlashMessages implements MutatorInterface public function __construct( RenderBlock $renderBlock, - FlashBagInterface $flashBag, + RequestStack $requestStack, string $flashesTemplate, string $flashesBlockId ) { $this->renderBlock = $renderBlock; - $this->flashBag = $flashBag; + $this->requestStack = $requestStack; $this->flashesTemplate = $flashesTemplate; $this->flashesBlockId = $flashesBlockId; } @@ -44,7 +45,7 @@ public function mutate(Handler $ajax): Handler $messages = $this->renderBlock->render( (new Block($this->flashesBlockId))->refresh(), $this->flashesTemplate, - ['flashes' => $this->flashBag->all()] + ['flashes' => $this->requestStack->getSession()->getFlashBag()->all()] ); $ajax->container("#$this->flashesBlockId")->html($messages); } catch (AjaxcomException $exception) { diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index 902eec4..5d58c45 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -4,7 +4,7 @@ services: public: true arguments: - '@everlution.ajaxcom' - - '@ajaxcom.mutation.containe' + - '@ajaxcom.mutation.container' everlution.ajaxcom: class: Everlution\Ajaxcom\Handler @@ -17,7 +17,7 @@ services: - '@twig' # mutations - ajaxcom.mutation.containe: + ajaxcom.mutation.container: class: Everlution\AjaxcomBundle\Mutation\Container # the order of following call is important calls: @@ -65,7 +65,7 @@ services: class: Everlution\AjaxcomBundle\Mutation\Callbacks public: true arguments: - - '@session' + - '@request_stack' ajaxcom.mutation.replace_class: class: Everlution\AjaxcomBundle\Mutation\ReplaceClass @@ -85,7 +85,7 @@ services: public: false arguments: - '@ajaxcom.render_block' - - '@session.flash_bag' + - '@request_stack' - '%everlution.ajaxcom.flash_template%' - '%everlution.ajaxcom.flash_block_id%' @@ -120,7 +120,7 @@ services: Everlution\AjaxcomBundle\Service\Ajaxcom: '@ajaxcom.handler' Everlution\Ajaxcom\Handler: '@everlution.ajaxcom' Everlution\AjaxcomBundle\Service\RenderBlock: '@ajaxcom.render_block' - Everlution\AjaxcomBundle\Mutation\Container: '@ajaxcom.mutation.containe' + Everlution\AjaxcomBundle\Mutation\Container: '@ajaxcom.mutation.container' Everlution\AjaxcomBundle\Mutation\AppendBlocks: '@ajaxcom.mutation.append_blocks' Everlution\AjaxcomBundle\Mutation\PrependBlocks: '@ajaxcom.mutation.prepend_blocks' Everlution\AjaxcomBundle\Mutation\AddBlocks: '@ajaxcom.mutation.add_blocks' From 7e847b4416a454e7c6973e7ae75fe319bdc4f3da Mon Sep 17 00:00:00 2001 From: Martin Lutter Date: Tue, 15 Nov 2022 13:32:50 +0100 Subject: [PATCH 2/3] make sure $session is Session --- src/Mutation/FlashMessages.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Mutation/FlashMessages.php b/src/Mutation/FlashMessages.php index 90e9fdb..6130188 100644 --- a/src/Mutation/FlashMessages.php +++ b/src/Mutation/FlashMessages.php @@ -9,7 +9,7 @@ use Everlution\AjaxcomBundle\DataObject\Block; use Everlution\AjaxcomBundle\Service\RenderBlock; use Symfony\Component\HttpFoundation\RequestStack; -use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; +use Symfony\Component\HttpFoundation\Session\Session; /** * Class FlashMessages. @@ -41,11 +41,16 @@ public function __construct( public function mutate(Handler $ajax): Handler { + $session = $this->requestStack->getSession(); + if (!$session instanceof Session) { + throw new AjaxcomException('Bad session instance'); + } + try { $messages = $this->renderBlock->render( (new Block($this->flashesBlockId))->refresh(), $this->flashesTemplate, - ['flashes' => $this->requestStack->getSession()->getFlashBag()->all()] + ['flashes' => $session->getFlashBag()->all()] ); $ajax->container("#$this->flashesBlockId")->html($messages); } catch (AjaxcomException $exception) { From 027ce672d94b771af8ac0249b0e1af2d17b26169 Mon Sep 17 00:00:00 2001 From: Martin Lutter Date: Tue, 15 Nov 2022 13:45:38 +0100 Subject: [PATCH 3/3] add ignore annotation --- src/Mutation/FlashMessages.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mutation/FlashMessages.php b/src/Mutation/FlashMessages.php index 6130188..cd58526 100644 --- a/src/Mutation/FlashMessages.php +++ b/src/Mutation/FlashMessages.php @@ -50,7 +50,7 @@ public function mutate(Handler $ajax): Handler $messages = $this->renderBlock->render( (new Block($this->flashesBlockId))->refresh(), $this->flashesTemplate, - ['flashes' => $session->getFlashBag()->all()] + ['flashes' => $session->/** @scrutinizer ignore-call */getFlashBag()->all()] ); $ajax->container("#$this->flashesBlockId")->html($messages); } catch (AjaxcomException $exception) {