public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); $testMap = $compiler->getEnvironment()->getTests(); if (!isset($testMap[$name])) { $message = sprintf('The test "%s" does not exist', $name); if ($alternatives = $compiler->getEnvironment()->computeAlternatives($name, array_keys($compiler->getEnvironment()->getTests()))) { $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives)); } throw new Twig_Error_Syntax($message, $this->getLine(), $compiler->getFilename()); } $name = $this->getAttribute('name'); $node = $this->getNode('node'); $compiler->raw($testMap[$name]->compile() . '(')->subcompile($node); if (null !== $this->getNode('arguments')) { $compiler->raw(', '); $max = count($this->getNode('arguments')) - 1; foreach ($this->getNode('arguments') as $i => $arg) { $compiler->subcompile($arg); if ($i != $max) { $compiler->raw(', '); } } } $compiler->raw(')'); }
/** * Compiles the node to PHP. * * @param Twig_Compiler $compiler A Twig_Compiler instance */ public function compile(Twig_Compiler $compiler) { $compiler->addDebugInfo($this)->write('')->subcompile($this->getNode('var'))->raw(' = '); if ($this->getNode('expr') instanceof Twig_Node_Expression_Name && '_self' === $this->getNode('expr')->getAttribute('name')) { $compiler->raw('$this'); } else { $compiler->raw('$this->loadTemplate(')->subcompile($this->getNode('expr'))->raw(', ')->repr($compiler->getFilename())->raw(', ')->repr($this->getLine())->raw(')'); } $compiler->raw(";\n"); }
public function compile(Twig_Compiler $compiler) { $name = $this->getNode('filter')->getAttribute('value'); if (false === ($filter = $compiler->getEnvironment()->getFilter($name))) { $message = sprintf('The filter "%s" does not exist', $name); if ($alternatives = $compiler->getEnvironment()->computeAlternatives($name, array_keys($compiler->getEnvironment()->getFilters()))) { $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives)); } throw new Twig_Error_Syntax($message, $this->getLine(), $compiler->getFilename()); } $this->compileFilter($compiler, $filter); }
public function compile(Twig_Compiler $compiler) { if ($compiler->getEnvironment()->isStrictVariables()) { $name = $this->getAttribute('name'); $type = $this->getAttribute('type'); $compiler->addDebugInfo($this); if (0 === strcasecmp($type, 'array')) { $compiler->write("if (false === is_array(\$context['{$name}'])) {\n"); } else { $compiler->write("if (false === \$context['{$name}'] instanceof {$type}) {\n"); } $compiler->indent()->write("throw new Twig_Error_Runtime('variable \\'{$name}\\' is expected to be of type {$type} but '.get_class(\$context['{$name}']).' was provided.', {$this->getLine()}, '{$compiler->getFilename()}');\n")->outdent()->write("}\n"); } }
public function compile(\Twig_Compiler $compiler) { $compiler->addDebugInfo($this); try { $template = $compiler->getFilename(); if (Helper::hasBufferKey($template, $this->tag)) { $string = Helper::getBufferKey($template, $this->tag); } else { $string = "[{$this->tag}]"; // display a placeholder } } catch (\Exception $e) { $string = "[{$this->tag}]"; // display a placeholder } $compiler->raw("echo secure_string('{$string}');"); }
protected function compileConstructor(Twig_Compiler $compiler) { $compiler->write("public function __construct(Twig_Environment \$env)\n", "{\n")->indent()->subcompile($this->getNode('constructor_start'))->write("parent::__construct(\$env);\n\n"); // parent if (null === ($parent = $this->getNode('parent'))) { $compiler->write("\$this->parent = false;\n\n"); } elseif ($parent instanceof Twig_Node_Expression_Constant) { $compiler->addDebugInfo($parent)->write('$this->parent = $this->loadTemplate(')->subcompile($parent)->raw(', ')->repr($compiler->getFilename())->raw(', ')->repr($this->getNode('parent')->getLine())->raw(");\n"); } $countTraits = count($this->getNode('traits')); if ($countTraits) { // traits foreach ($this->getNode('traits') as $i => $trait) { $node = $trait->getNode('template'); $compiler->write(sprintf('$_trait_%s = $this->loadTemplate(', $i))->subcompile($node)->raw(', ')->repr($compiler->getFilename())->raw(', ')->repr($node->getLine())->raw(");\n"); $compiler->addDebugInfo($trait->getNode('template'))->write(sprintf("if (!\$_trait_%s->isTraitable()) {\n", $i))->indent()->write("throw new Twig_Error_Runtime('Template \"'.")->subcompile($trait->getNode('template'))->raw(".'\" cannot be used as a trait.');\n")->outdent()->write("}\n")->write(sprintf("\$_trait_%s_blocks = \$_trait_%s->getBlocks();\n\n", $i, $i)); foreach ($trait->getNode('targets') as $key => $value) { $compiler->write(sprintf('if (!isset($_trait_%s_blocks[', $i))->string($key)->raw("])) {\n")->indent()->write("throw new Twig_Error_Runtime(sprintf('Block ")->string($key)->raw(' is not defined in trait ')->subcompile($trait->getNode('template'))->raw(".'));\n")->outdent()->write("}\n\n")->write(sprintf('$_trait_%s_blocks[', $i))->subcompile($value)->raw(sprintf('] = $_trait_%s_blocks[', $i))->string($key)->raw(sprintf(']; unset($_trait_%s_blocks[', $i))->string($key)->raw("]);\n\n"); } } if ($countTraits > 1) { $compiler->write("\$this->traits = array_merge(\n")->indent(); for ($i = 0; $i < $countTraits; ++$i) { $compiler->write(sprintf('$_trait_%s_blocks' . ($i == $countTraits - 1 ? '' : ',') . "\n", $i)); } $compiler->outdent()->write(");\n\n"); } else { $compiler->write("\$this->traits = \$_trait_0_blocks;\n\n"); } $compiler->write("\$this->blocks = array_merge(\n")->indent()->write("\$this->traits,\n")->write("array(\n"); } else { $compiler->write("\$this->blocks = array(\n"); } // blocks $compiler->indent(); foreach ($this->getNode('blocks') as $name => $node) { $compiler->write(sprintf("'%s' => array(\$this, 'block_%s'),\n", $name, $name)); } if ($countTraits) { $compiler->outdent()->write(")\n"); } $compiler->outdent()->write(");\n")->outdent()->subcompile($this->getNode('constructor_end'))->write("}\n\n"); }
public function compile(Twig_Compiler $compiler) { $name = $this->getAttribute('name'); if (false === ($function = $compiler->getEnvironment()->getFunction($name))) { $message = sprintf('The function "%s" does not exist', $name); if ($alternatives = $compiler->getEnvironment()->computeAlternatives($name, array_keys($compiler->getEnvironment()->getFunctions()))) { $message = sprintf('%s. Did you mean "%s"', $message, implode('", "', $alternatives)); } throw new Twig_Error_Syntax($message, $this->getLine(), $compiler->getFilename()); } $compiler->raw($function->compile() . '('); $first = true; if ($function->needsEnvironment()) { $compiler->raw('$this->env'); $first = false; } if ($function->needsContext()) { if (!$first) { $compiler->raw(', '); } $compiler->raw('$context'); $first = false; } foreach ($function->getArguments() as $argument) { if (!$first) { $compiler->raw(', '); } $compiler->string($argument); $first = false; } foreach ($this->getNode('arguments') as $node) { if (!$first) { $compiler->raw(', '); } $compiler->subcompile($node); $first = false; } $compiler->raw(')'); }
protected function addGetTemplate(Twig_Compiler $compiler) { $compiler->write('$this->loadTemplate(')->subcompile($this->getNode('expr'))->raw(', ')->repr($compiler->getFilename())->raw(', ')->repr($this->getLine())->raw(')'); }
protected function compileLoadTemplate(Twig_Compiler $compiler, $node, $var) { if ($node instanceof Twig_Node_Expression_Constant) { $compiler->write(sprintf("%s = \$this->loadTemplate(", $var))->subcompile($node)->raw(', ')->repr($compiler->getFilename())->raw(', ')->repr($node->getLine())->raw(");\n"); } else { $compiler->write(sprintf("%s = ", $var))->subcompile($node)->raw(";\n")->write(sprintf("if (!%s", $var))->raw(" instanceof Twig_Template) {\n")->indent()->write(sprintf("%s = \$this->loadTemplate(%s")->raw(', ')->repr($compiler->getFilename())->raw(', ')->repr($node->getLine())->raw(");\n", $var, $var))->outdent()->write("}\n"); } }
protected function compileLoadTemplate(Twig_Compiler $compiler, $node, $var) { if ($node instanceof Twig_Node_Expression_Constant) { $compiler->write(sprintf('%s = $this->loadTemplate(', $var))->subcompile($node)->raw(', ')->repr($compiler->getFilename())->raw(', ')->repr($node->getLine())->raw(");\n"); } else { throw new LogicException('Trait templates can only be constant nodes'); } }
protected function addGetTemplate(Twig_Compiler $compiler) { $compiler->write("\$this->loadTemplate(")->string($this->getAttribute('filename'))->raw(', ')->repr($compiler->getFilename())->raw(', ')->repr($this->getLine())->raw(', ')->string($this->getAttribute('index'))->raw(")"); }
protected function compileGetParent(Twig_Compiler $compiler) { if (null === ($parent = $this->getNode('parent'))) { return; } $compiler->write("protected function doGetParent(array \$context)\n", "{\n")->indent()->addDebugInfo($parent)->write('return '); if ($parent instanceof Twig_Node_Expression_Constant) { $compiler->subcompile($parent); } else { $compiler->raw('$this->loadTemplate(')->subcompile($parent)->raw(', ')->repr($compiler->getFilename())->raw(', ')->repr($this->getNode('parent')->getLine())->raw(')'); } $compiler->raw(";\n")->outdent()->write("}\n\n"); }