Skip to content

Commit 90d0ff4

Browse files
[5.x] Corrects Antlers error logging with PHP nodes (#11800)
1 parent 116f214 commit 90d0ff4

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

src/View/Antlers/Language/Runtime/GlobalRuntimeState.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ public static function mergeTagRuntimeAssignments($assignments)
216216

217217
public static function resetGlobalState()
218218
{
219+
self::$templateFileStack = [];
219220
self::$shareVariablesTemplateTrigger = '';
220221
self::$layoutVariables = [];
221222
self::$containsLayout = false;

src/View/Antlers/Language/Runtime/NodeProcessor.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,7 +1218,9 @@ public function reduce($processNodes)
12181218
'User content Antlers PHP tag.'
12191219
);
12201220
} else {
1221-
Log::warning('PHP Node evaluated in user content: '.$node->name->name, [
1221+
$logContent = $node->rawStart.$node->innerContent().$node->rawEnd;
1222+
1223+
Log::warning('PHP Node evaluated in user content: '.$logContent, [
12221224
'file' => GlobalRuntimeState::$currentExecutionFile,
12231225
'trace' => GlobalRuntimeState::$templateFileStack,
12241226
'content' => $node->innerContent(),
@@ -2456,7 +2458,7 @@ protected function evaluatePhp($buffer)
24562458

24572459
protected function evaluateAntlersPhpNode(PhpExecutionNode $node)
24582460
{
2459-
if (! GlobalRuntimeState::$allowPhpInContent == false && GlobalRuntimeState::$isEvaluatingUserData) {
2461+
if (! GlobalRuntimeState::$allowPhpInContent && GlobalRuntimeState::$isEvaluatingUserData) {
24602462
return StringUtilities::sanitizePhp($node->content);
24612463
}
24622464

tests/Antlers/Runtime/PhpEnabledTest.php

Lines changed: 78 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
namespace Tests\Antlers\Runtime;
44

5+
use Illuminate\Support\Facades\Log;
56
use PHPUnit\Framework\Attributes\Test;
7+
use Statamic\Fields\Field;
68
use Statamic\Fields\Fieldtype;
79
use Statamic\Fields\Value;
10+
use Statamic\Fieldtypes\Text;
11+
use Statamic\View\Antlers\Language\Runtime\GlobalRuntimeState;
812
use Statamic\View\Antlers\Language\Runtime\RuntimeConfiguration;
913
use Statamic\View\Antlers\Language\Utilities\StringUtilities;
1014
use Tests\Antlers\ParserTestCase;
@@ -513,8 +517,8 @@ public function test_php_node_assignments_within_loops()
513517
public function test_assignments_from_php_nodes()
514518
{
515519
$template = <<<'EOT'
516-
{{?
517-
$value_one = 100;
520+
{{?
521+
$value_one = 100;
518522
$value_two = 0;
519523
?}}
520524
@@ -533,4 +537,76 @@ public function test_assignments_from_php_nodes()
533537
$this->assertStringContainsString('<value_one: 1125>', $result);
534538
$this->assertStringContainsString('<value_two: 1025>', $result);
535539
}
540+
541+
public function test_disabled_php_echo_node_inside_user_values()
542+
{
543+
$textFieldtype = new Text();
544+
$field = new Field('text_field', [
545+
'type' => 'text',
546+
'antlers' => true,
547+
]);
548+
549+
$textContent = <<<'TEXT'
550+
Text: {{$ Str::upper('hello, world.') $}}
551+
TEXT;
552+
553+
$textFieldtype->setField($field);
554+
$value = new Value($textContent, 'text_field', $textFieldtype);
555+
556+
Log::shouldReceive('warning')
557+
->once()
558+
->with("PHP Node evaluated in user content: {{\$ Str::upper('hello, world.') \$}}", [
559+
'file' => null,
560+
'trace' => [],
561+
'content' => " Str::upper('hello, world.') ",
562+
]);
563+
564+
$result = $this->renderString('{{ text_field }}', ['text_field' => $value]);
565+
566+
$this->assertSame('Text: ', $result);
567+
568+
GlobalRuntimeState::$allowPhpInContent = true;
569+
570+
$result = $this->renderString('{{ text_field }}', ['text_field' => $value]);
571+
572+
$this->assertSame('Text: HELLO, WORLD.', $result);
573+
574+
GlobalRuntimeState::$allowPhpInContent = false;
575+
}
576+
577+
public function test_disabled_php_node_inside_user_values()
578+
{
579+
$textFieldtype = new Text();
580+
$field = new Field('text_field', [
581+
'type' => 'text',
582+
'antlers' => true,
583+
]);
584+
585+
$textContent = <<<'TEXT'
586+
Text: {{? echo Str::upper('hello, world.') ?}}
587+
TEXT;
588+
589+
$textFieldtype->setField($field);
590+
$value = new Value($textContent, 'text_field', $textFieldtype);
591+
592+
Log::shouldReceive('warning')
593+
->once()
594+
->with("PHP Node evaluated in user content: {{? echo Str::upper('hello, world.') ?}}", [
595+
'file' => null,
596+
'trace' => [],
597+
'content' => " echo Str::upper('hello, world.') ",
598+
]);
599+
600+
$result = $this->renderString('{{ text_field }}', ['text_field' => $value]);
601+
602+
$this->assertSame('Text: ', $result);
603+
604+
GlobalRuntimeState::$allowPhpInContent = true;
605+
606+
$result = $this->renderString('{{ text_field }}', ['text_field' => $value]);
607+
608+
$this->assertSame('Text: HELLO, WORLD.', $result);
609+
610+
GlobalRuntimeState::$allowPhpInContent = false;
611+
}
536612
}

0 commit comments

Comments
 (0)