Skip to content

Commit ea84216

Browse files
robbedgRobbe De Geyndt
andauthored
Fix for deprecated session service in Symfony 6 (#468)
* Fix for deprecated session service in Symfony 6 * Keep Backward compatibility with optional service argument * Fix code styling Co-authored-by: Robbe De Geyndt <robbe.degeyndt@aware.be>
1 parent b946f06 commit ea84216

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

EditInPlace/Activator.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Translation\Bundle\EditInPlace;
1313

1414
use Symfony\Component\HttpFoundation\Request;
15+
use Symfony\Component\HttpFoundation\RequestStack;
1516
use Symfony\Component\HttpFoundation\Session\Session;
1617

1718
/**
@@ -24,40 +25,66 @@ final class Activator implements ActivatorInterface
2425
const KEY = 'translation_bundle.edit_in_place.enabled';
2526

2627
/**
27-
* @var Session
28+
* @var RequestStack
2829
*/
29-
private $session;
30+
private $requestStack;
3031

31-
public function __construct(Session $session)
32+
/**
33+
* @var Session|null
34+
*/
35+
private $session = null;
36+
37+
public function __construct(RequestStack $requestStack)
38+
{
39+
$this->requestStack = $requestStack;
40+
}
41+
42+
/**
43+
* Set session if available.
44+
*/
45+
public function setSession(Session $session): void
3246
{
3347
$this->session = $session;
3448
}
3549

50+
/**
51+
* Get session based on availability.
52+
*/
53+
private function getSession(): Session
54+
{
55+
$session = $this->session;
56+
if (null === $session) {
57+
$session = $this->requestStack->getSession();
58+
}
59+
60+
return $session;
61+
}
62+
3663
/**
3764
* Enable the Edit In Place mode.
3865
*/
3966
public function activate(): void
4067
{
41-
$this->session->set(self::KEY, true);
68+
$this->getSession()->set(self::KEY, true);
4269
}
4370

4471
/**
4572
* Disable the Edit In Place mode.
4673
*/
4774
public function deactivate(): void
4875
{
49-
$this->session->remove(self::KEY);
76+
$this->getSession()->remove(self::KEY);
5077
}
5178

5279
/**
5380
* {@inheritdoc}
5481
*/
5582
public function checkRequest(Request $request = null): bool
5683
{
57-
if (!$this->session->has(self::KEY)) {
84+
if (!$this->getSession()->has(self::KEY)) {
5885
return false;
5986
}
6087

61-
return $this->session->get(self::KEY, false);
88+
return $this->getSession()->get(self::KEY, false);
6289
}
6390
}

Resources/config/edit_in_place.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ services:
1919
- ~
2020

2121
Translation\Bundle\EditInPlace\Activator:
22-
arguments: ['@session']
22+
arguments: ['@request_stack']
23+
calls:
24+
- setSession: ['@?session']
2325
public: true
2426

2527
Translation\Bundle\Translator\EditInPlaceTranslator:

0 commit comments

Comments
 (0)