From 657f384aad6422c1756f44daa650517254057376 Mon Sep 17 00:00:00 2001
From: Christian Kuhn <lolli@schwarzbu.ch>
Date: Tue, 18 Sep 2018 21:57:30 +0200
Subject: [PATCH] Revert "[TASK] Improve TranslateViewHelper compiling"

The micro optimization introduced a bug, fixing the bug
would basically lead to the same code that is within
renderStatic() already. There is then no advantage in
having an inline compiled solution in comparison to a
static call to the class anymore. The initial patch and
its follow up are reverted.

This reverts commit e6f470ebd2ddac7f0c864fd7c18c3d12a9009c30.
This reverts commit 852ad1d9ad6f3bac4c977d2466d1875f9e632e01.

Resolves: #86292
Reverts: #82454
Releases: master
Change-Id: I573a4298b0b9aedf83d51416919204bf910ae4bf
Reviewed-on: https://review.typo3.org/58322
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
Reviewed-by: Frank Naegler <frank.naegler@typo3.org>
Tested-by: Frank Naegler <frank.naegler@typo3.org>
---
 .../ViewHelpers/TranslateViewHelper.php       | 70 ++++++++-----------
 ...anslateViewHelperFixtureForEmptyString.php |  2 +-
 2 files changed, 29 insertions(+), 43 deletions(-)

diff --git a/typo3/sysext/fluid/Classes/ViewHelpers/TranslateViewHelper.php b/typo3/sysext/fluid/Classes/ViewHelpers/TranslateViewHelper.php
index c12c5849db7b..dedb94b2c22e 100644
--- a/typo3/sysext/fluid/Classes/ViewHelpers/TranslateViewHelper.php
+++ b/typo3/sysext/fluid/Classes/ViewHelpers/TranslateViewHelper.php
@@ -16,9 +16,9 @@ namespace TYPO3\CMS\Fluid\ViewHelpers;
 
 use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
 use TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException;
-use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
-use TYPO3Fluid\Fluid\Core\Parser\SyntaxTree\ViewHelperNode;
+use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
 use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
+use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithRenderStatic;
 
 /**
  * Translate a key from locallang. The files are loaded from the folder
@@ -73,6 +73,8 @@ use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper;
  */
 class TranslateViewHelper extends AbstractViewHelper
 {
+    use CompileWithRenderStatic;
+
     /**
      * Output is escaped already. We must not escape children, to avoid double encoding.
      *
@@ -97,59 +99,43 @@ class TranslateViewHelper extends AbstractViewHelper
     }
 
     /**
-     * @param string $argumentsName
-     * @param string $closureName
-     * @param string $initializationPhpCode
-     * @param ViewHelperNode $node
-     * @param TemplateCompiler $compiler
+     * Return array element by key.
+     *
+     * @param array $arguments
+     * @param \Closure $renderChildrenClosure
+     * @param RenderingContextInterface $renderingContext
+     * @throws InvalidVariableException
      * @return string
      */
-    public function compile($argumentsName, $closureName, &$initializationPhpCode, ViewHelperNode $node, TemplateCompiler $compiler)
-    {
-        return sprintf(
-            '\\%1$s::translate(%2$s[\'key\'] ?? %2$s[\'id\'], %2$s[\'extensionName\'] ?? $renderingContext->getControllerContext()->getRequest()->getControllerExtensionName(), %2$s[\'arguments\'], %2$s[\'languageKey\'], %2$s[\'alternativeLanguageKeys\']) ?? %2$s[\'default\'] ?? %3$s()',
-            LocalizationUtility::class,
-            $argumentsName,
-            $closureName
-        );
-    }
-
-    /**
-     * @return string|null
-     */
-    public function render()
+    public static function renderStatic(array $arguments, \Closure $renderChildrenClosure, RenderingContextInterface $renderingContext)
     {
-        $key = $this->arguments['key'];
-        $id = $this->arguments['id'];
-        $default = $this->arguments['default'];
-        $extensionName = $this->arguments['extensionName'];
-        $arguments = $this->arguments['arguments'];
-        $languageKey = $this->arguments['languageKey'];
-        $alternativeLanguageKeys = $this->arguments['alternativeLanguageKeys'];
-
-        if (empty($id) && empty($key)) {
-            throw new InvalidVariableException('Either "key" or "id" must be provided for f:translate', 1351584844);
-        }
+        $key = $arguments['key'];
+        $id = $arguments['id'];
+        $default = $arguments['default'];
+        $extensionName = $arguments['extensionName'];
+        $translateArguments = $arguments['arguments'];
 
         // Wrapper including a compatibility layer for TYPO3 Flow Translation
         if ($id === null) {
             $id = $key;
         }
 
-        $request = $this->renderingContext->getControllerContext()->getRequest();
+        if ((string)$id === '') {
+            throw new InvalidVariableException('An argument "key" or "id" has to be provided', 1351584844);
+        }
+
+        $request = $renderingContext->getControllerContext()->getRequest();
+        $extensionName = $extensionName ?? $request->getControllerExtensionName();
         try {
-            $value = $this->translate(
-                $key ?? $id,
-                $extensionName ?? $request->getControllerExtensionName(),
-                $arguments,
-                $languageKey,
-                $alternativeLanguageKeys
-            );
+            $value = static::translate($id, $extensionName, $translateArguments, $arguments['languageKey'], $arguments['alternativeLanguageKeys']);
         } catch (\InvalidArgumentException $e) {
             $value = null;
         }
         if ($value === null) {
-            $value = $default !== null ? $default : $this->renderChildren();
+            $value = $default ?? $renderChildrenClosure();
+            if (!empty($translateArguments)) {
+                $value = vsprintf($value, $translateArguments);
+            }
         }
         return $value;
     }
@@ -165,7 +151,7 @@ class TranslateViewHelper extends AbstractViewHelper
      *
      * @return string|null
      */
-    protected function translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys)
+    protected static function translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys)
     {
         return LocalizationUtility::translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys);
     }
diff --git a/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Fixtures/TranslateViewHelperFixtureForEmptyString.php b/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Fixtures/TranslateViewHelperFixtureForEmptyString.php
index 2e82c38a15b6..614cd9f16df6 100644
--- a/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Fixtures/TranslateViewHelperFixtureForEmptyString.php
+++ b/typo3/sysext/fluid/Tests/Unit/ViewHelpers/Fixtures/TranslateViewHelperFixtureForEmptyString.php
@@ -29,7 +29,7 @@ class TranslateViewHelperFixtureForEmptyString extends TranslateViewHelper
      * @param string $languageKey Language key to use for this translation
      * @param string[] $alternativeLanguageKeys Alternative language keys if no translation does exist
      */
-    protected function translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys)
+    protected static function translate($id, $extensionName, $arguments, $languageKey, $alternativeLanguageKeys)
     {
         return null;
     }
-- 
GitLab