Skip to content

Commit

Permalink
invoke filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Lorenz committed Dec 26, 2024
1 parent c7be388 commit d0612c9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Extension/CoreExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ public function getFilters(): array
// iteration and runtime
new TwigFilter('default', [self::class, 'default'], ['node_class' => DefaultFilter::class]),
new TwigFilter('keys', [self::class, 'keys']),
new TwigFilter('invoke', [self::class, 'invoke']),
];
}

Expand Down Expand Up @@ -915,6 +916,20 @@ public static function keys($array): array
return array_keys($array);
}

/**
* Invokes a callable
*
* @param callable $callable
* @param ...$arguments
* @return mixed
*
* @internal
*/
public static function invoke(callable $callable, ...$arguments): mixed
{
return $callable(...$arguments);
}

/**
* Reverses a variable.
*
Expand Down
14 changes: 14 additions & 0 deletions tests/Fixtures/filters/invoke.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
"invoke" filter
--TEMPLATE--
{% set func = x => 'Hello '~x %}
{{ func|invoke('World') }}
{% set func2 = (x, y) => x+y %}
{{ func2|invoke(3, 2) }}
--DATA--
return []
--CONFIG--
return []
--EXPECT--
Hello World
5

0 comments on commit d0612c9

Please sign in to comment.