From 5b8a5cc9078229152a2cd7a067b7ea883d4f6acb Mon Sep 17 00:00:00 2001 From: Simon Praetorius Date: Mon, 11 Dec 2023 13:48:56 +0100 Subject: [PATCH] [BUGFIX] Explicitly set variables override local variables (#849) --- Documentation/Usage/Syntax.rst | 12 ++++++++++++ src/Core/Variables/ScopedVariableProvider.php | 1 + tests/Functional/ViewHelpers/ForViewHelperTest.php | 7 +++++++ .../Core/Variables/ScopedVariableProviderTest.php | 2 +- 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Documentation/Usage/Syntax.rst b/Documentation/Usage/Syntax.rst index b98f134f4..d54a0da16 100644 --- a/Documentation/Usage/Syntax.rst +++ b/Documentation/Usage/Syntax.rst @@ -200,3 +200,15 @@ the scope: + +If a global variable is created inside a local scope and uses the same name as a local +variable, it will still leak out of the scope and will also be valid inside the scope: + +.. code-block:: xml + + + + + + + diff --git a/src/Core/Variables/ScopedVariableProvider.php b/src/Core/Variables/ScopedVariableProvider.php index 7c1ce2e08..f5ee20fad 100644 --- a/src/Core/Variables/ScopedVariableProvider.php +++ b/src/Core/Variables/ScopedVariableProvider.php @@ -42,6 +42,7 @@ public function getLocalVariableProvider(): VariableProviderInterface public function add($identifier, $value): void { $this->globalVariables->add($identifier, $value); + $this->localVariables->add($identifier, $value); } /** diff --git a/tests/Functional/ViewHelpers/ForViewHelperTest.php b/tests/Functional/ViewHelpers/ForViewHelperTest.php index e03cdacac..f7b9ed3e3 100644 --- a/tests/Functional/ViewHelpers/ForViewHelperTest.php +++ b/tests/Functional/ViewHelpers/ForViewHelperTest.php @@ -178,6 +178,13 @@ public static function renderDataProvider(): \Generator 'overwritten', ]; + $value = ['bar', 2]; + yield 'local variables can be converted to global variables inside loop' => [ + '{item}|{item}|{item}', + ['value' => $value], + 'bar|overwritten|2|overwritten|overwritten', + ]; + $value = ['bar', 2]; yield 'variables set inside loop can be used after loop' => [ '{foo}', diff --git a/tests/Unit/Core/Variables/ScopedVariableProviderTest.php b/tests/Unit/Core/Variables/ScopedVariableProviderTest.php index 4be59ba05..8fe21d27a 100644 --- a/tests/Unit/Core/Variables/ScopedVariableProviderTest.php +++ b/tests/Unit/Core/Variables/ScopedVariableProviderTest.php @@ -283,7 +283,7 @@ public function addVariable() $variableProvider = new ScopedVariableProvider(new StandardVariableProvider(), new StandardVariableProvider()); $variableProvider->add('globalVar', 'global'); self::assertEquals('global', $variableProvider->getGlobalVariableProvider()->get('globalVar')); - self::assertNull($variableProvider->getLocalVariableProvider()->get('globalVar')); + self::assertEquals('global', $variableProvider->getLocalVariableProvider()->get('globalVar')); } /**