vendor/twig/twig/src/TwigFunction.php line 31

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Twig;
  11. use Twig\Node\Expression\FunctionExpression;
  12. use Twig\Node\Node;
  13. /**
  14. * Represents a template function.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. *
  18. * @see https://twig.symfony.com/doc/templates.html#functions
  19. */
  20. final class TwigFunction extends AbstractTwigCallable
  21. {
  22. /**
  23. * @param callable|array{class-string, string}|null $callable A callable implementing the function. If null, you need to overwrite the "node_class" option to customize compilation.
  24. */
  25. public function __construct(string $name, $callable = null, array $options = [])
  26. {
  27. parent::__construct($name, $callable, $options);
  28. $this->options = array_merge([
  29. 'is_safe' => null,
  30. 'is_safe_callback' => null,
  31. 'node_class' => FunctionExpression::class,
  32. 'parser_callable' => null,
  33. ], $this->options);
  34. }
  35. public function getType(): string
  36. {
  37. return 'function';
  38. }
  39. public function getParserCallable(): ?callable
  40. {
  41. return $this->options['parser_callable'];
  42. }
  43. public function getSafe(Node $functionArgs): ?array
  44. {
  45. if (null !== $this->options['is_safe']) {
  46. return $this->options['is_safe'];
  47. }
  48. if (null !== $this->options['is_safe_callback']) {
  49. return $this->options['is_safe_callback']($functionArgs);
  50. }
  51. return [];
  52. }
  53. }