Skip to content

Commit

Permalink
[5.x] Antlers: Improves logging behavior when using the ray modifier (
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnathonKoster authored Aug 22, 2024
1 parent 5aafeba commit cd5e263
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/View/Antlers/Language/Runtime/ModifierManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
class ModifierManager
{
public static $statamicModifiers = null;
public static $lastModifierName = null;
const METHOD_MODIFIER = '{__method_args}';

public static function isModifier(ParameterNode $node)
Expand All @@ -27,8 +28,15 @@ public static function isModifier(ParameterNode $node)
return array_key_exists($node->name, self::$statamicModifiers);
}

public static function clearModifierState()
{
self::$lastModifierName = null;
}

public static function guardRuntimeModifier($modifierName)
{
self::$lastModifierName = $modifierName;

if (GlobalRuntimeState::$isEvaluatingUserData) {
$guardList = GlobalRuntimeState::$bannedContentModifierPaths;
} else {
Expand Down Expand Up @@ -62,6 +70,8 @@ public static function guardRuntimeModifier($modifierName)

public static function evaluate($value, Environment $env, ModifierChainNode $modifierChain, $context)
{
self::clearModifierState();

if (count($modifierChain->modifierChain) == 0) {
return $value;
}
Expand Down
15 changes: 13 additions & 2 deletions src/View/Antlers/Language/Runtime/NodeProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,15 @@ protected function isInternalTagLike(AntlersNode $node)
return false;
}

private function shouldReportArrayToStringWarning(): bool
{
if (ModifierManager::$lastModifierName === 'ray') {
return false;
}

return true;
}

/**
* Tests if the runtime should continue processing the node/value combination.
*
Expand All @@ -810,8 +819,10 @@ private function guardRuntime(AntlersNode $node, $value)

return false;
} elseif ($this->isInterpolationProcessor == false && $this->isLoopable($value) && $node->isClosedBy == null) {
$varName = $node->name->getContent();
Log::debug("Cannot render an array variable as a string: {{ {$varName} }}", $this->getErrorLogContext($node));
if ($this->shouldReportArrayToStringWarning()) {
$varName = $node->name->getContent();
Log::debug("Cannot render an array variable as a string: {{ {$varName} }}", $this->getErrorLogContext($node));
}

return false;
} elseif (is_object($value) && $node->isClosedBy == null) {
Expand Down
2 changes: 2 additions & 0 deletions src/View/Antlers/Language/Runtime/Sandbox/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -1249,6 +1249,8 @@ private function convertToRuntimeNumeric($value, $name)
*/
private function scopeValue($name, $originalNode = null)
{
ModifierManager::clearModifierState();

if (! empty(GlobalRuntimeState::$prefixState)) {
$this->dataRetriever->setHandlePrefixes(array_reverse(GlobalRuntimeState::$prefixState));
}
Expand Down
25 changes: 25 additions & 0 deletions tests/Antlers/Runtime/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,31 @@ public function unclosed_array_variable_pairs_should_be_null()
$this->assertEquals('', $this->renderString($template, $this->variables));
}

#[Test]
public function unclosed_array_variable_does_not_report_warning_if_followed_by_ray_modifier()
{
Log::shouldReceive('debug')->never();

$template = '{{ simple | ray }}';

$this->assertEquals('', $this->renderString($template, $this->variables));
}

#[Test]
public function unclosed_array_variable_does_reports_warning_even_if_a_call_before_it_did_not()
{
// Test case to ensure the modifier state is cleared correctly.

Log::shouldReceive('debug')->once()
->with('Cannot render an array variable as a string: {{ simple }}', [
'line' => 1, 'file' => '',
]);

$template = '{{ simple | ray }}{{ simple }}';

$this->assertEquals('', $this->renderString($template, $this->variables));
}

#[Test]
public function single_condition()
{
Expand Down

0 comments on commit cd5e263

Please sign in to comment.