diff --git a/src/Extension/CoreExtension.php b/src/Extension/CoreExtension.php index 01e72856690..5b900877b66 100644 --- a/src/Extension/CoreExtension.php +++ b/src/Extension/CoreExtension.php @@ -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']), ]; } @@ -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. * diff --git a/tests/Fixtures/filters/invoke.test b/tests/Fixtures/filters/invoke.test new file mode 100644 index 00000000000..b4a707b97a7 --- /dev/null +++ b/tests/Fixtures/filters/invoke.test @@ -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