From 93a7348606aba92d5d70b90369f20452a9860e7c Mon Sep 17 00:00:00 2001 From: Kijin Sung Date: Sun, 30 Oct 2022 21:41:40 +0900 Subject: [PATCH] Support $foo->$bar syntax in templates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기존: $__Context->$__Context->bar 로 변환되어 오작동 개선: $__Context->{$__Context->bar} 로 변환되어 정상 작동되며 변싱 순서에 따라 이 중괄호가 다시 해석되지는 않음 --- classes/template/TemplateHandler.class.php | 10 +++++++++- tests/unit/classes/TemplateHandlerTest.php | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/classes/template/TemplateHandler.class.php b/classes/template/TemplateHandler.class.php index eab53150d..fa7fcb9c3 100644 --- a/classes/template/TemplateHandler.class.php +++ b/classes/template/TemplateHandler.class.php @@ -1108,7 +1108,13 @@ class TemplateHandler return ''; } - return preg_replace_callback('@(?\$([a-z_][a-z0-9_]*)@i', function($matches) { + return '->{$__Context->' . $matches[1] . '}'; + }, $php); + + // Replace all other variables with Context attributes. + $php = preg_replace_callback('@(?|(?' . $matches[1]; } }, $php); + + return $php; } } diff --git a/tests/unit/classes/TemplateHandlerTest.php b/tests/unit/classes/TemplateHandlerTest.php index d2a4ba124..1bde836c5 100644 --- a/tests/unit/classes/TemplateHandlerTest.php +++ b/tests/unit/classes/TemplateHandlerTest.php @@ -315,6 +315,15 @@ class TemplateHandlerTest extends \Codeception\TestCase\Test array( '{\RX_BASEDIR}', '?>' + ), + // Rhymix improvements: object attributes enclosed in curly braces + array( + '
{$foo->$bar[$bazz]}
', + '?>
foo->{$__Context->bar}[$__Context->bazz] ?>
' + ), + array( + '
', + "\n" . 'if($__Context->foo->{$__Context->bar}){ ?>
' ), // Rhymix autoescape array(