Beispiel #1
0
 protected function compileString(\Twig_NodeInterface $body, \Twig_Node_Expression_Array $vars, $ignoreStrictCheck = false)
 {
     if ($body instanceof \Twig_Node_Expression_Constant) {
         $msg = $body->getAttribute('value');
     } elseif ($body instanceof \Twig_Node_Text) {
         $msg = $body->getAttribute('data');
     } else {
         return array($body, $vars);
     }
     preg_match_all('/(?<!%)%([^%]+)%/', $msg, $matches);
     foreach ($matches[1] as $var) {
         $key = new \Twig_Node_Expression_Constant('%' . $var . '%', $body->getLine());
         if (!$vars->hasElement($key)) {
             if ('count' === $var && null !== $this->getNode('count')) {
                 $vars->addElement($this->getNode('count'), $key);
             } else {
                 $varExpr = new \Twig_Node_Expression_Name($var, $body->getLine());
                 $varExpr->setAttribute('ignore_strict_check', $ignoreStrictCheck);
                 $vars->addElement($varExpr, $key);
             }
         }
     }
     return array(new \Twig_Node_Expression_Constant(str_replace('%%', '%', trim($msg)), $body->getLine()), $vars);
 }
Beispiel #2
0
 public function testConstructor()
 {
     $node = new Twig_Node_Expression_Name('foo', 1);
     $this->assertEquals('foo', $node->getAttribute('name'));
 }
Beispiel #3
0
 /**
  * Extracts the text and tokens for the "trans" tag.
  *
  * @param \Twig_Node $body
  *   The node to compile.
  *
  * @return array
  *   Returns an array containing the two following parameters:
  *   - string $text
  *       The extracted text.
  *   - array $tokens
  *       The extracted tokens as new \Twig_Node_Expression_Name instances.
  */
 protected function compileString(\Twig_Node $body)
 {
     if ($body instanceof \Twig_Node_Expression_Name || $body instanceof \Twig_Node_Expression_Constant || $body instanceof \Twig_Node_Expression_TempName) {
         return array($body, array());
     }
     $tokens = array();
     if (count($body)) {
         $text = '';
         foreach ($body as $node) {
             if (get_class($node) === 'Twig_Node' && $node->getNode(0) instanceof \Twig_Node_SetTemp) {
                 $node = $node->getNode(1);
             }
             if ($node instanceof \Twig_Node_Print) {
                 $n = $node->getNode('expr');
                 while ($n instanceof \Twig_Node_Expression_Filter) {
                     $n = $n->getNode('node');
                 }
                 $args = $n;
                 // Support TwigExtension->renderVar() function in chain.
                 if ($args instanceof \Twig_Node_Expression_Function) {
                     $args = $n->getNode('arguments')->getNode(0);
                 }
                 // Detect if a token implements one of the filters reserved for
                 // modifying the prefix of a token. The default prefix used for
                 // translations is "@". This escapes the printed token and makes them
                 // safe for templates.
                 // @see TwigExtension::getFilters()
                 $argPrefix = '@';
                 while ($args instanceof \Twig_Node_Expression_Filter) {
                     switch ($args->getNode('filter')->getAttribute('value')) {
                         case 'placeholder':
                             $argPrefix = '%';
                             break;
                     }
                     $args = $args->getNode('node');
                 }
                 if ($args instanceof \Twig_Node_Expression_GetAttr) {
                     $argName = array();
                     // Reuse the incoming expression.
                     $expr = $args;
                     // Assemble a valid argument name by walking through the expression.
                     $argName[] = $args->getNode('attribute')->getAttribute('value');
                     while ($args->hasNode('node')) {
                         $args = $args->getNode('node');
                         if ($args instanceof \Twig_Node_Expression_Name) {
                             $argName[] = $args->getAttribute('name');
                         } else {
                             $argName[] = $args->getNode('attribute')->getAttribute('value');
                         }
                     }
                     $argName = array_reverse($argName);
                     $argName = implode('.', $argName);
                 } else {
                     $argName = $n->getAttribute('name');
                     if (!is_null($args)) {
                         $argName = $args->getAttribute('name');
                     }
                     $expr = new \Twig_Node_Expression_Name($argName, $n->getLine());
                 }
                 $placeholder = sprintf('%s%s', $argPrefix, $argName);
                 $text .= $placeholder;
                 $expr->setAttribute('placeholder', $placeholder);
                 $tokens[] = $expr;
             } else {
                 $text .= $node->getAttribute('data');
             }
         }
     } else {
         $text = $body->getAttribute('data');
     }
     return array(new \Twig_Node(array(new \Twig_Node_Expression_Constant(trim($text), $body->getLine()))), $tokens);
 }
Beispiel #4
0
 public function getFunctionNode(Twig_Node_Expression_Name $node)
 {
     $args = $this->parseArguments();
     if ('parent' === $node->getAttribute('name')) {
         if (!count($this->parser->getBlockStack())) {
             throw new Twig_Error_Syntax('Calling "parent" outside a block is forbidden', $node->getLine());
         }
         if (!$this->parser->getParent()) {
             throw new Twig_Error_Syntax('Calling "parent" on a template that does not extend another one is forbidden', $node->getLine());
         }
         return new Twig_Node_Expression_Parent($this->parser->peekBlockStack(), $node->getLine());
     }
     if ('block' === $node->getAttribute('name')) {
         return new Twig_Node_Expression_BlockReference($args->getNode(0), false, $node->getLine());
     }
     if (null !== ($alias = $this->parser->getImportedFunction($node->getAttribute('name')))) {
         return new Twig_Node_Expression_GetAttr($alias['node'], new Twig_Node_Expression_Constant($alias['name'], $node->getLine()), $args, Twig_TemplateInterface::METHOD_CALL, $node->getLine());
     }
     return new Twig_Node_Expression_Function($node, $args, $node->getLine());
 }