Skip to content

Commit 84c843d

Browse files
authored
Suppress getMasterRequest() deprecation message (#444)
1 parent d7174d9 commit 84c843d

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

Legacy/LegacyHelper.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <tobias.nyholm@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Translation\Bundle\Legacy;
13+
14+
use Symfony\Component\HttpFoundation\RequestStack;
15+
16+
/**
17+
* A legacy helper to suppress deprecations on RequestStack.
18+
*
19+
* @author Victor Bocharsky <bocharsky.bw@gmail.com>
20+
*/
21+
class LegacyHelper
22+
{
23+
public static function getMainRequest(RequestStack $requestStack)
24+
{
25+
if (\method_exists($requestStack, 'getMainRequest')) {
26+
return $requestStack->getMainRequest();
27+
}
28+
29+
return $requestStack->getMasterRequest();
30+
}
31+
}

Translator/EditInPlaceTranslator.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Contracts\Translation\LocaleAwareInterface;
1919
use Symfony\Contracts\Translation\TranslatorInterface as NewTranslatorInterface;
2020
use Translation\Bundle\EditInPlace\ActivatorInterface;
21+
use Translation\Bundle\Legacy\LegacyHelper;
2122

2223
/**
2324
* Custom Translator for HTML rendering only (output `<x-trans>` tags).
@@ -75,7 +76,8 @@ public function getCatalogue($locale = null): MessageCatalogueInterface
7576
public function trans($id, array $parameters = [], $domain = null, $locale = null): ?string
7677
{
7778
$original = $this->translator->trans($id, $parameters, $domain, $locale);
78-
if (!$this->activator->checkRequest($this->requestStack->getMasterRequest())) {
79+
$request = LegacyHelper::getMainRequest($this->requestStack);
80+
if (!$this->activator->checkRequest($request)) {
7981
return $original;
8082
}
8183

@@ -105,7 +107,8 @@ public function trans($id, array $parameters = [], $domain = null, $locale = nul
105107
*/
106108
public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null): ?string
107109
{
108-
if (!$this->activator->checkRequest($this->requestStack->getMasterRequest())) {
110+
$request = LegacyHelper::getMainRequest($this->requestStack);
111+
if (!$this->activator->checkRequest($request)) {
109112
return $this->translator->transChoice($id, $number, $parameters, $domain, $locale);
110113
}
111114

Twig/EditInPlaceExtension.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Bridge\Twig\Extension\TranslationExtension;
1515
use Symfony\Component\HttpFoundation\RequestStack;
1616
use Translation\Bundle\EditInPlace\ActivatorInterface;
17+
use Translation\Bundle\Legacy\LegacyHelper;
1718
use Twig\Extension\AbstractExtension;
1819
use Twig\TwigFilter;
1920

@@ -52,7 +53,9 @@ public function getFilters(): array
5253
*/
5354
public function isSafe($node): array
5455
{
55-
return $this->activator->checkRequest($this->requestStack->getMasterRequest()) ? ['html'] : [];
56+
$request = LegacyHelper::getMainRequest($this->requestStack);
57+
58+
return $this->activator->checkRequest($request) ? ['html'] : [];
5659
}
5760

5861
/**

0 commit comments

Comments
 (0)