From 051b14193409391782fc369e89ec626fab8ad0a8 Mon Sep 17 00:00:00 2001 From: Robbe De Geyndt Date: Tue, 1 Feb 2022 18:16:37 +0100 Subject: [PATCH 1/3] Fix for deprecated session service in Symfony 6 --- EditInPlace/Activator.php | 18 +++++++++--------- Resources/config/edit_in_place.yaml | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/EditInPlace/Activator.php b/EditInPlace/Activator.php index 1e25b5a2..814cd18e 100644 --- a/EditInPlace/Activator.php +++ b/EditInPlace/Activator.php @@ -12,7 +12,7 @@ namespace Translation\Bundle\EditInPlace; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\RequestStack; /** * Default Activator implementation. @@ -24,13 +24,13 @@ final class Activator implements ActivatorInterface const KEY = 'translation_bundle.edit_in_place.enabled'; /** - * @var Session + * @var RequestStack */ - private $session; + private $requestStack; - public function __construct(Session $session) + public function __construct(RequestStack $requestStack) { - $this->session = $session; + $this->requestStack = $requestStack; } /** @@ -38,7 +38,7 @@ public function __construct(Session $session) */ public function activate(): void { - $this->session->set(self::KEY, true); + $this->requestStack->getSession()->set(self::KEY, true); } /** @@ -46,7 +46,7 @@ public function activate(): void */ public function deactivate(): void { - $this->session->remove(self::KEY); + $this->requestStack->getSession()->remove(self::KEY); } /** @@ -54,10 +54,10 @@ public function deactivate(): void */ public function checkRequest(Request $request = null): bool { - if (!$this->session->has(self::KEY)) { + if (!$this->requestStack->getSession()->has(self::KEY)) { return false; } - return $this->session->get(self::KEY, false); + return $this->requestStack->getSession()->get(self::KEY, false); } } diff --git a/Resources/config/edit_in_place.yaml b/Resources/config/edit_in_place.yaml index c6ac41cd..764e6395 100644 --- a/Resources/config/edit_in_place.yaml +++ b/Resources/config/edit_in_place.yaml @@ -19,7 +19,7 @@ services: - ~ Translation\Bundle\EditInPlace\Activator: - arguments: ['@session'] + arguments: ['@request_stack'] public: true Translation\Bundle\Translator\EditInPlaceTranslator: From cf293819c7282307ad0d34701c08a582d9df6ee5 Mon Sep 17 00:00:00 2001 From: Robbe De Geyndt Date: Tue, 1 Feb 2022 20:17:04 +0100 Subject: [PATCH 2/3] Keep Backward compatibility with optional service argument --- EditInPlace/Activator.php | 33 +++++++++++++++++++++++++---- Resources/config/edit_in_place.yaml | 2 ++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/EditInPlace/Activator.php b/EditInPlace/Activator.php index 814cd18e..3dd624b6 100644 --- a/EditInPlace/Activator.php +++ b/EditInPlace/Activator.php @@ -13,6 +13,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\HttpFoundation\Session\Session; /** * Default Activator implementation. @@ -28,17 +29,41 @@ final class Activator implements ActivatorInterface */ private $requestStack; + /** + * @var Session|null + */ + private $session = null; + public function __construct(RequestStack $requestStack) { $this->requestStack = $requestStack; } + /** + * Set session if available + */ + public function setSession(Session $session): void + { + $this->session = $session; + } + + /** + * Get session based on availability + */ + private function getSession(): Session { + $session = $this->session; + if (is_null($session)) { + $session = $this->requestStack->getSession(); + } + return $session; + } + /** * Enable the Edit In Place mode. */ public function activate(): void { - $this->requestStack->getSession()->set(self::KEY, true); + $this->getSession()->set(self::KEY, true); } /** @@ -46,7 +71,7 @@ public function activate(): void */ public function deactivate(): void { - $this->requestStack->getSession()->remove(self::KEY); + $this->getSession()->remove(self::KEY); } /** @@ -54,10 +79,10 @@ public function deactivate(): void */ public function checkRequest(Request $request = null): bool { - if (!$this->requestStack->getSession()->has(self::KEY)) { + if (!$this->getSession()->has(self::KEY)) { return false; } - return $this->requestStack->getSession()->get(self::KEY, false); + return $this->getSession()->get(self::KEY, false); } } diff --git a/Resources/config/edit_in_place.yaml b/Resources/config/edit_in_place.yaml index 764e6395..af60adb3 100644 --- a/Resources/config/edit_in_place.yaml +++ b/Resources/config/edit_in_place.yaml @@ -20,6 +20,8 @@ services: Translation\Bundle\EditInPlace\Activator: arguments: ['@request_stack'] + calls: + - setSession: ['@?session'] public: true Translation\Bundle\Translator\EditInPlaceTranslator: From 29f8d6ddb3dd1994cfc49caa4ed28c9d4b71a83c Mon Sep 17 00:00:00 2001 From: Robbe De Geyndt Date: Wed, 2 Feb 2022 09:53:25 +0100 Subject: [PATCH 3/3] Fix code styling --- EditInPlace/Activator.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/EditInPlace/Activator.php b/EditInPlace/Activator.php index 3dd624b6..16c2045c 100644 --- a/EditInPlace/Activator.php +++ b/EditInPlace/Activator.php @@ -40,7 +40,7 @@ public function __construct(RequestStack $requestStack) } /** - * Set session if available + * Set session if available. */ public function setSession(Session $session): void { @@ -48,13 +48,15 @@ public function setSession(Session $session): void } /** - * Get session based on availability + * Get session based on availability. */ - private function getSession(): Session { + private function getSession(): Session + { $session = $this->session; - if (is_null($session)) { + if (null === $session) { $session = $this->requestStack->getSession(); } + return $session; }