Function.php 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. class Twig_Node_Expression_Function extends Twig_Node_Expression
  11. {
  12. public function __construct($name, Twig_NodeInterface $arguments, $lineno)
  13. {
  14. parent::__construct(array('arguments' => $arguments), array('name' => $name), $lineno);
  15. }
  16. public function compile(Twig_Compiler $compiler)
  17. {
  18. $function = $compiler->getEnvironment()->getFunction($this->getAttribute('name'));
  19. if (false === $function) {
  20. throw new Twig_Error_Syntax(sprintf('The function "%s" does not exist', $this->getAttribute('name')), $this->getLine());
  21. }
  22. $compiler
  23. ->raw($function->compile().'(')
  24. ->raw($function->needsEnvironment() ? '$this->env' : '')
  25. ;
  26. if ($function->needsContext()) {
  27. $compiler->raw($function->needsEnvironment() ? ', $context' : '$context');
  28. }
  29. $first = true;
  30. foreach ($this->getNode('arguments') as $node) {
  31. if (!$first) {
  32. $compiler->raw(', ');
  33. } else {
  34. if ($function->needsEnvironment() || $function->needsContext()) {
  35. $compiler->raw(', ');
  36. }
  37. $first = false;
  38. }
  39. $compiler->subcompile($node);
  40. }
  41. $compiler->raw(')');
  42. }
  43. }