diff --git a/Legacy/LegacyHelper.php b/Legacy/LegacyHelper.php new file mode 100644 index 00000000..1b334915 --- /dev/null +++ b/Legacy/LegacyHelper.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Translation\Bundle\Legacy; + +use Symfony\Component\HttpFoundation\RequestStack; + +/** + * A legacy helper to suppress deprecations on RequestStack. + * + * @author Victor Bocharsky + */ +class LegacyHelper +{ + public static function getMainRequest(RequestStack $requestStack) + { + if (\method_exists($requestStack, 'getMainRequest')) { + return $requestStack->getMainRequest(); + } + + return $requestStack->getMasterRequest(); + } +} diff --git a/Translator/EditInPlaceTranslator.php b/Translator/EditInPlaceTranslator.php index 1d94e95e..f2966af3 100644 --- a/Translator/EditInPlaceTranslator.php +++ b/Translator/EditInPlaceTranslator.php @@ -18,6 +18,7 @@ use Symfony\Contracts\Translation\LocaleAwareInterface; use Symfony\Contracts\Translation\TranslatorInterface as NewTranslatorInterface; use Translation\Bundle\EditInPlace\ActivatorInterface; +use Translation\Bundle\Legacy\LegacyHelper; /** * Custom Translator for HTML rendering only (output `` tags). @@ -75,7 +76,8 @@ public function getCatalogue($locale = null): MessageCatalogueInterface public function trans($id, array $parameters = [], $domain = null, $locale = null): ?string { $original = $this->translator->trans($id, $parameters, $domain, $locale); - if (!$this->activator->checkRequest($this->requestStack->getMasterRequest())) { + $request = LegacyHelper::getMainRequest($this->requestStack); + if (!$this->activator->checkRequest($request)) { return $original; } @@ -105,7 +107,8 @@ public function trans($id, array $parameters = [], $domain = null, $locale = nul */ public function transChoice($id, $number, array $parameters = [], $domain = null, $locale = null): ?string { - if (!$this->activator->checkRequest($this->requestStack->getMasterRequest())) { + $request = LegacyHelper::getMainRequest($this->requestStack); + if (!$this->activator->checkRequest($request)) { return $this->translator->transChoice($id, $number, $parameters, $domain, $locale); } diff --git a/Twig/EditInPlaceExtension.php b/Twig/EditInPlaceExtension.php index f86a571e..9a6ba66d 100644 --- a/Twig/EditInPlaceExtension.php +++ b/Twig/EditInPlaceExtension.php @@ -14,6 +14,7 @@ use Symfony\Bridge\Twig\Extension\TranslationExtension; use Symfony\Component\HttpFoundation\RequestStack; use Translation\Bundle\EditInPlace\ActivatorInterface; +use Translation\Bundle\Legacy\LegacyHelper; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; @@ -52,7 +53,9 @@ public function getFilters(): array */ public function isSafe($node): array { - return $this->activator->checkRequest($this->requestStack->getMasterRequest()) ? ['html'] : []; + $request = LegacyHelper::getMainRequest($this->requestStack); + + return $this->activator->checkRequest($request) ? ['html'] : []; } /**