Skip to content

Commit

Permalink
[BUGFIX] Choose between global/local variable based on primary identi…
Browse files Browse the repository at this point in the history
…fier

With the previous implementation of ScopedVariableProvider,
variables that contained arrays or object structures could behave
incorrectly in a scoped context. When accessing a object path
in a fluid template (e. g. myVar.subKey), the value of that
"sub variable" was considered for the decision which variable
(global or local) should be used.

This behavior is incorrect, only the "primary" variable name
should determine if a local variable is set or not (and should
fall back to a global variable). This would mean that a non-existing
sub value in a local variable would trigger a fallback to the
global variable.

This change restores the correct behavior: Only the "primary"
variable name is checked initially. If the variable exists in the
local variables, those will be used, even if the requested sub value
doesn't exist there. Only if the "primary" variable name doesn't
exist in local variables, global variables will be used instead.

Resolves #848
  • Loading branch information
s2b committed Dec 9, 2023
1 parent d521f00 commit 5d2b9e7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Core/Variables/ScopedVariableProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ public function get($identifier): mixed
public function getByPath($path): mixed
{
$path = $this->resolveSubVariableReferences($path);
return $this->localVariables->getByPath($path) ?? $this->globalVariables->getByPath($path);
$identifier = explode('.', $path, 2)[0];
return $this->localVariables->exists($identifier)
? $this->localVariables->getByPath($path)
: $this->globalVariables->getByPath($path);
}

public function getAllIdentifiers(): array
Expand Down
7 changes: 7 additions & 0 deletions tests/Unit/Core/Variables/ScopedVariableProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,13 @@ public static function getVariableByPathDataProvider(): \Generator
'local',
];

yield 'local undefined subkey overrides global set subkey' => [
['myVar' => ['myKey' => 'global']],
['myVar' => ['myKey' => null]],
'myVar.myKey',
null,
];

yield 'variable variables using only globals' => [
['myVar' => ['sub' => 'global'], 'path' => 'sub'],
[],
Expand Down

0 comments on commit 5d2b9e7

Please sign in to comment.