[FEATURE] Support feature toggle in expression language 69/58869/5
authorFrank Naegler <frank.naegler@typo3.org>
Wed, 7 Nov 2018 22:00:23 +0000 (23:00 +0100)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Thu, 8 Nov 2018 17:43:51 +0000 (18:43 +0100)
Resolves: #86881
Releases: master
Change-Id: Ib9325081b572f22936cc28cbc1f8b977f9c1b0bf
Reviewed-on: https://review.typo3.org/58869
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/core/Classes/ExpressionLanguage/FunctionsProvider/DefaultFunctionsProvider.php
typo3/sysext/core/Documentation/Changelog/master/Feature-86881-SupportOfFeaturesInExpressionLanguage.rst [new file with mode: 0644]
typo3/sysext/core/Tests/Unit/Configuration/TypoScript/ConditionMatching/AbstractConditionMatcherTest.php

index ed9dde3..a20ee9e 100644 (file)
@@ -17,6 +17,7 @@ namespace TYPO3\CMS\Core\ExpressionLanguage\FunctionsProvider;
 
 use Symfony\Component\ExpressionLanguage\ExpressionFunction;
 use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;
+use TYPO3\CMS\Core\Configuration\Features;
 use TYPO3\CMS\Core\Context\Context;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\StringUtility;
@@ -39,6 +40,7 @@ class DefaultFunctionsProvider implements ExpressionFunctionProviderInterface
             $this->getLikeFunction(),
             $this->getEnvFunction(),
             $this->getDateFunction(),
+            $this->getFeatureToggleFunction(),
         ];
     }
 
@@ -96,4 +98,14 @@ class DefaultFunctionsProvider implements ExpressionFunctionProviderInterface
                 ->getAspect('date')->getDateTime()->format($format);
         });
     }
+
+    protected function getFeatureToggleFunction(): ExpressionFunction
+    {
+        return new ExpressionFunction('feature', function () {
+            // Not implemented, we only use the evaluator
+        }, function ($arguments, $featureName) {
+            return GeneralUtility::makeInstance(Features::class)
+                ->isFeatureEnabled($featureName);
+        });
+    }
 }
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-86881-SupportOfFeaturesInExpressionLanguage.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-86881-SupportOfFeaturesInExpressionLanguage.rst
new file mode 100644 (file)
index 0000000..9a5e625
--- /dev/null
@@ -0,0 +1,27 @@
+.. include:: ../../Includes.txt
+
+============================================================
+Feature: #86881 - Support of Features in expression language
+============================================================
+
+See :issue:`86881`
+
+Description
+===========
+
+With #83429 the core got support for feature toggles including a small API.
+This patch adds support for feature toggle check in the symfony expression language DefaultFunctionProvider.
+With the new function :typoscript:`feature()` the feature toggle can be checked.
+
+.. code-block:: typoscript
+
+   [feature("TypoScript.strictSyntax")]
+   # This condition matches if the feature toggle "TypoScript.strictSyntax" is true
+   [END]
+
+   [feature("TypoScript.strictSyntax") === false]
+   # This condition matches if the feature toggle "TypoScript.strictSyntax" is false
+   [END]
+
+
+.. index:: Backend, Frontend, TypoScript, ext:core
index 358acb0..3828e5e 100644 (file)
@@ -171,6 +171,47 @@ class AbstractConditionMatcherTest extends UnitTestCase
     }
 
     /**
+     * @test
+     */
+    public function checkConditionMatcherForFeatureFunction(): void
+    {
+        $featureName = 'test.testFeature';
+        $GLOBALS['TYPO3_CONF_VARS']['SYS']['features'][$featureName] = true;
+        $this->assertTrue(
+            $this->evaluateExpressionMethod->invokeArgs($this->conditionMatcher, ['feature("' . $featureName . '")'])
+        );
+        $this->assertTrue(
+            $this->evaluateExpressionMethod->invokeArgs($this->conditionMatcher, ['feature("' . $featureName . '") == true'])
+        );
+        $this->assertTrue(
+            $this->evaluateExpressionMethod->invokeArgs($this->conditionMatcher, ['feature("' . $featureName . '") === true'])
+        );
+        $this->assertFalse(
+            $this->evaluateExpressionMethod->invokeArgs($this->conditionMatcher, ['feature("' . $featureName . '") == false'])
+        );
+        $this->assertFalse(
+            $this->evaluateExpressionMethod->invokeArgs($this->conditionMatcher, ['feature("' . $featureName . '") === false'])
+        );
+
+        $GLOBALS['TYPO3_CONF_VARS']['SYS']['features'][$featureName] = false;
+        $this->assertFalse(
+            $this->evaluateExpressionMethod->invokeArgs($this->conditionMatcher, ['feature("' . $featureName . '")'])
+        );
+        $this->assertFalse(
+            $this->evaluateExpressionMethod->invokeArgs($this->conditionMatcher, ['feature("' . $featureName . '") == true'])
+        );
+        $this->assertFalse(
+            $this->evaluateExpressionMethod->invokeArgs($this->conditionMatcher, ['feature("' . $featureName . '") === true'])
+        );
+        $this->assertTrue(
+            $this->evaluateExpressionMethod->invokeArgs($this->conditionMatcher, ['feature("' . $featureName . '") == false'])
+        );
+        $this->assertTrue(
+            $this->evaluateExpressionMethod->invokeArgs($this->conditionMatcher, ['feature("' . $featureName . '") === false'])
+        );
+    }
+
+    /**
      * @return array
      */
     public function hostnameDataProvider(): array