From 535a1123f3e9b658236e0290cdcf3903715093bd Mon Sep 17 00:00:00 2001 From: Faizan Akram Date: Thu, 12 Dec 2024 14:42:49 +0100 Subject: [PATCH] adds support for invoking closures - fixes #3848 - adds changelog entry --- CHANGELOG | 1 + src/Extension/CoreExtension.php | 3 +++ tests/TemplateTest.php | 3 +++ 3 files changed, 7 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index b0520220415..f8e055b9a2a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ * Fix the null coalescing operator when the test returns null * Fix the Elvis operator when used as '? :' instead of '?:' + * Support for invoking closures # 3.17.0 (2024-12-10) diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index b60944c4a4e..d3d637c05e4 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -1777,6 +1777,9 @@ public static function getAttribute(Environment $env, Source $source, $object, $ // precedence: getXxx() > isXxx() > hasXxx() if (!isset($cache[$class])) { $methods = get_class_methods($object); + if ($object instanceof \Closure) { + $methods[] = '__invoke'; + } sort($methods); $lcMethods = array_map('strtolower', $methods); $classCache = []; diff --git a/tests/TemplateTest.php b/tests/TemplateTest.php index 4d2489962f7..811f4f5deb7 100644 --- a/tests/TemplateTest.php +++ b/tests/TemplateTest.php @@ -394,6 +394,9 @@ public static function getGetAttributeTests() [true, ['foo' => 'bar'], $arrayAccess, 'vars', [], $anyType], ]); + // test for Closure::__invoke() + $tests[] = [true, 'closure called', fn (): string => 'closure called', '__invoke', [], $anyType]; + // tests when input is not an array or object $tests = array_merge($tests, [ [false, null, 42, 'a', [], $anyType, 'Impossible to access an attribute ("a") on a int variable ("42") in "index.twig".'],