Function.php 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. $name = $this->getAttribute('name');
  19. if (false === $function = $compiler->getEnvironment()->getFunction($name)) {
  20. $message = sprintf('The function "%s" does not exist', $name);
  21. if ($alternatives = $compiler->getEnvironment()->computeAlternatives($name, array_keys($compiler->getEnvironment()->getFunctions()))) {
  22. $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives));
  23. }
  24. throw new Twig_Error_Syntax($message, $this->getLine());
  25. }
  26. $compiler->raw($function->compile().'(');
  27. $first = true;
  28. if ($function->needsEnvironment()) {
  29. $compiler->raw('$this->env');
  30. $first = false;
  31. }
  32. if ($function->needsContext()) {
  33. if (!$first) {
  34. $compiler->raw(', ');
  35. }
  36. $compiler->raw('$context');
  37. $first = false;
  38. }
  39. foreach ($function->getArguments() as $argument) {
  40. if (!$first) {
  41. $compiler->raw(', ');
  42. }
  43. $compiler->string($argument);
  44. $first = false;
  45. }
  46. foreach ($this->getNode('arguments') as $node) {
  47. if (!$first) {
  48. $compiler->raw(', ');
  49. }
  50. $compiler->subcompile($node);
  51. $first = false;
  52. }
  53. $compiler->raw(')');
  54. }
  55. }