[FEATURE] support condition in INCLUDE_TYPOSCRIPT tag 31/42431/10
authorJigal van Hemert <jigal.van.hemert@typo3.org>
Sat, 8 Aug 2015 23:32:15 +0000 (01:32 +0200)
committerAnja Leichsenring <aleichsenring@ab-softlab.de>
Tue, 15 Sep 2015 08:10:00 +0000 (10:10 +0200)
An optional property 'condition' will only include the file or directory
if the condition is met.

Resolves: #16525
Releases: master
Change-Id: I4be8b93d733ef40601a2943ebad357c095f997ad
Reviewed-on: http://review.typo3.org/42431
Reviewed-by: Daniel Goerz <ervaude@gmail.com>
Tested-by: Daniel Goerz <ervaude@gmail.com>
Reviewed-by: Oliver Thiele <mail@oliver-thiele.de>
Tested-by: Oliver Thiele <mail@oliver-thiele.de>
Reviewed-by: Xavier Perseguers <xavier@typo3.org>
Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de>
typo3/sysext/core/Classes/TypoScript/Parser/TypoScriptParser.php
typo3/sysext/core/Documentation/Changelog/master/Feature-16525-AddConditionsToINCLUDE_TYPOSCRIPT.rst [new file with mode: 0644]

index 842400e..0a8871c 100644 (file)
@@ -20,6 +20,7 @@ use TYPO3\CMS\Core\Utility\MathUtility;
 use TYPO3\CMS\Core\Utility\PathUtility;
 use TYPO3\CMS\Core\Utility\StringUtility;
 use TYPO3\CMS\Core\TypoScript\ExtendedTemplateService;
+use TYPO3\CMS\Backend\Configuration\TypoScript\ConditionMatching\ConditionMatcher;
 
 /**
  * The TypoScript parser
@@ -819,6 +820,24 @@ class TypoScriptParser {
                                $optionalProperties = $parts[$i + 2];
                                $tsContentsTillNextInclude = $parts[$i + 3];
 
+                               // Check condition
+                               $matches = preg_split('#(?i)condition\\s*=\\s*"((?:\\\\\\\\|\\\\"|[^\\"])*)"(\\s*|>)#', $optionalProperties, 2, PREG_SPLIT_DELIM_CAPTURE);
+                               // If there was a condition
+                               if (count($matches) > 1) {
+                                       // Unescape the condition
+                                       $condition = trim(stripslashes($matches[1]));
+                                       // If necessary put condition in square brackets
+                                       if ($condition[0] !== '[') {
+                                               $condition = '[' . $condition . ']';
+                                       }
+                                       /** @var ConditionMatcher $conditionMatcher */
+                                       $conditionMatcher = GeneralUtility::makeInstance(ConditionMatcher::class);
+                                       // If it didn't match then proceed to the next include
+                                       if (!$conditionMatcher->match($condition)) {
+                                               continue;
+                                       }
+                               }
+
                                // Resolve a possible relative paths if a parent file is given
                                if ($parentFilenameOrPath !== '' && $filename[0] === '.') {
                                        $filename = PathUtility::getAbsolutePathOfRelativeReferencedFileOrPath($parentFilenameOrPath, $filename);
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-16525-AddConditionsToINCLUDE_TYPOSCRIPT.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-16525-AddConditionsToINCLUDE_TYPOSCRIPT.rst
new file mode 100644 (file)
index 0000000..27dab4a
--- /dev/null
@@ -0,0 +1,28 @@
+======================================================
+Feature: #16525 - Add conditions to INCLUDE_TYPOSCRIPT
+======================================================
+
+Description
+===========
+
+The INCLUDE_TYPOSCRIPT tag now has an extra (optional) property "condition" which causes the file/directory to be included only
+if the condition is met.
+
+As usual a condition is enclosed in square brackets, but if these are not present they will be added. Any double quotes must be
+escaped by adding backslashes and any backslash must be doubled.
+
+Example
+-------
+
+.. code-block:: typoscript
+
+       <INCLUDE_TYPOSCRIPT: source="FILE:EXT:my_extension/Configuration/TypoScript/firefox.ts" condition="[loginUser = *]">
+
+Condition with square brackets. File will only be included if a frontend user is logged in.
+
+.. code-block:: typoscript
+
+       <INCLUDE_TYPOSCRIPT: source="FILE:EXT:my_extension/Configuration/TypoScript/staging.ts" condition="applicationContext = /^Production\\/Staging\\/Server\\d+$/">
+
+Condition without square brackets, backslashes doubled inside the condition. File will only be included in application context
+Production/Staging/Server followed by at least one digit.