From d286b57e9927322db870ceef8c25d092b1b50a90 Mon Sep 17 00:00:00 2001 From: bocharsky-bw Date: Wed, 16 Jun 2021 23:16:01 +0300 Subject: [PATCH 1/2] Suppress getMasterRequest() deprecation message --- Legacy/LegacyHelper.php | 31 ++++++++++++++++++++++++++++ Translator/EditInPlaceTranslator.php | 7 +++++-- Twig/EditInPlaceExtension.php | 5 ++++- 3 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 Legacy/LegacyHelper.php diff --git a/Legacy/LegacyHelper.php b/Legacy/LegacyHelper.php new file mode 100644 index 00000000..ef93a926 --- /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'] : []; } /** From 3e8400ce77c7858932338e95a706ca7e22ed0cc9 Mon Sep 17 00:00:00 2001 From: bocharsky-bw Date: Wed, 16 Jun 2021 23:25:16 +0300 Subject: [PATCH 2/2] Apply PHP CS Fixer changes --- Legacy/LegacyHelper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Legacy/LegacyHelper.php b/Legacy/LegacyHelper.php index ef93a926..1b334915 100644 --- a/Legacy/LegacyHelper.php +++ b/Legacy/LegacyHelper.php @@ -14,7 +14,7 @@ use Symfony\Component\HttpFoundation\RequestStack; /** - * A legacy helper to suppress deprecations on RequestStack + * A legacy helper to suppress deprecations on RequestStack. * * @author Victor Bocharsky */ @@ -22,7 +22,7 @@ class LegacyHelper { public static function getMainRequest(RequestStack $requestStack) { - if (method_exists($requestStack, 'getMainRequest')) { + if (\method_exists($requestStack, 'getMainRequest')) { return $requestStack->getMainRequest(); }