For.php 4.3KB

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