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; }
/** * {@inheritdoc} */ public function parse(Apishka_Templater_TokenStream $stream, $test = null, $dropNeedle = false) { // push all variables into the stack to keep the current state of the parser // using get_object_vars() instead of foreach would lead to https://bugs.php.net/71336 $vars = array(); foreach ($this as $k => $v) { $vars[$k] = $v; } unset($vars['stack'], $vars['env'], $vars['handlers'], $vars['visitors'], $vars['expressionParser'], $vars['reservedMacroNames']); $this->stack[] = $vars; // tag handlers if (null === $this->handlers) { $this->handlers = array(); foreach ($this->env->getTokenParsers() as $handler) { $handler->setParser($this); $this->handlers[$handler->getTag()] = $handler; } } // node visitors if (null === $this->visitors) { $this->visitors = $this->env->getNodeVisitors(); } if ($this->expressionParser === null) { $this->expressionParser = Apishka_Templater_ExpressionParser::apishka($this, $this->env->getUnaryOperators(), $this->env->getBinaryOperators()); } $this->stream = $stream; $this->parent = null; $this->blocks = array(); $this->macros = array(); $this->traits = array(); $this->blockStack = array(); $this->importedSymbols = array(array()); $this->embeddedTemplates = array(); try { $body = $this->subparse($test, $dropNeedle); if (null !== $this->parent && null === ($body = $this->filterBodyNodes($body))) { $body = Apishka_Templater_Node::apishka(); } } catch (Apishka_Templater_Error_Syntax $e) { if (!$e->getTemplateFile()) { $e->setTemplateFile($this->getFilename()); } if (!$e->getTemplateLine()) { $e->setTemplateLine($this->stream->getCurrent()->getLine()); } throw $e; } $node = Apishka_Templater_Node_Module::apishka(Apishka_Templater_Node_Body::apishka(array($body)), $this->parent, Apishka_Templater_Node::apishka($this->blocks), Apishka_Templater_Node::apishka($this->macros), Apishka_Templater_Node::apishka($this->traits), $this->embeddedTemplates, $this->getFilename()); $traverser = new Apishka_Templater_NodeTraverser($this->env, $this->visitors); $node = $traverser->traverse($node); // restore previous stack so previous parse() call can resume working foreach (array_pop($this->stack) as $key => $val) { $this->{$key} = $val; } return $node; }