For.php 4.4KB

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