Skip to content

Commit

Permalink
adds support for invoking closures
Browse files Browse the repository at this point in the history
- fixes twigphp#3848
  • Loading branch information
faizanakram99 committed Dec 12, 2024
1 parent 77376c9 commit 12c41d3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down
7 changes: 7 additions & 0 deletions tests/TemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,13 @@ public static function getGetAttributeTests()
[true, ['foo' => 'bar'], $arrayAccess, 'vars', [], $anyType],
]);

// test for Closure::__invoke()
$closure = function (): string {
return 'closure called';
};

$tests[] = [true, 'closure called', $closure, '__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".'],
Expand Down

0 comments on commit 12c41d3

Please sign in to comment.