public function parse(Apishka_Templater_Token $token) { $lineno = $token->getLine(); $expr = $this->parser->getExpressionParser()->parseExpression(); $stream = $this->parser->getStream(); $stream->expect(Apishka_Templater_Token::BLOCK_END_TYPE); $body = $this->parser->subparse(array($this, 'decideIfFork')); $tests = array($expr, $body); $else = null; $end = false; while (!$end) { switch ($stream->next()->getValue()) { case 'else': $stream->expect(Apishka_Templater_Token::BLOCK_END_TYPE); $else = $this->parser->subparse(array($this, 'decideIfEnd')); break; case 'elseif': $expr = $this->parser->getExpressionParser()->parseExpression(); $stream->expect(Apishka_Templater_Token::BLOCK_END_TYPE); $body = $this->parser->subparse(array($this, 'decideIfFork')); $tests[] = $expr; $tests[] = $body; break; case 'endif': $end = true; break; default: throw new Apishka_Templater_Error_Syntax(sprintf('Unexpected end of template. Twig was looking for the following tags "else", "elseif", or "endif" to close the "if" block started at line %d).', $lineno), $stream->getCurrent()->getLine(), $stream->getFilename()); } } $stream->expect(Apishka_Templater_Token::BLOCK_END_TYPE); return Apishka_Templater_Node_If::apishka(Apishka_Templater_Node::apishka($tests), $else, $lineno, $this->getTag()); }
public function parse(Apishka_Templater_Token $token) { $lineno = $token->getLine(); $stream = $this->parser->getStream(); $name = $stream->expect(Apishka_Templater_Token::NAME_TYPE)->getValue(); if ($this->parser->hasBlock($name)) { throw new Apishka_Templater_Error_Syntax(sprintf("The block '%s' has already been defined line %d.", $name, $this->parser->getBlock($name)->getLine()), $stream->getCurrent()->getLine(), $stream->getFilename()); } $this->parser->setBlock($name, $block = Apishka_Templater_Node_Block::apishka($name, Apishka_Templater_Node::apishka(array()), $lineno)); $this->parser->pushLocalScope(); $this->parser->pushBlockStack($name); if ($stream->nextIf(Apishka_Templater_Token::BLOCK_END_TYPE)) { $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true); if ($token = $stream->nextIf(Apishka_Templater_Token::NAME_TYPE)) { $value = $token->getValue(); if ($value != $name) { throw new Apishka_Templater_Error_Syntax(sprintf('Expected endblock for block "%s" (but "%s" given).', $name, $value), $stream->getCurrent()->getLine(), $stream->getFilename()); } } } else { $body = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Print::apishka($this->parser->getExpressionParser()->parseExpression(), $lineno))); } $stream->expect(Apishka_Templater_Token::BLOCK_END_TYPE); $block->setNode('body', $body); $this->parser->popBlockStack(); $this->parser->popLocalScope(); return Apishka_Templater_Node_BlockReference::apishka($name, $lineno, $this->getTag()); }
public function __construct(Apishka_Templater_Node_Expression_AssignName $keyTarget, Apishka_Templater_Node_Expression_AssignName $valueTarget, Apishka_Templater_Node_ExpressionAbstract $seq, Apishka_Templater_Node_ExpressionAbstract $ifexpr = null, Apishka_Templater_NodeAbstract $body, Apishka_Templater_NodeAbstract $else = null, $lineno, $tag = null) { $body = Apishka_Templater_Node::apishka(array($body, $this->loop = Apishka_Templater_Node_ForLoop::apishka($lineno, $tag))); if (null !== $ifexpr) { $body = Apishka_Templater_Node_If::apishka(Apishka_Templater_Node::apishka(array($ifexpr, $body)), null, $lineno, $tag); } parent::__construct(array('key_target' => $keyTarget, 'value_target' => $valueTarget, 'seq' => $seq, 'body' => $body, 'else' => $else), array('with_loop' => true, 'ifexpr' => null !== $ifexpr), $lineno, $tag); }
public function parse(Apishka_Templater_Token $token) { $name = $this->parser->getVarName(); $ref = Apishka_Templater_Node_Expression_BlockReference::apishka(Apishka_Templater_Node_Expression_Constant::apishka($name, $token->getLine()), Apishka_Templater_Node::apishka(), true, $token->getLine(), $this->getTag()); $filter = $this->parser->getExpressionParser()->parseFilterExpressionRaw($ref, $this->getTag()); $this->parser->getStream()->expect(Apishka_Templater_Token::BLOCK_END_TYPE); $body = $this->parser->subparse(array($this, 'decideBlockEnd'), true); $this->parser->getStream()->expect(Apishka_Templater_Token::BLOCK_END_TYPE); $block = Apishka_Templater_Node_Block::apishka($name, $body, $token->getLine()); $this->parser->setBlock($name, $block); return Apishka_Templater_Node_Print::apishka($filter, $token->getLine(), $this->getTag()); }
/** * {@inheritdoc} */ protected function doLeaveNode(Apishka_Templater_NodeAbstract $node, Apishka_Templater_Environment $env) { if ($node instanceof Apishka_Templater_Node_Module) { $varName = $this->getVarName(); $node->setNode('display_start', Apishka_Templater_Node::apishka(array(new Apishka_Templater_Profiler_Node_EnterProfile($this->extensionName, Apishka_Templater_Profiler_Profile::TEMPLATE, $node->getAttribute('filename'), $varName), $node->getNode('display_start')))); $node->setNode('display_end', Apishka_Templater_Node::apishka(array(new Apishka_Templater_Profiler_Node_LeaveProfile($varName), $node->getNode('display_end')))); } elseif ($node instanceof Apishka_Templater_Node_Block) { $varName = $this->getVarName(); $node->setNode('body', Apishka_Templater_Node_Body::apishka(array(new Apishka_Templater_Profiler_Node_EnterProfile($this->extensionName, Apishka_Templater_Profiler_Profile::BLOCK, $node->getAttribute('name'), $varName), $node->getNode('body'), new Apishka_Templater_Profiler_Node_LeaveProfile($varName)))); } return $node; }
public function getTests() { $tests = array(); // render: no args $node = Apishka_Templater_Node_Expression_BlockReference::apishka(Apishka_Templater_Node_Expression_Constant::apishka('foo', 1), Apishka_Templater_Node::apishka(), false, 1); $tests[] = array($node, '$this->renderBlock("foo", $context, $blocks)'); // render: with args $node = Apishka_Templater_Node_Expression_BlockReference::apishka(Apishka_Templater_Node_Expression_Constant::apishka('foo', 1), Apishka_Templater_Node::apishka(array('named_arg' => Apishka_Templater_Node_Expression_Constant::apishka('bar', 1))), false, 1); $tests[] = array($node, '$this->renderBlock("foo", array_replace($context, array("named_arg" => "bar",)), $blocks)'); // display: no args $node = Apishka_Templater_Node_Expression_BlockReference::apishka(Apishka_Templater_Node_Expression_Constant::apishka('foo', 1), Apishka_Templater_Node::apishka(), false, 1); $node->setAttribute('output', true); $tests[] = array($node, '// line 1' . PHP_EOL . '$this->displayBlock("foo", $context, $blocks);'); // display: with args $node = Apishka_Templater_Node_Expression_BlockReference::apishka(Apishka_Templater_Node_Expression_Constant::apishka('foo', 1), Apishka_Templater_Node::apishka(array('named_arg' => Apishka_Templater_Node_Expression_Constant::apishka('bar', 1))), false, 1); $node->setAttribute('output', true); $tests[] = array($node, '// line 1 $this->displayBlock("foo", array_replace($context, array("named_arg" => "bar",)), $blocks);'); return $tests; }
public function getTests() { $tests = array(); $names = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_AssignName::apishka('foo', 1)), array(), 1); $values = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_Constant::apishka('foo', 1)), array(), 1); $node = Apishka_Templater_Node_Set::apishka(false, $names, $values, 1); $tests[] = array($node, <<<EOF // line 1 \$context["foo"] = "foo"; EOF ); $names = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_AssignName::apishka('foo', 1)), array(), 1); $values = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Constant::apishka('foo', 1), 1)), array(), 1); $node = Apishka_Templater_Node_Set::apishka(true, $names, $values, 1); $tests[] = array($node, <<<EOF // line 1 ob_start(); echo "foo"; \$context["foo"] = ('' === \$tmp = ob_get_clean()) ? '' : new Apishka_Templater_Markup(\$tmp, \$this->env->getCharset()); EOF ); $names = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_AssignName::apishka('foo', 1)), array(), 1); $values = Apishka_Templater_Node_Text::apishka('foo', 1); $node = Apishka_Templater_Node_Set::apishka(true, $names, $values, 1); $tests[] = array($node, <<<EOF // line 1 \$context["foo"] = ('' === \$tmp = "foo") ? '' : new Apishka_Templater_Markup(\$tmp, \$this->env->getCharset()); EOF ); $names = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_AssignName::apishka('foo', 1), Apishka_Templater_Node_Expression_AssignName::apishka('bar', 1)), array(), 1); $values = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_Constant::apishka('foo', 1), Apishka_Templater_Node_Expression_Name::apishka('bar', 1)), array(), 1); $node = Apishka_Templater_Node_Set::apishka(false, $names, $values, 1); $tests[] = array($node, <<<EOF // line 1 list(\$context["foo"], \$context["bar"]) = array("foo", {$this->getVariableGetter('bar')}); EOF ); return $tests; }
public function getTests() { $tests = array(); $t = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_Constant::apishka(true, 1), Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Name::apishka('foo', 1), 1)), array(), 1); $else = null; $node = Apishka_Templater_Node_If::apishka($t, $else, 1); $tests[] = array($node, <<<EOF // line 1 if (true) { echo {$this->getVariableGetter('foo')}; } EOF ); $t = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_Constant::apishka(true, 1), Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Name::apishka('foo', 1), 1), Apishka_Templater_Node_Expression_Constant::apishka(false, 1), Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Name::apishka('bar', 1), 1)), array(), 1); $else = null; $node = Apishka_Templater_Node_If::apishka($t, $else, 1); $tests[] = array($node, <<<EOF // line 1 if (true) { echo {$this->getVariableGetter('foo')}; } elseif (false) { echo {$this->getVariableGetter('bar')}; } EOF ); $t = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_Constant::apishka(true, 1), Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Name::apishka('foo', 1), 1)), array(), 1); $else = Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Name::apishka('bar', 1), 1); $node = Apishka_Templater_Node_If::apishka($t, $else, 1); $tests[] = array($node, <<<EOF // line 1 if (true) { echo {$this->getVariableGetter('foo')}; } else { echo {$this->getVariableGetter('bar')}; } EOF ); return $tests; }
public function parse(Apishka_Templater_Token $token) { $template = $this->parser->getExpressionParser()->parseExpression(); $stream = $this->parser->getStream(); if (!$template instanceof Apishka_Templater_Node_Expression_Constant) { throw new Apishka_Templater_Error_Syntax('The template references in a "use" statement must be a string.', $stream->getCurrent()->getLine(), $stream->getFilename()); } $targets = array(); if ($stream->nextIf('with')) { do { $name = $stream->expect(Apishka_Templater_Token::NAME_TYPE)->getValue(); $alias = $name; if ($stream->nextIf('as')) { $alias = $stream->expect(Apishka_Templater_Token::NAME_TYPE)->getValue(); } $targets[$name] = Apishka_Templater_Node_Expression_Constant::apishka($alias, -1); if (!$stream->nextIf(Apishka_Templater_Token::PUNCTUATION_TYPE, ',')) { break; } } while (true); } $stream->expect(Apishka_Templater_Token::BLOCK_END_TYPE); $this->parser->addTrait(Apishka_Templater_Node::apishka(array('template' => $template, 'targets' => Apishka_Templater_Node::apishka($targets)))); }
public function subparse($test, $dropNeedle = false) { $lineno = $this->getCurrentToken()->getLine(); $rv = array(); while (!$this->stream->isEOF()) { switch ($this->getCurrentToken()->getType()) { case Apishka_Templater_Token::TEXT_TYPE: $token = $this->stream->next(); $rv[] = Apishka_Templater_Node_Text::apishka($token->getValue(), $token->getLine()); break; case Apishka_Templater_Token::VAR_START_TYPE: $token = $this->stream->next(); $expr = $this->expressionParser->parseExpression(); $this->stream->expect(Apishka_Templater_Token::VAR_END_TYPE); $rv[] = Apishka_Templater_Node_Print::apishka($expr, $token->getLine()); break; case Apishka_Templater_Token::BLOCK_START_TYPE: $this->stream->next(); $token = $this->getCurrentToken(); if ($token->getType() !== Apishka_Templater_Token::NAME_TYPE) { throw new Apishka_Templater_Error_Syntax('A block must start with a tag name.', $token->getLine(), $this->getFilename()); } if (null !== $test && $test($token)) { if ($dropNeedle) { $this->stream->next(); } if (1 === count($rv)) { return $rv[0]; } return Apishka_Templater_Node::apishka($rv, array(), $lineno); } if (!isset($this->handlers[$token->getValue()])) { if (null !== $test) { $e = new Apishka_Templater_Error_Syntax(sprintf('Unexpected "%s" tag', $token->getValue()), $token->getLine(), $this->getFilename()); if (is_array($test) && isset($test[0]) && $test[0] instanceof Apishka_Templater_TokenParserInterface) { $e->appendMessage(sprintf(' (expecting closing tag for the "%s" tag defined near line %s).', $test[0]->getTag(), $lineno)); } } else { $e = new Apishka_Templater_Error_Syntax(sprintf('Unknown "%s" tag.', $token->getValue()), $token->getLine(), $this->getFilename()); $e->addSuggestions($token->getValue(), array_keys($this->env->getTags())); } throw $e; } $this->stream->next(); $subparser = $this->handlers[$token->getValue()]; $node = $subparser->parse($token); if (null !== $node) { $rv[] = $node; } break; default: throw new Apishka_Templater_Error_Syntax('Lexer or parser ended up in unsupported state.', 0, $this->getFilename()); } } if (1 === count($rv)) { return $rv[0]; } return Apishka_Templater_Node::apishka($rv, array(), $lineno); }
protected function createTest($node, $name, array $arguments = array()) { return Apishka_Templater_Node_Expression_Test::apishka($node, $name, Apishka_Templater_Node::apishka($arguments), 1); }
public function parseMultitargetExpression() { $targets = array(); while (true) { $targets[] = $this->parseExpression(); if (!$this->parser->getStream()->nextIf(Apishka_Templater_Token::PUNCTUATION_TYPE, ',')) { break; } } return Apishka_Templater_Node::apishka($targets); }
public function getTests() { $tests = array(); $keyTarget = Apishka_Templater_Node_Expression_AssignName::apishka('key', 1); $valueTarget = Apishka_Templater_Node_Expression_AssignName::apishka('item', 1); $seq = Apishka_Templater_Node_Expression_Name::apishka('items', 1); $ifexpr = null; $body = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Name::apishka('foo', 1), 1)), array(), 1); $else = null; $node = Apishka_Templater_Node_For::apishka($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 = Apishka_Templater_Node_Expression_AssignName::apishka('k', 1); $valueTarget = Apishka_Templater_Node_Expression_AssignName::apishka('v', 1); $seq = Apishka_Templater_Node_Expression_Name::apishka('values', 1); $ifexpr = null; $body = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Name::apishka('foo', 1), 1)), array(), 1); $else = null; $node = Apishka_Templater_Node_For::apishka($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 = Apishka_Templater_Node_Expression_AssignName::apishka('k', 1); $valueTarget = Apishka_Templater_Node_Expression_AssignName::apishka('v', 1); $seq = Apishka_Templater_Node_Expression_Name::apishka('values', 1); $ifexpr = Apishka_Templater_Node_Expression_Constant::apishka(true, 1); $body = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Name::apishka('foo', 1), 1)), array(), 1); $else = null; $node = Apishka_Templater_Node_For::apishka($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 = Apishka_Templater_Node_Expression_AssignName::apishka('k', 1); $valueTarget = Apishka_Templater_Node_Expression_AssignName::apishka('v', 1); $seq = Apishka_Templater_Node_Expression_Name::apishka('values', 1); $ifexpr = null; $body = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Name::apishka('foo', 1), 1)), array(), 1); $else = Apishka_Templater_Node_Print::apishka(Apishka_Templater_Node_Expression_Name::apishka('foo', 1), 1); $node = Apishka_Templater_Node_For::apishka($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; }
protected function createFilter($node, $name, array $arguments = array()) { $name = Apishka_Templater_Node_Expression_Constant::apishka($name, 1); $arguments = Apishka_Templater_Node::apishka($arguments); return Apishka_Templater_Node_Expression_Filter::apishka($node, $name, $arguments, 1); }
public function getTests() { $twig = new Apishka_Templater_Environment($this->createMock('Apishka_Templater_LoaderInterface')); $tests = array(); $body = Apishka_Templater_Node_Text::apishka('foo', 1); $extends = null; $blocks = Apishka_Templater_Node::apishka(); $macros = Apishka_Templater_Node::apishka(); $traits = Apishka_Templater_Node::apishka(); $filename = 'foo.twig'; $node = Apishka_Templater_Node_Module::apishka($body, $extends, $blocks, $macros, $traits, Apishka_Templater_Node::apishka(array()), $filename); $tests[] = array($node, <<<EOF <?php /* foo.twig */ class __TwigTemplate_%x extends Apishka_Templater_TemplateAbstract { public function __construct(Apishka_Templater_Environment \$env) { parent::__construct(\$env); \$this->parent = false; \$this->blocks = array( ); } public function getSupportedNames() { return array( 'foo.twig', ); } protected function doDisplay(array \$context, array \$blocks = array()) { // line 1 echo "foo"; } public function getTemplateName() { return "foo.twig"; } public function getDebugInfo() { return array ( 26 => 1,); } } EOF , $twig, true); $body = Apishka_Templater_Node::apishka(array()); $extends = Apishka_Templater_Node_Expression_Constant::apishka('layout.twig', 1); $node = Apishka_Templater_Node_Module::apishka($body, $extends, $blocks, $macros, $traits, Apishka_Templater_Node::apishka(array()), $filename); $tests[] = array($node, <<<EOF <?php /* foo.twig */ class __TwigTemplate_%x extends Apishka_Templater_TemplateAbstract { public function __construct(Apishka_Templater_Environment \$env) { parent::__construct(\$env); // line 1 \$this->parent = \$this->loadTemplate("layout.twig", "foo.twig", 1); \$this->blocks = array( ); } public function getSupportedNames() { return array( 'foo.twig', ); } protected function doGetParent(array \$context) { return "layout.twig"; } protected function doDisplay(array \$context, array \$blocks = array()) { \$this->parent->display(\$context, array_merge(\$this->blocks, \$blocks)); } public function getTemplateName() { return "foo.twig"; } public function isTraitable() { return false; } public function getDebugInfo() { return array ( 11 => 1,); } } EOF , $twig, true); $set = Apishka_Templater_Node_Set::apishka(false, Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_AssignName::apishka('foo', 4))), Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_Constant::apishka('foo', 4))), 4); $body = Apishka_Templater_Node::apishka(array($set)); $extends = Apishka_Templater_Node_Expression_Conditional::apishka(Apishka_Templater_Node_Expression_Constant::apishka(true, 2), Apishka_Templater_Node_Expression_Constant::apishka('foo', 2), Apishka_Templater_Node_Expression_Constant::apishka('foo', 2), 2); $node = Apishka_Templater_Node_Module::apishka($body, $extends, $blocks, $macros, $traits, Apishka_Templater_Node::apishka(array()), $filename); $tests[] = array($node, <<<EOF <?php /* foo.twig */ class __TwigTemplate_%x extends Apishka_Templater_TemplateAbstract { public function getSupportedNames() { return array( 'foo.twig', ); } protected function doGetParent(array \$context) { // line 2 return \$this->loadTemplate(((true) ? ("foo") : ("foo")), "foo.twig", 2); } protected function doDisplay(array \$context, array \$blocks = array()) { // line 4 \$context["foo"] = "foo"; // line 2 \$this->getParent(\$context)->display(\$context, array_merge(\$this->blocks, \$blocks)); } public function getTemplateName() { return "foo.twig"; } public function isTraitable() { return false; } public function getDebugInfo() { return array ( 24 => 2, 22 => 4, 16 => 2,); } } EOF , $twig, true); return $tests; }
protected function compileIsTraitable(Apishka_Templater_Compiler $compiler) { // A template can be used as a trait if: // * it has no parent // * it has no macros // * it has no body // // Put another way, a template can be used as a trait if it // only contains blocks and use statements. $traitable = null === $this->getNode('parent') && 0 === count($this->getNode('macros')); if ($traitable) { if ($this->getNode('body') instanceof Apishka_Templater_Node_Body) { $nodes = $this->getNode('body')->getNode(0); } else { $nodes = $this->getNode('body'); } if (!count($nodes)) { $nodes = Apishka_Templater_Node::apishka(array($nodes)); } foreach ($nodes as $node) { if (!count($node)) { continue; } if ($node instanceof Apishka_Templater_Node_Text && ctype_space($node->getAttribute('data'))) { continue; } if ($node instanceof Apishka_Templater_Node_BlockReference) { continue; } $traitable = false; break; } } if ($traitable) { return; } $compiler->write("public function isTraitable()\n", "{\n")->indent()->write(sprintf("return %s;\n", $traitable ? 'true' : 'false'))->outdent()->write("}\n\n"); }
public function getTestsForBlock() { $tests = array(); $node = Apishka_Templater_Node_Expression_BlockReference::apishka(Apishka_Templater_Node_Expression_Constant::apishka('foo', 1), Apishka_Templater_Node::apishka(), false, 1); $node->setAttribute('output', true); $tests[] = array('{{ block("foo") }}', $node); $node = Apishka_Templater_Node_Expression_BlockReference::apishka(Apishka_Templater_Node_Expression_Constant::apishka('foo', 1), Apishka_Templater_Node::apishka(array('named_arg' => Apishka_Templater_Node_Expression_Constant::apishka('bar', 1))), false, 1); $node->setAttribute('output', true); $tests[] = array('{{ block("foo", named_arg="bar") }}', $node); return $tests; }
public function getTests() { $body = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Text::apishka('foo', 1))); $node = Apishka_Templater_Node_AutoEscape::apishka(true, $body, 1); return array(array($node, "// line 1\necho \"foo\";")); }
public function parse(Apishka_Templater_Token $token) { // simulate the parsing of another template right in the middle of the parsing of the current template $this->parser->parse(new Apishka_Templater_TokenStream(array(new Apishka_Templater_Token(Apishka_Templater_Token::BLOCK_START_TYPE, '', 1), new Apishka_Templater_Token(Apishka_Templater_Token::NAME_TYPE, 'extends', 1), new Apishka_Templater_Token(Apishka_Templater_Token::STRING_TYPE, 'base', 1), new Apishka_Templater_Token(Apishka_Templater_Token::BLOCK_END_TYPE, '', 1), new Apishka_Templater_Token(Apishka_Templater_Token::EOF_TYPE, '', 1)))); $this->parser->getStream()->expect(Apishka_Templater_Token::BLOCK_END_TYPE); return Apishka_Templater_Node::apishka(array()); }
private function getEscaperFilter($type, Apishka_Templater_NodeAbstract $node) { $line = $node->getLine(); $name = Apishka_Templater_Node_Expression_Constant::apishka('escape', $line); $args = Apishka_Templater_Node::apishka(array(Apishka_Templater_Node_Expression_Constant::apishka((string) $type, $line), Apishka_Templater_Node_Expression_Constant::apishka(null, $line), Apishka_Templater_Node_Expression_Constant::apishka(true, $line))); return Apishka_Templater_Node_Expression_Filter::apishka($node, $name, $args, $line); }