GetAttr.php 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. * (c) 2009 Armin Ronacher
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. class Twig_Node_Expression_GetAttr extends Twig_Node_Expression
  12. {
  13. public function __construct(Twig_Node_Expression $node, Twig_Node_Expression $attribute, Twig_NodeInterface $arguments, $type, $lineno)
  14. {
  15. parent::__construct(array('node' => $node, 'attribute' => $attribute, 'arguments' => $arguments), array('type' => $type), $lineno);
  16. }
  17. public function compile(Twig_Compiler $compiler)
  18. {
  19. $compiler->raw('$this->getAttribute(');
  20. if ($this->hasAttribute('is_defined_test') && $compiler->getEnvironment()->isStrictVariables()) {
  21. $compiler->subcompile(new Twig_Node_Expression_Filter(
  22. $this->getNode('node'),
  23. new Twig_Node_Expression_Constant('default', $this->getLine()),
  24. new Twig_Node(),
  25. $this->getLine()
  26. ));
  27. } else {
  28. $compiler->subcompile($this->getNode('node'));
  29. }
  30. $compiler
  31. ->raw(', ')
  32. ->subcompile($this->getNode('attribute'))
  33. ->raw(', array(')
  34. ;
  35. foreach ($this->getNode('arguments') as $node) {
  36. $compiler
  37. ->subcompile($node)
  38. ->raw(', ')
  39. ;
  40. }
  41. $compiler
  42. ->raw('), ')
  43. ->repr($this->getAttribute('type'))
  44. ->raw($this->hasAttribute('is_defined_test') ? ', true' : ', false')
  45. ->raw(')');
  46. }
  47. }