Skip to content

Commit 993b77a

Browse files
authored
[TASK] Allow TemplateValidator to discover underscore variables (#1283)
Fluid 5 disallows variable names that start with an underscore. While this is already enforced in `StandardVariableProvider` for setting such variables, this change now also triggers an exception if a variable with an invalid name is accessed. This is probably a temporary change to be removed with Fluid 6. For now, it allows the `TemplateValidator`, which is used during template warmup, to discover this issue during parse time, and to let users know about affected templates. Resolves: #1263
1 parent ae55cbd commit 993b77a

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

src/Core/Parser/SyntaxTree/ObjectAccessorNode.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use TYPO3Fluid\Fluid\Core\Compiler\TemplateCompiler;
1313
use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface;
14+
use TYPO3Fluid\Fluid\Core\Variables\InvalidVariableIdentifierException;
1415

1516
/**
1617
* A node which handles object access. This means it handles structures like {object.accessor.bla}
@@ -34,6 +35,12 @@ final class ObjectAccessorNode extends AbstractNode
3435
*/
3536
public function __construct(string $objectPath)
3637
{
38+
// TODO consider removing this exception in later Fluid versions. For now it allows the
39+
// TemplateValidator to find occurrances of variables starting with "_" in templates,
40+
// which are no longer allowed since Fluid 5.
41+
if (str_starts_with($objectPath, '_') && $objectPath !== '_all') {
42+
throw new InvalidVariableIdentifierException('Variable identifiers cannot start with a "_": ' . $objectPath, 1765900762);
43+
}
3744
$this->objectPath = $objectPath;
3845
}
3946

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{_something.sub}

tests/Functional/Validation/TemplateValidatorTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static function validatorFindsParseErrorsDataProvider(): array
2626
[$fixturePath . 'InvalidNamespace.fluid.html', 'Unknown Namespace: foo', 0],
2727
[$fixturePath . 'RequiredArgumentMissing.fluid.html', 'Required argument "each" was not supplied.', 1237823699],
2828
[$fixturePath . 'RedefinedComponentArgument.fluid.html', 'Template argument "foo" has been defined multiple times.', 1744908509],
29+
[$fixturePath . 'AccessToInvalidVariableName.fluid.html', 'Variable identifiers cannot start with a "_": _something.sub', 1765900762],
2930
];
3031
}
3132

0 commit comments

Comments
 (0)