From ba9b9681ca261621a52994c1af794728290b583e Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 16 May 2019 13:58:43 +0200 Subject: [PATCH 1/2] fixed a PHP fatal error when calling a macro, imported in the template, in another macro --- CHANGELOG | 1 + src/Parser.php | 6 +----- .../Fixtures/tags/macro/macro_in_a_macro.test | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) create mode 100644 test/Twig/Tests/Fixtures/tags/macro/macro_in_a_macro.test diff --git a/CHANGELOG b/CHANGELOG index 32ce8d9f4..951671e50 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.41.1 (2019-XX-XX) + * fixed a PHP fatal error when calling a macro imported in the template in another macro * fixed wrong error message on "import" and "from" * 1.41.0 (2019-05-14) diff --git a/src/Parser.php b/src/Parser.php index 444d4c5a1..58e2e2bb9 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -334,11 +334,7 @@ class Parser implements \Twig_ParserInterface public function getImportedSymbol($type, $alias) { - foreach ($this->importedSymbols as $functions) { - if (isset($functions[$type][$alias])) { - return $functions[$type][$alias]; - } - } + return isset($this->importedSymbols[0][$type][$alias]) ? $this->importedSymbols[0][$type][$alias] : null; } public function isMainScope() diff --git a/test/Twig/Tests/Fixtures/tags/macro/macro_in_a_macro.test b/test/Twig/Tests/Fixtures/tags/macro/macro_in_a_macro.test new file mode 100644 index 000000000..293d91145 --- /dev/null +++ b/test/Twig/Tests/Fixtures/tags/macro/macro_in_a_macro.test @@ -0,0 +1,15 @@ +--TEST-- +"from" tag with syntax error +--TEMPLATE-- +{% from _self import another %} + +{% macro foo() %} + {{ another() }} +{% endmacro %} + +{% macro another() %} +{% endmacro %} +--DATA-- +return [] +--EXCEPTION-- +Twig\Error\SyntaxError: Unknown "another" function in "index.twig" at line 5. From b0f29468f0a66455ebf351ac8f28c9644a9e4605 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 16 May 2019 14:16:39 +0200 Subject: [PATCH 2/2] simplified code --- src/Parser.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Parser.php b/src/Parser.php index 62b40ef7d..2e57a615e 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -279,7 +279,7 @@ class Parser public function getImportedSymbol($type, $alias) { - return isset($this->importedSymbols[0][$type][$alias]) ? $this->importedSymbols[0][$type][$alias] : null; + return $this->importedSymbols[0][$type][$alias] ?? null; } public function isMainScope()