Function.php 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2010 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. /**
  11. * Represents a template function.
  12. *
  13. * @package twig
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. abstract class Twig_Function implements Twig_FunctionInterface
  17. {
  18. protected $options;
  19. public function __construct(array $options = array())
  20. {
  21. $this->options = array_merge(array(
  22. 'needs_environment' => false,
  23. 'needs_context' => false,
  24. ), $options);
  25. }
  26. public function needsEnvironment()
  27. {
  28. return $this->options['needs_environment'];
  29. }
  30. public function needsContext()
  31. {
  32. return $this->options['needs_context'];
  33. }
  34. public function getSafe(Twig_Node $functionArgs)
  35. {
  36. if (isset($this->options['is_safe'])) {
  37. return $this->options['is_safe'];
  38. }
  39. if (isset($this->options['is_safe_callback'])) {
  40. return call_user_func($this->options['is_safe_callback'], $functionArgs);
  41. }
  42. return array();
  43. }
  44. }