コード例 #1
0
 /**
  * @param \Twig_NodeInterface $node
  * @param \Twig_Environment $env
  * @return \Twig_NodeInterface
  */
 public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
 {
     $this->stack[] = $node;
     if ($node instanceof TransNode) {
         $id = $node->getNode('body')->getAttribute('data');
         $domain = 'messages';
         // Older version of Symfony are storing null in the node instead of omitting it
         if ($node->hasNode('domain') && null !== ($domainNode = $node->getNode('domain'))) {
             $domain = $domainNode->getAttribute('value');
         }
         $message = new Message($id, $domain);
         $message->addSource($this->fileSourceFactory->create($this->file, $node->getLine()));
         $this->catalogue->add($message);
     } elseif ($node instanceof \Twig_Node_Expression_Filter) {
         $name = $node->getNode('filter')->getAttribute('value');
         if ('trans' === $name || 'transchoice' === $name) {
             $idNode = $node->getNode('node');
             if (!$idNode instanceof \Twig_Node_Expression_Constant) {
                 return $node;
                 // FIXME: see below
                 //                     throw new \RuntimeException(sprintf('Cannot infer translation id from node "%s". Please refactor to only translate constants.', get_class($idNode)));
             }
             $id = $idNode->getAttribute('value');
             $index = 'trans' === $name ? 1 : 2;
             $domain = 'messages';
             $arguments = $node->getNode('arguments');
             if ($arguments->hasNode($index)) {
                 $argument = $arguments->getNode($index);
                 if (!$argument instanceof \Twig_Node_Expression_Constant) {
                     return $node;
                     // FIXME: Throw exception if there is some way for the user to turn this off
                     //        on a case-by-case basis, similar to @Ignore in PHP
                 }
                 $domain = $argument->getAttribute('value');
             }
             $message = new Message($id, $domain);
             $message->addSource($this->fileSourceFactory->create($this->file, $node->getLine()));
             for ($i = count($this->stack) - 2; $i >= 0; $i -= 1) {
                 if (!$this->stack[$i] instanceof \Twig_Node_Expression_Filter) {
                     break;
                 }
                 $name = $this->stack[$i]->getNode('filter')->getAttribute('value');
                 if ('desc' === $name || 'meaning' === $name) {
                     $arguments = $this->stack[$i]->getNode('arguments');
                     if (!$arguments->hasNode(0)) {
                         throw new RuntimeException(sprintf('The "%s" filter requires exactly one argument, the description text.', $name));
                     }
                     $text = $arguments->getNode(0);
                     if (!$text instanceof \Twig_Node_Expression_Constant) {
                         throw new RuntimeException(sprintf('The first argument of the "%s" filter must be a constant expression, such as a string.', $name));
                     }
                     $message->{'set' . $name}($text->getAttribute('value'));
                 } elseif ('trans' === $name) {
                     break;
                 }
             }
             $this->catalogue->add($message);
         }
     }
     return $node;
 }