Skip to content

Commit a336ffe

Browse files
authored
Merge pull request #54 from wikimedia/deprecated-session
Replace deprecated Session with RequestStack, for Symfony 5.3
2 parents 9aa17ce + dcb795f commit a336ffe

4 files changed

Lines changed: 36 additions & 21 deletions

File tree

Resources/config/services.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ services:
1818
factory: ['Wikimedia\ToolforgeBundle\Service\Intuition', serviceFactory]
1919
arguments:
2020
- "@request_stack"
21-
- "@session"
2221
- "%kernel.project_dir%"
2322
- "%toolforge.intuition.domain%"
2423

@@ -46,6 +45,6 @@ services:
4645
Wikimedia\ToolforgeBundle\Twig\Extension:
4746
arguments:
4847
- '@Krinkle\Intuition\Intuition'
49-
- "@session"
48+
- "@request_stack"
5049
- "%toolforge.intuition.domain%"
5150
tags: [ "twig.extension" ]

Service/Intuition.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,18 @@
66

77
use Krinkle\Intuition\Intuition as KrinkleIntuition;
88
use Symfony\Component\HttpFoundation\RequestStack;
9-
use Symfony\Component\HttpFoundation\Session\SessionInterface;
109

1110
class Intuition extends KrinkleIntuition
1211
{
1312

1413
/**
1514
* @param RequestStack $requestStack
16-
* @param SessionInterface $session
1715
* @param string $projectDir Root filesystem directory of the application.
1816
* @param string $domain The i18n domain.
1917
* @return Intuition
2018
*/
2119
public static function serviceFactory(
2220
RequestStack $requestStack,
23-
SessionInterface $session,
2421
string $projectDir,
2522
string $domain
2623
): Intuition {
@@ -29,18 +26,26 @@ public static function serviceFactory(
2926

3027
// Current request doesn't exist in unit tests, in which case we'll fall back to English.
3128
if (null !== $requestStack->getCurrentRequest()) {
32-
// Use lang from the request or the session.
33-
$queryLang = $requestStack->getCurrentRequest()->query->get('uselang');
34-
$sessionLang = $session->get('lang');
35-
if (!empty($queryLang)) {
36-
$useLang = $queryLang;
37-
} elseif (!empty($sessionLang)) {
38-
$useLang = $sessionLang;
29+
$currentRequest = $requestStack->getCurrentRequest();
30+
// Use lang from the 'lang' query parameter or the 'lang' session variable.
31+
$queryLang = false;
32+
if ($currentRequest->query->has('uselang')) {
33+
$queryLang = $currentRequest->query->has('uselang');
34+
if (!empty($queryLang)) {
35+
$useLang = $queryLang;
36+
}
3937
}
40-
41-
// Save the language to the session.
42-
if ($session->get('lang') !== $useLang) {
43-
$session->set('lang', $useLang);
38+
$sessionLang = false;
39+
if ($currentRequest->hasSession()) {
40+
$session = $currentRequest->getSession();
41+
$sessionLang = $session->get('lang');
42+
if (!empty($sessionLang)) {
43+
$useLang = $sessionLang;
44+
}
45+
// Save the language to the session.
46+
if ($session->get('lang') !== $useLang) {
47+
$session->set('lang', $useLang);
48+
}
4449
}
4550
}
4651

Tests/Twig/ExtensionTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
namespace Wikimedia\ToolforgeBundle\Tests\Twig;
66

77
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\HttpFoundation\Request;
89
use Symfony\Component\HttpFoundation\RequestStack;
910
use Symfony\Component\HttpFoundation\Session\Session;
11+
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
1012
use Wikimedia\ToolforgeBundle\Service\Intuition;
1113
use Wikimedia\ToolforgeBundle\Twig\Extension;
1214

@@ -19,11 +21,14 @@ class ExtensionTest extends TestCase
1921
public function setUp(): void
2022
{
2123
parent::setUp();
22-
$session = new Session();
24+
$requestStack = new RequestStack();
25+
$request = new Request();
26+
$request->setSession(new Session(new MockArraySessionStorage()));
27+
$requestStack->push($request);
2328
$domain = 'toolforge';
2429
$rootDir = dirname(__DIR__, 2);
25-
$intuition = Intuition::serviceFactory(new RequestStack(), $session, $rootDir, $domain);
26-
$this->extension = new Extension($intuition, new Session(), $domain);
30+
$intuition = Intuition::serviceFactory($requestStack, $rootDir, $domain);
31+
$this->extension = new Extension($intuition, $requestStack, $domain);
2732
}
2833

2934
public function testBasics(): void

Twig/Extension.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Wikimedia\ToolforgeBundle\Twig;
66

77
use NumberFormatter;
8+
use Symfony\Component\HttpFoundation\RequestStack;
89
use Symfony\Component\HttpFoundation\Session\Session;
910
use Symfony\Component\Process\Process;
1011
use Twig\Extension\AbstractExtension;
@@ -29,11 +30,13 @@ class Extension extends AbstractExtension
2930

3031
public function __construct(
3132
Intuition $intuition,
32-
Session $session,
33+
RequestStack $requestStack,
3334
string $domain
3435
) {
3536
$this->intuition = $intuition;
36-
$this->session = $session;
37+
if ($requestStack->getCurrentRequest() && $requestStack->getCurrentRequest()->hasSession()) {
38+
$this->session = $requestStack->getCurrentRequest()->getSession();
39+
}
3740
$this->domain = $domain;
3841
}
3942

@@ -68,6 +71,9 @@ public function getFunctions(): array
6871
*/
6972
public function getLoggedInUser()
7073
{
74+
if (!$this->session) {
75+
return false;
76+
}
7177
return $this->session->get('logged_in_user');
7278
}
7379

0 commit comments

Comments
 (0)