Exemplo n.º 1
0
 public function compile(JsCompiler $compiler, \Twig_NodeInterface $node)
 {
     if (!$node instanceof \Twig_Node_Expression_Constant) {
         throw new \RuntimeException(sprintf('$node must be an instanceof of \\Expression_Constant, but got "%s".', get_class($node)));
     }
     if ($compiler->isTemplateName) {
         $env = $compiler->getEnvironment();
         $source = $env->getLoader()->getSource($node->getAttribute('value'));
         $module = $env->parse($env->tokenize($source, $node->getAttribute('value')));
         $compiler->raw($compiler->getFunctionName($module));
         return;
     }
     $compiler->repr($node->getAttribute('value'));
 }
Exemplo n.º 2
0
 protected function compileClassHeader(JsCompiler $compiler, Twig_NodeInterface $node)
 {
     $this->functionName = $functionName = $compiler->templateFunctionName = $compiler->getFunctionName($node);
     $parts = explode('.', $functionName);
     array_pop($parts);
     $filename = $node->getAttribute('filename');
     if (!empty($filename) && false !== strpos($filename, DIRECTORY_SEPARATOR)) {
         $parts = explode(DIRECTORY_SEPARATOR, realpath($filename));
         $filename = implode(DIRECTORY_SEPARATOR, array_splice($parts, -4));
     }
     $compiler->write("/**\n")->write(" * @fileoverview Compiled template for file\n")->write(" *\n")->write(" * " . str_replace('*/', '*\\/', $filename) . "\n")->write(" *\n")->write(" * @suppress {checkTypes|fileoverviewTags}\n")->write(" */\n")->write("\n");
     $compiler->write("goog.provide('{$functionName}');\n")->write("\n")->write("goog.require('twig');\n")->write("goog.require('twig.filter');\n")->write("\n")->write("/**\n", " * @constructor\n", " * @param {twig.Environment} env\n", " * @extends {twig.Template}\n", " */\n")->write("{$functionName} = function(env) {\n")->indent()->write("twig.Template.call(this, env);\n");
     if (count($node->getNode('blocks')) || count($node->getNode('traits'))) {
         $this->compileConstructor($compiler, $node);
     }
     $compiler->outdent()->write("};\n")->write("twig.inherits({$functionName}, twig.Template);\n\n");
 }
 /**
  * Parses module dependencies from Twig AST.
  *
  * @param JsCompiler          $compiler
  * @param \Twig_NodeInterface $node
  *
  * @return array of dependencies
  */
 private function getDependencies(JsCompiler $compiler, \Twig_NodeInterface $node)
 {
     $dependencies = ['files' => [], 'functions' => []];
     $stack = iterator_to_array($node->getIterator());
     while (count($stack)) {
         $subNode = array_shift($stack);
         if (!$subNode instanceof \Twig_Node) {
             continue;
         }
         if ($subNode instanceof \Twig_Node_Include || $subNode instanceof \Twig_Node_Import) {
             $expr = $subNode->getNode('expr');
             $env = $compiler->getEnvironment();
             $path = $expr->getAttribute('value');
             $source = $env->getLoader()->getSource($path);
             $module = $env->parse($env->tokenize($source, $path));
             $name = $compiler->getFunctionName($module);
             array_push($dependencies['files'], $name . 'Template');
             array_push($dependencies['functions'], $name);
         } elseif ($subNode->count()) {
             $stack = array_merge(iterator_to_array($subNode->getIterator()), $stack);
         }
     }
     return $dependencies;
 }