GetAttr.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\twig;
  8. class GetAttr extends \Twig_Node_Expression
  9. {
  10. /**
  11. * @inheritdoc
  12. */
  13. public function compile(\Twig_Compiler $compiler)
  14. {
  15. $compiler->raw(Template::class.'::attribute($this->env, $this->getSourceContext(), ');
  16. if ($this->getAttribute('ignore_strict_check')) {
  17. $this->getNode('node')->setAttribute('ignore_strict_check', true);
  18. }
  19. $compiler->subcompile($this->getNode('node'));
  20. $compiler->raw(', ')->subcompile($this->getNode('attribute'));
  21. // only generate optional arguments when needed (to make generated code more readable)
  22. $needFourth = $this->getAttribute('ignore_strict_check');
  23. $needThird = $needFourth || $this->getAttribute('is_defined_test');
  24. $needSecond = $needThird || \Twig_Template::ANY_CALL !== $this->getAttribute('type');
  25. $needFirst = $needSecond || $this->hasNode('arguments');
  26. if ($needFirst) {
  27. if ($this->hasNode('arguments')) {
  28. $compiler->raw(', ')->subcompile($this->getNode('arguments'));
  29. } else {
  30. $compiler->raw(', array()');
  31. }
  32. }
  33. if ($needSecond) {
  34. $compiler->raw(', ')->repr($this->getAttribute('type'));
  35. }
  36. if ($needThird) {
  37. $compiler->raw(', ')->repr($this->getAttribute('is_defined_test'));
  38. }
  39. if ($needFourth) {
  40. $compiler->raw(', ')->repr($this->getAttribute('ignore_strict_check'));
  41. }
  42. $compiler->raw(')');
  43. }
  44. }