Пример #1
0
 public function compile(JsCompiler $compiler, \Twig_NodeInterface $node)
 {
     if (!$node instanceof \Twig_Node_Expression_Function) {
         throw new \RuntimeException(sprintf('$node must be an instanceof of \\Expression_Function, but got "%s".', get_class($node)));
     }
     $function = $compiler->getEnvironment()->getFunction($node->getAttribute('name'));
     if (false === $function) {
         throw new \Twig_Error_Syntax(sprintf('The function "%s" does not exist', $node->getAttribute('name')), $node->getLine());
     }
     if ($jsFunction = $compiler->getJsFunction($node->getAttribute('name'))) {
         $compiler->raw($jsFunction . '(');
     } else {
         $compiler->raw('this.env_.invoke(')->string($node->getAttribute('name'))->raw(', ');
     }
     $compiler->raw($function->needsEnvironment() ? 'this.env_' : '');
     if ($function->needsContext()) {
         $compiler->raw($function->needsEnvironment() ? ', context' : 'context');
     }
     $first = true;
     foreach ($node->getNode('arguments') as $argNode) {
         if (!$first) {
             $compiler->raw(', ');
         } else {
             if ($function->needsEnvironment() || $function->needsContext()) {
                 $compiler->raw(', ');
             }
             $first = false;
         }
         $compiler->subcompile($argNode);
     }
     $compiler->raw(')');
 }
Пример #2
0
 public function compile(JsCompiler $compiler, \Twig_NodeInterface $node)
 {
     if (!$node instanceof \Twig_Node_Expression_GetAttr) {
         throw new \RuntimeException(sprintf('$node must be an instanceof of \\Expression_GetAttr, but got "%s".', get_class($node)));
     }
     $compiler->raw('twig.attr(');
     if ($node->getAttribute('is_defined_test') && $compiler->getEnvironment()->isStrictVariables()) {
         $compiler->subcompile(new \Twig_Node_Expression_Filter($node->getNode('node'), new \Twig_Node_Expression_Constant('default', $node->getLine()), new \Twig_Node(), $node->getLine()));
     } else {
         $compiler->subcompile($node->getNode('node'));
     }
     $compiler->raw(', ')->subcompile($node->getNode('attribute'));
     $defaultArguments = 0 === count($node->getNode('arguments'));
     $defaultAccess = \Twig_TemplateInterface::ANY_CALL === $node->getAttribute('type');
     $defaultTest = false == $node->getAttribute('is_defined_test');
     if (!$defaultArguments) {
         $compiler->raw(', ')->subcompile($node->getNode('arguments'));
     } elseif (!$defaultAccess || !$defaultTest) {
         $compiler->raw(', undefined');
     }
     if (!$defaultAccess) {
         $compiler->raw(', ')->repr($node->getAttribute('type'));
     } elseif (!$defaultTest) {
         $compiler->raw(', undefined');
     }
     if (!$defaultTest) {
         $compiler->raw(', ' . ($node->getAttribute('is_defined_test') ? 'true' : 'false'));
     }
     $compiler->raw(')');
 }
Пример #3
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'));
 }
Пример #4
0
 public function compile(JsCompiler $compiler, \Twig_NodeInterface $node)
 {
     if (!$node instanceof \Twig_Node_Expression_Filter) {
         throw new \RuntimeException(sprintf('$node must be an instanceof of \\Twig_Node_Expression_Filter, but got "%s".', get_class($node)));
     }
     $name = $node->getNode('filter')->getAttribute('value');
     if (false === ($filter = $compiler->getEnvironment()->getFilter($name))) {
         throw new \Twig_Error_Syntax(sprintf('The filter "%s" does not exist', $name), $node->getLine());
     }
     if (($filterCompiler = $compiler->getFilterCompiler($name)) && false !== $filterCompiler->compile($compiler, $node)) {
         return;
     } elseif ($functionName = $compiler->getFilterFunction($name)) {
         $compiler->raw($functionName . '(');
     } else {
         $compiler->raw('this.env_.filter(')->string($name)->raw(', ');
     }
     $compiler->raw($filter->needsEnvironment() ? 'this.env_, ' : '')->raw($filter->needsContext() ? 'context, ' : '')->subcompile($node->getNode('node'));
     foreach ($node->getNode('arguments') as $subNode) {
         $compiler->raw(', ')->subcompile($subNode);
     }
     $compiler->raw(')');
 }
 /**
  * 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;
 }