/** * Compiles the node to PHP. * * @param \Twig_Compiler $compiler * * @return null */ public function compile(\Twig_Compiler $compiler) { // Remember what 'nav' was set to before $compiler->write("if (isset(\$context['nav'])) {\n")->indent()->write("\$_nav = \$context['nav'];\n")->outdent()->write("}\n"); parent::compile($compiler); $compiler->write("if (isset(\$_thisItemLevel)) {\n")->indent()->write("\$_tmpContext = \$context;\n")->write("if (\$_thisItemLevel > \$_firstItemLevel) {\n")->indent()->write("for (\$_i = \$_thisItemLevel; \$_i > \$_firstItemLevel; \$_i--) {\n")->indent()->write("if (isset(\$_contextsByLevel[\$_i])) {\n")->indent()->write("\$context = \$_contextsByLevel[\$_i];\n")->subcompile($this->navItemNode->getNode('lower_body'), false)->subcompile($this->navItemNode->getNode('outdent'), false)->outdent()->write("}\n")->outdent()->write("}\n")->outdent()->write("}\n")->write("\$context = \$_contextsByLevel[\$_firstItemLevel];\n")->subcompile($this->navItemNode->getNode('lower_body'), false)->write("\$context = \$_tmpContext;\n")->write("unset(\$_thisItemLevel, \$_firstItemLevel, \$_i, \$_contextsByLevel, \$_tmpContext);\n")->outdent()->write("}\n")->write("if (isset(\$_nav)) {\n")->indent()->write("\$context['nav'] = \$_nav;\n")->write("unset(\$_nav);\n")->outdent()->write("}\n"); }
/** * @covers Twig_Node_For::__construct */ public function testConstructor() { $keyTarget = new Twig_Node_Expression_AssignName('key', 0); $valueTarget = new Twig_Node_Expression_AssignName('item', 0); $seq = new Twig_Node_Expression_Name('items', 0); $body = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0); $else = null; $withLoop = false; $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, $withLoop, 0); $this->assertEquals($keyTarget, $node->getNode('key_target')); $this->assertEquals($valueTarget, $node->getNode('value_target')); $this->assertEquals($seq, $node->getNode('seq')); $this->assertEquals($body, $node->getNode('body')); $this->assertEquals(null, $node->getNode('else')); $this->assertEquals($withLoop, $node->getAttribute('with_loop')); $else = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0); $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, $withLoop, 0); $this->assertEquals($else, $node->getNode('else')); }
public function getTests() { $tests = array(); $keyTarget = new Twig_Node_Expression_AssignName('key', 1); $valueTarget = new Twig_Node_Expression_AssignName('item', 1); $seq = new Twig_Node_Expression_Name('items', 1); $ifexpr = null; $body = new Twig_Node(array(new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 1), 1)), array(), 1); $else = null; $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1); $node->setAttribute('with_loop', false); $tests[] = array($node, <<<EOF // line 1 \$context['_parent'] = \$context; \$context['_seq'] = twig_ensure_traversable({$this->getVariableGetter('items')}); foreach (\$context['_seq'] as \$context["key"] => \$context["item"]) { echo {$this->getVariableGetter('foo')}; } \$_parent = \$context['_parent']; unset(\$context['_seq'], \$context['_iterated'], \$context['key'], \$context['item'], \$context['_parent'], \$context['loop']); \$context = array_intersect_key(\$context, \$_parent) + \$_parent; EOF ); $keyTarget = new Twig_Node_Expression_AssignName('k', 1); $valueTarget = new Twig_Node_Expression_AssignName('v', 1); $seq = new Twig_Node_Expression_Name('values', 1); $ifexpr = null; $body = new Twig_Node(array(new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 1), 1)), array(), 1); $else = null; $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1); $node->setAttribute('with_loop', true); $tests[] = array($node, <<<EOF // line 1 \$context['_parent'] = \$context; \$context['_seq'] = twig_ensure_traversable({$this->getVariableGetter('values')}); \$context['loop'] = array( 'parent' => \$context['_parent'], 'index0' => 0, 'index' => 1, 'first' => true, ); if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) { \$length = count(\$context['_seq']); \$context['loop']['revindex0'] = \$length - 1; \$context['loop']['revindex'] = \$length; \$context['loop']['length'] = \$length; \$context['loop']['last'] = 1 === \$length; } foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) { echo {$this->getVariableGetter('foo')}; ++\$context['loop']['index0']; ++\$context['loop']['index']; \$context['loop']['first'] = false; if (isset(\$context['loop']['length'])) { --\$context['loop']['revindex0']; --\$context['loop']['revindex']; \$context['loop']['last'] = 0 === \$context['loop']['revindex0']; } } \$_parent = \$context['_parent']; unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']); \$context = array_intersect_key(\$context, \$_parent) + \$_parent; EOF ); $keyTarget = new Twig_Node_Expression_AssignName('k', 1); $valueTarget = new Twig_Node_Expression_AssignName('v', 1); $seq = new Twig_Node_Expression_Name('values', 1); $ifexpr = new Twig_Node_Expression_Constant(true, 1); $body = new Twig_Node(array(new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 1), 1)), array(), 1); $else = null; $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1); $node->setAttribute('with_loop', true); $tests[] = array($node, <<<EOF // line 1 \$context['_parent'] = \$context; \$context['_seq'] = twig_ensure_traversable({$this->getVariableGetter('values')}); \$context['loop'] = array( 'parent' => \$context['_parent'], 'index0' => 0, 'index' => 1, 'first' => true, ); foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) { if (true) { echo {$this->getVariableGetter('foo')}; ++\$context['loop']['index0']; ++\$context['loop']['index']; \$context['loop']['first'] = false; } } \$_parent = \$context['_parent']; unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']); \$context = array_intersect_key(\$context, \$_parent) + \$_parent; EOF ); $keyTarget = new Twig_Node_Expression_AssignName('k', 1); $valueTarget = new Twig_Node_Expression_AssignName('v', 1); $seq = new Twig_Node_Expression_Name('values', 1); $ifexpr = null; $body = new Twig_Node(array(new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 1), 1)), array(), 1); $else = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 1), 1); $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $ifexpr, $body, $else, 1); $node->setAttribute('with_loop', true); $tests[] = array($node, <<<EOF // line 1 \$context['_parent'] = \$context; \$context['_seq'] = twig_ensure_traversable({$this->getVariableGetter('values')}); \$context['_iterated'] = false; \$context['loop'] = array( 'parent' => \$context['_parent'], 'index0' => 0, 'index' => 1, 'first' => true, ); if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) { \$length = count(\$context['_seq']); \$context['loop']['revindex0'] = \$length - 1; \$context['loop']['revindex'] = \$length; \$context['loop']['length'] = \$length; \$context['loop']['last'] = 1 === \$length; } foreach (\$context['_seq'] as \$context["k"] => \$context["v"]) { echo {$this->getVariableGetter('foo')}; \$context['_iterated'] = true; ++\$context['loop']['index0']; ++\$context['loop']['index']; \$context['loop']['first'] = false; if (isset(\$context['loop']['length'])) { --\$context['loop']['revindex0']; --\$context['loop']['revindex']; \$context['loop']['last'] = 0 === \$context['loop']['revindex0']; } } if (!\$context['_iterated']) { echo {$this->getVariableGetter('foo')}; } \$_parent = \$context['_parent']; unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']); \$context = array_intersect_key(\$context, \$_parent) + \$_parent; EOF ); return $tests; }
public function getTests() { $tests = array(); $keyTarget = new Twig_Node_Expression_AssignName('key', 0); $valueTarget = new Twig_Node_Expression_AssignName('item', 0); $seq = new Twig_Node_Expression_Name('items', 0); $body = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0); $else = null; $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, 0); $node->setAttribute('with_loop', false); $tests[] = array($node, <<<EOF \$context['_parent'] = (array) \$context; \$context['_seq'] = twig_ensure_traversable((isset(\$context['items']) ? \$context['items'] : null)); foreach (\$context['_seq'] as \$context['key'] => \$context['item']) { echo (isset(\$context['foo']) ? \$context['foo'] : null); } \$_parent = \$context['_parent']; unset(\$context['_seq'], \$context['_iterated'], \$context['key'], \$context['item'], \$context['_parent'], \$context['loop']); \$context = array_merge(\$_parent, array_intersect_key(\$context, \$_parent)); EOF ); $keyTarget = new Twig_Node_Expression_AssignName('k', 0); $valueTarget = new Twig_Node_Expression_AssignName('v', 0); $seq = new Twig_Node_Expression_Name('values', 0); $body = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0); $else = null; $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, 0); $node->setAttribute('with_loop', true); $tests[] = array($node, <<<EOF \$context['_parent'] = (array) \$context; \$context['_seq'] = twig_ensure_traversable((isset(\$context['values']) ? \$context['values'] : null)); \$context['loop'] = array( 'parent' => \$context['_parent'], 'index0' => 0, 'index' => 1, 'first' => true, ); if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) { \$length = count(\$context['_seq']); \$context['loop']['revindex0'] = \$length - 1; \$context['loop']['revindex'] = \$length; \$context['loop']['length'] = \$length; \$context['loop']['last'] = 1 === \$length; } foreach (\$context['_seq'] as \$context['k'] => \$context['v']) { echo (isset(\$context['foo']) ? \$context['foo'] : null); ++\$context['loop']['index0']; ++\$context['loop']['index']; \$context['loop']['first'] = false; if (isset(\$context['loop']['length'])) { --\$context['loop']['revindex0']; --\$context['loop']['revindex']; \$context['loop']['last'] = 0 === \$context['loop']['revindex0']; } } \$_parent = \$context['_parent']; unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']); \$context = array_merge(\$_parent, array_intersect_key(\$context, \$_parent)); EOF ); $keyTarget = new Twig_Node_Expression_AssignName('k', 0); $valueTarget = new Twig_Node_Expression_AssignName('v', 0); $seq = new Twig_Node_Expression_Name('values', 0); $body = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0); $else = new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 0), 0); $node = new Twig_Node_For($keyTarget, $valueTarget, $seq, $body, $else, 0); $node->setAttribute('with_loop', true); $tests[] = array($node, <<<EOF \$context['_parent'] = (array) \$context; \$context['_seq'] = twig_ensure_traversable((isset(\$context['values']) ? \$context['values'] : null)); \$context['_iterated'] = false; \$context['loop'] = array( 'parent' => \$context['_parent'], 'index0' => 0, 'index' => 1, 'first' => true, ); if (is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable)) { \$length = count(\$context['_seq']); \$context['loop']['revindex0'] = \$length - 1; \$context['loop']['revindex'] = \$length; \$context['loop']['length'] = \$length; \$context['loop']['last'] = 1 === \$length; } foreach (\$context['_seq'] as \$context['k'] => \$context['v']) { echo (isset(\$context['foo']) ? \$context['foo'] : null); \$context['_iterated'] = true; ++\$context['loop']['index0']; ++\$context['loop']['index']; \$context['loop']['first'] = false; if (isset(\$context['loop']['length'])) { --\$context['loop']['revindex0']; --\$context['loop']['revindex']; \$context['loop']['last'] = 0 === \$context['loop']['revindex0']; } } if (!\$context['_iterated']) { echo (isset(\$context['foo']) ? \$context['foo'] : null); } \$_parent = \$context['_parent']; unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']); \$context = array_merge(\$_parent, array_intersect_key(\$context, \$_parent)); EOF ); return $tests; }
/** * @param \Twig_Node_For $node */ public function addForCollection(\Twig_Node_For $node) { $iteratorArray = $node->getNode('value_target')->getAttribute('name'); $iterator = $iteratorArray[0]; $interval = $node->getNode('seq'); if ($interval->hasNode('left') && $interval->hasNode('right')) { $left = $interval->getNode('left')->getAttribute('value'); $right = $interval->getNode('right')->getAttribute('value'); $this->forCollection[$iterator] = array('left' => $left, 'right' => $right); } }