For.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /**
  12. * Represents a for node.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Twig_Node_For extends Twig_Node
  17. {
  18. protected $loop;
  19. public function __construct(Twig_Node_Expression_AssignName $keyTarget, Twig_Node_Expression_AssignName $valueTarget, Twig_Node_Expression $seq, Twig_Node_Expression $ifexpr = null, Twig_NodeInterface $body, Twig_NodeInterface $else = null, $lineno, $tag = null)
  20. {
  21. $body = new Twig_Node(array($body, $this->loop = new Twig_Node_ForLoop($lineno, $tag)));
  22. if (null !== $ifexpr) {
  23. $body = new Twig_Node_If(new Twig_Node(array($ifexpr, $body)), null, $lineno, $tag);
  24. }
  25. parent::__construct(array('key_target' => $keyTarget, 'value_target' => $valueTarget, 'seq' => $seq, 'body' => $body, 'else' => $else), array('with_loop' => true, 'ifexpr' => null !== $ifexpr), $lineno, $tag);
  26. }
  27. public function compile(Twig_Compiler $compiler)
  28. {
  29. $compiler
  30. ->addDebugInfo($this)
  31. ->write("\$context['_parent'] = \$context;\n")
  32. ->write("\$context['_seq'] = twig_ensure_traversable(")
  33. ->subcompile($this->getNode('seq'))
  34. ->raw(");\n")
  35. ;
  36. if (null !== $this->getNode('else')) {
  37. $compiler->write("\$context['_iterated'] = false;\n");
  38. }
  39. if ($this->getAttribute('with_loop')) {
  40. $compiler
  41. ->write("\$context['loop'] = array(\n")
  42. ->write(" 'parent' => \$context['_parent'],\n")
  43. ->write(" 'index0' => 0,\n")
  44. ->write(" 'index' => 1,\n")
  45. ->write(" 'first' => true,\n")
  46. ->write(");\n")
  47. ;
  48. if (!$this->getAttribute('ifexpr')) {
  49. $compiler
  50. ->write("if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) {\n")
  51. ->indent()
  52. ->write("\$length = count(\$context['_seq']);\n")
  53. ->write("\$context['loop']['revindex0'] = \$length - 1;\n")
  54. ->write("\$context['loop']['revindex'] = \$length;\n")
  55. ->write("\$context['loop']['length'] = \$length;\n")
  56. ->write("\$context['loop']['last'] = 1 === \$length;\n")
  57. ->outdent()
  58. ->write("}\n")
  59. ;
  60. }
  61. }
  62. $this->loop->setAttribute('else', null !== $this->getNode('else'));
  63. $this->loop->setAttribute('with_loop', $this->getAttribute('with_loop'));
  64. $this->loop->setAttribute('ifexpr', $this->getAttribute('ifexpr'));
  65. $compiler
  66. ->write("foreach (\$context['_seq'] as ")
  67. ->subcompile($this->getNode('key_target'))
  68. ->raw(' => ')
  69. ->subcompile($this->getNode('value_target'))
  70. ->raw(") {\n")
  71. ->indent()
  72. ->subcompile($this->getNode('body'))
  73. ->outdent()
  74. ->write("}\n")
  75. ;
  76. if (null !== $this->getNode('else')) {
  77. $compiler
  78. ->write("if (!\$context['_iterated']) {\n")
  79. ->indent()
  80. ->subcompile($this->getNode('else'))
  81. ->outdent()
  82. ->write("}\n")
  83. ;
  84. }
  85. $compiler->write("\$_parent = \$context['_parent'];\n");
  86. // remove some "private" loop variables (needed for nested loops)
  87. $compiler->write('unset($context[\'_seq\'], $context[\'_iterated\'], $context[\''.$this->getNode('key_target')->getAttribute('name').'\'], $context[\''.$this->getNode('value_target')->getAttribute('name').'\'], $context[\'_parent\'], $context[\'loop\']);'."\n");
  88. // keep the values set in the inner context for variables defined in the outer context
  89. $compiler->write("\$context = array_intersect_key(\$context, \$_parent) + \$_parent;\n");
  90. }
  91. }