コード例 #1
0
ファイル: NodeVisitor.php プロジェクト: davyrolink/TFD7
 function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
 {
     if ($node instanceof Twig_Node_Print) {
         // make sure that every {{ }} printed object is handled as a TFD_Node_Render node (aka autorender)
         if (!$node->getNode('expr') instanceof Twig_Node_Expression_Function) {
             if ($env->isAutoRender()) {
                 $targetNode = $node->getNode('expr');
                 if ($targetNode instanceof Twig_Node_Expression_Name) {
                     $targetNode->setAttribute('always_defined', TRUE);
                 }
                 if (!$targetNode instanceof Twig_Node_Expression_MethodCall) {
                     $node = new TFD_Node_Render($targetNode, $node->getLine(), $node->getNodeTag());
                 }
             }
         } elseif ($node->getNode('expr') instanceof Twig_Node_Expression_Function) {
             $targetNode = $node->getNode('expr');
             if ($targetNode->getAttribute('name') == 'hide') {
                 $targetNode = $this->castObject('TFD_Node_Expression_Nocall', $targetNode);
                 $targetNode->setAttribute('always_defined', TRUE);
                 $node = new TFD_Node_Hide($targetNode, $node->getLine(), $node->getNodeTag());
             }
         }
     }
     return $node;
 }
コード例 #2
0
ファイル: TwigFileExtractor.php プロジェクト: kazak/forum
 /**
  * @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';
         if (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;
 }
コード例 #3
0
ファイル: AsseticNodeVisitor.php プロジェクト: nacef/symfony
    /**
     * Extracts formulae from filter function nodes.
     *
     * @return array|null The formula
     */
    private function checkNode(\Twig_NodeInterface $node, \Twig_Environment $env)
    {
        if ($node instanceof \Twig_Node_Expression_Function) {
            $name = $node->getNode('name')->getAttribute('name');
            if ($env->getFunction($name) instanceof AsseticFilterFunction) {
                $arguments = array();
                foreach ($node->getNode('arguments') as $argument) {
                    $arguments[] = eval('return '.$env->compile($argument).';');
                }

                $invoker = $env->getExtension('assetic')->getFilterInvoker($name);
                $factory = $invoker->getFactory();

                $inputs = isset($arguments[0]) ? (array) $arguments[0] : array();
                $filters = $invoker->getFilters();
                $options = array_replace($invoker->getOptions(), isset($arguments[1]) ? $arguments[1] : array());

                if (!isset($options['name'])) {
                    $options['name'] = $factory->generateAssetName($inputs, $filters);
                }

                return array($inputs, $filters, $options);
            }
        }
    }
コード例 #4
0
 public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env)
 {
     if ($node instanceof \Twig_Node_Expression_Binary_Concat && ($left = $node->getNode('left')) instanceof \Twig_Node_Expression_Constant && ($right = $node->getNode('right')) instanceof \Twig_Node_Expression_Constant) {
         return new \Twig_Node_Expression_Constant($left->getAttribute('value') . $right->getAttribute('value'), $left->getLine());
     }
     return $node;
 }
コード例 #5
0
ファイル: Twig.php プロジェクト: tigron/skeleton-i18n
 /**
  * Defined by Twig_NodeVisitorInterface
  *
  * Extracts messages from calls to the translate function.
  */
 public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
 {
     if ($node instanceof \Skeleton\I18n\Template\Twig\Extension\Node\Trans\Tigron) {
         if ($node->getNodeTag() == 'trans') {
             $this->extracted[] = $node->getNode('body')->getAttribute('value');
         }
     } elseif ($node instanceof \Twig_Node_Print) {
         $n = $node->getNode('expr');
         while ($n instanceof \Twig_Node_Expression_Filter) {
             $filter = null;
             if ($n->hasNode('filter')) {
                 $filter = $n->getNode('filter')->getAttribute('value');
             }
             $n = $n->getNode('node');
             if ($n instanceof \Twig_Node_Expression_Constant and $filter == 'trans') {
                 $this->extracted[] = $n->getAttribute('value');
             }
         }
     } elseif ($node instanceof \Twig_Node_Expression_Array) {
         $data = $node->getIterator();
         foreach ($data as $row) {
             if ($row instanceof \Twig_Node_Expression_Filter) {
                 if ($row->hasNode('filter') and $row->getNode('filter')->getAttribute('value') == 'trans') {
                     $this->extracted[] = $row->getNode('node')->getAttribute('value');
                 }
             }
         }
     }
     return $node;
 }
コード例 #6
0
ファイル: Sandbox.php プロジェクト: kuzmichus/schoolreg
 /**
  * Called before child nodes are visited.
  *
  * @param Twig_NodeInterface $node The node to visit
  * @param Twig_Environment   $env  The Twig environment instance
  *
  * @param Twig_NodeInterface The modified node
  */
 public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
 {
     if ($node instanceof Twig_Node_Module) {
         $this->inAModule = true;
         $this->tags = array();
         $this->filters = array();
         return $node;
     } elseif ($this->inAModule) {
         // look for tags
         if ($node->getNodeTag()) {
             $this->tags[] = $node->getNodeTag();
         }
         // look for filters
         if ($node instanceof Twig_Node_Expression_Filter) {
             for ($i = 0; $i < count($node->getNode('filters')); $i += 2) {
                 $this->filters[] = $node->getNode('filters')->getNode($i)->getAttribute('value');
             }
         }
         // look for simple print statements ({{ article }})
         if ($node instanceof Twig_Node_Print && $node->getNode('expr') instanceof Twig_Node_Expression_Name) {
             return new Twig_Node_SandboxedPrint($node);
         }
     }
     return $node;
 }
コード例 #7
0
 /**
  * {@inheritdoc}
  */
 public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
 {
     if ($node instanceof \Twig_Node_Module) {
         $this->domain = null;
     }
     if ($node instanceof TransDefaultDomainNode) {
         $var = $env->getParser()->getVarName();
         $name = new \Twig_Node_Expression_AssignName($var, $node->getLine());
         $this->domain = new \Twig_Node_Expression_Name($var, $node->getLine());
         return new \Twig_Node_Set(false, new \Twig_Node(array($name)), new \Twig_Node(array($node->getNode('expr'))), $node->getLine());
     }
     if (null === $this->domain) {
         return $node;
     }
     if ($node instanceof \Twig_Node_Expression_Filter && in_array($node->getNode('filter')->getAttribute('value'), array('trans', 'transchoice'))) {
         $ind = 'trans' === $node->getNode('filter')->getAttribute('value') ? 1 : 2;
         $arguments = $node->getNode('arguments');
         if (!$arguments->hasNode($ind)) {
             if (!$arguments->hasNode($ind - 1)) {
                 $arguments->setNode($ind - 1, new \Twig_Node_Expression_Array(array(), $node->getLine()));
             }
             $arguments->setNode($ind, $this->domain);
         }
     } elseif ($node instanceof TransNode) {
         if (null === $node->getNode('domain')) {
             $node->setNode('domain', $this->domain);
         }
     }
     return $node;
 }
コード例 #8
0
 /**
  * Removes "raw" filters.
  *
  * @param Twig_NodeInterface $node A Node
  * @param Twig_Environment   $env  The current Twig environment
  */
 protected function optimizeRawFilter($node, $env)
 {
     if ($node instanceof Twig_Node_Expression_Filter && 'raw' == $node->getNode('filter')->getAttribute('value')) {
         return $node->getNode('node');
     }
     return $node;
 }
コード例 #9
0
ファイル: Sandbox.php プロジェクト: eroslover/drupal7
 /**
  * Called before child nodes are visited.
  *
  * @param Twig_NodeInterface $node The node to visit
  * @param Twig_Environment   $env  The Twig environment instance
  *
  * @return Twig_NodeInterface The modified node
  */
 public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
 {
     if ($node instanceof Twig_Node_Module) {
         $this->inAModule = true;
         $this->tags = array();
         $this->filters = array();
         $this->functions = array();
         return $node;
     } elseif ($this->inAModule) {
         // look for tags
         if ($node->getNodeTag() && !isset($this->tags[$node->getNodeTag()])) {
             $this->tags[$node->getNodeTag()] = $node;
         }
         // look for filters
         if ($node instanceof Twig_Node_Expression_Filter && !isset($this->filters[$node->getNode('filter')->getAttribute('value')])) {
             $this->filters[$node->getNode('filter')->getAttribute('value')] = $node;
         }
         // look for functions
         if ($node instanceof Twig_Node_Expression_Function && !isset($this->functions[$node->getAttribute('name')])) {
             $this->functions[$node->getAttribute('name')] = $node;
         }
         // wrap print to check __toString() calls
         if ($node instanceof Twig_Node_Print) {
             return new Twig_Node_SandboxedPrint($node->getNode('expr'), $node->getLine(), $node->getNodeTag());
         }
     }
     return $node;
 }
コード例 #10
0
 public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
 {
     if ($this->enabled && $node instanceof \Twig_Node_Expression_Filter) {
         $name = $node->getNode('filter')->getAttribute('value');
         if ('desc' === $name || 'meaning' === $name) {
             return $this->enterNode($node->getNode('node'), $env);
         }
     }
     return $node;
 }
コード例 #11
0
ファイル: Profiler.php プロジェクト: neteasy-work/hkgbf_crm
 /**
  * {@inheritdoc}
  */
 public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
 {
     if ($node instanceof Twig_Node_Module) {
         $varName = $this->getVarName();
         $node->setNode('display_start', new Twig_Node(array(new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::TEMPLATE, $node->getAttribute('filename'), $varName), $node->getNode('display_start'))));
         $node->setNode('display_end', new Twig_Node(array(new Twig_Profiler_Node_LeaveProfile($varName), $node->getNode('display_end'))));
     } elseif ($node instanceof Twig_Node_Block) {
         $varName = $this->getVarName();
         $node->setNode('body', new Twig_Node_Body(array(new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::BLOCK, $node->getAttribute('name'), $varName), $node->getNode('body'), new Twig_Profiler_Node_LeaveProfile($varName))));
     } elseif ($node instanceof Twig_Node_Macro) {
         $varName = $this->getVarName();
         $node->setNode('body', new Twig_Node_Body(array(new Twig_Profiler_Node_EnterProfile($this->extensionName, Twig_Profiler_Profile::MACRO, $node->getAttribute('name'), $varName), $node->getNode('body'), new Twig_Profiler_Node_LeaveProfile($varName))));
     }
     return $node;
 }
コード例 #12
0
ファイル: Optimizer.php プロジェクト: LiGhT1EsS/yii2
 /**
  * @inheritdoc
  */
 public function leaveNode(\Twig_NodeInterface $node, \Twig_Environment $env)
 {
     if ($node instanceof \Twig_Node_Print) {
         $expression = $node->getNode('expr');
         if ($expression instanceof \Twig_Node_Expression_Function) {
             $name = $expression->getAttribute('name');
             if (preg_match('/^(?:register_.+_asset|use|.+_begin|.+_end)$/', $name)) {
                 return new \Twig_Node_Do($expression, $expression->getLine());
             } elseif (in_array($name, ['begin_page', 'end_page', 'begin_body', 'end_body', 'head'])) {
                 $arguments = [new \Twig_Node_Expression_Constant($name, $expression->getLine())];
                 if ($expression->hasNode('arguments') && $expression->getNode('arguments') !== null) {
                     foreach ($expression->getNode('arguments') as $key => $value) {
                         if (is_int($key)) {
                             $arguments[] = $value;
                         } else {
                             $arguments[$key] = $value;
                         }
                     }
                 }
                 $expression->setNode('arguments', new \Twig_Node($arguments));
                 return new \Twig_Node_Do($expression, $expression->getLine());
             }
         }
     }
     return $node;
 }
コード例 #13
0
 /**
  * Called after child nodes are visited.
  *
  * @param Twig_NodeInterface $node The node to visit
  * @param Twig_Environment   $env  The Twig environment instance
  *
  * @return Twig_NodeInterface The modified node
  */
 public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
 {
     if ($node instanceof Twig_Node_Module) {
         $this->inAModule = false;
         $node->setNode('display_start', new Twig_Node(array(new Twig_Node_CheckSecurity($this->filters, $this->tags, $this->functions), $node->getNode('display_start'))));
     }
     return $node;
 }
コード例 #14
0
 /**
  * {@inheritdoc}
  */
 public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
 {
     if (!$this->enabled) {
         return $node;
     }
     if ($node instanceof \Twig_Node_Expression_Filter && 'trans' === $node->getNode('filter')->getAttribute('value') && $node->getNode('node') instanceof \Twig_Node_Expression_Constant) {
         // extract constant nodes with a trans filter
         $this->messages[] = array($node->getNode('node')->getAttribute('value'), $this->getReadDomainFromArguments($node->getNode('arguments'), 1));
     } elseif ($node instanceof \Twig_Node_Expression_Filter && 'transchoice' === $node->getNode('filter')->getAttribute('value') && $node->getNode('node') instanceof \Twig_Node_Expression_Constant) {
         // extract constant nodes with a trans filter
         $this->messages[] = array($node->getNode('node')->getAttribute('value'), $this->getReadDomainFromArguments($node->getNode('arguments'), 2));
     } elseif ($node instanceof TransNode) {
         // extract trans nodes
         $this->messages[] = array($node->getNode('body')->getAttribute('data'), $this->getReadDomainFromNode($node->getNode('domain')));
     }
     return $node;
 }
コード例 #15
0
 /**
  * @param \Twig_NodeInterface $node
  * @param \Twig_Environment $env
  * @return \Twig_NodeInterface
  */
 public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
 {
     if (!$this->enabled) {
         return $node;
     }
     if ($node instanceof \Twig_Node_Expression_Filter && 'desc' === $node->getNode('filter')->getAttribute('value')) {
         $transNode = $node->getNode('node');
         while ($transNode instanceof \Twig_Node_Expression_Filter && 'trans' !== $transNode->getNode('filter')->getAttribute('value') && 'transchoice' !== $transNode->getNode('filter')->getAttribute('value')) {
             $transNode = $transNode->getNode('node');
         }
         if (!$transNode instanceof \Twig_Node_Expression_Filter) {
             throw new RuntimeException(sprintf('The "desc" filter must be applied after a "trans", or "transchoice" filter.'));
         }
         $wrappingNode = $node->getNode('node');
         $testNode = clone $wrappingNode;
         $defaultNode = $node->getNode('arguments')->getNode(0);
         // if the |transchoice filter is used, delegate the call to the TranslationExtension
         // so that we can catch a possible exception when the default translation has not yet
         // been extracted
         if ('transchoice' === $transNode->getNode('filter')->getAttribute('value')) {
             $transchoiceArguments = new \Twig_Node_Expression_Array(array(), $transNode->getLine());
             $transchoiceArguments->addElement($wrappingNode->getNode('node'));
             $transchoiceArguments->addElement($defaultNode);
             foreach ($wrappingNode->getNode('arguments') as $arg) {
                 $transchoiceArguments->addElement($arg);
             }
             $transchoiceNode = new \Twig_Node_Expression_MethodCall(new \Twig_Node_Expression_ExtensionReference('jms_translation', $transNode->getLine()), 'transchoiceWithDefault', $transchoiceArguments, $transNode->getLine());
             $node->setNode('node', $transchoiceNode);
             return $node;
         }
         // if the |trans filter has replacements parameters
         // (e.g. |trans({'%foo%': 'bar'}))
         if ($wrappingNode->getNode('arguments')->hasNode(0)) {
             $lineno = $wrappingNode->getLine();
             // remove the replacements from the test node
             $testNode->setNode('arguments', clone $testNode->getNode('arguments'));
             $testNode->getNode('arguments')->setNode(0, new \Twig_Node_Expression_Array(array(), $lineno));
             // wrap the default node in a |replace filter
             $defaultNode = new \Twig_Node_Expression_Filter(clone $node->getNode('arguments')->getNode(0), new \Twig_Node_Expression_Constant('replace', $lineno), new \Twig_Node(array(clone $wrappingNode->getNode('arguments')->getNode(0))), $lineno);
         }
         $condition = new \Twig_Node_Expression_Conditional(new \Twig_Node_Expression_Binary_Equal($testNode, $transNode->getNode('node'), $wrappingNode->getLine()), $defaultNode, clone $wrappingNode, $wrappingNode->getLine());
         $node->setNode('node', $condition);
     }
     return $node;
 }
コード例 #16
0
ファイル: For.php プロジェクト: Jimm31/Kassa
 protected function checkLoopUsageBody(Twig_TokenStream $stream, Twig_NodeInterface $node)
 {
     if ($node instanceof Twig_Node_Expression_GetAttr && $node->getNode('node') instanceof Twig_Node_Expression_Name && 'loop' == $node->getNode('node')->getAttribute('name')) {
         $attribute = $node->getNode('attribute');
         if ($attribute instanceof Twig_Node_Expression_Constant && in_array($attribute->getAttribute('value'), array('length', 'revindex0', 'revindex', 'last'))) {
             throw new Twig_Error_Syntax(sprintf('The "loop.%s" variable is not defined when looping with a condition', $attribute->getAttribute('value')), $node->getLine(), $stream->getFilename());
         }
     }
     // should check for parent.loop.XXX usage
     if ($node instanceof Twig_Node_For) {
         return;
     }
     foreach ($node as $n) {
         if (!$n) {
             continue;
         }
         $this->checkLoopUsageBody($stream, $n);
     }
 }
コード例 #17
0
 /**
  * {@inheritdoc}
  */
 public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
 {
     if ($node instanceof RegionNode) {
         $this->regions[] = $node->getAttributes();
         return $node;
     }
     if ($node instanceof \Twig_Node_Include) {
         $this->includes[] = $node->getNode('expr')->getAttribute('value');
         return $node;
     }
     return $node;
 }
コード例 #18
0
ファイル: Sandbox.php プロジェクト: Zyko0/Baikal
 /**
  * Called before child nodes are visited.
  *
  * @param Twig_NodeInterface $node The node to visit
  * @param Twig_Environment   $env  The Twig environment instance
  *
  * @return Twig_NodeInterface The modified node
  */
 public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
 {
     // in a sandbox tag, only include tags are allowed
     if ($node instanceof Twig_Node_Sandbox && !$node->getNode('body') instanceof Twig_Node_Include) {
         foreach ($node->getNode('body') as $n) {
             if ($n instanceof Twig_Node_Text && ctype_space($n->getAttribute('data'))) {
                 continue;
             }
             if (!$n instanceof Twig_Node_Include) {
                 throw new Twig_Error_Syntax('Only "include" tags are allowed within a "sandbox" section', $n->getLine());
             }
         }
     }
     if ($node instanceof Twig_Node_Module) {
         $this->inAModule = true;
         $this->tags = array();
         $this->filters = array();
         $this->functions = array();
         return $node;
     } elseif ($this->inAModule) {
         // look for tags
         if ($node->getNodeTag()) {
             $this->tags[] = $node->getNodeTag();
         }
         // look for filters
         if ($node instanceof Twig_Node_Expression_Filter) {
             $this->filters[] = $node->getNode('filter')->getAttribute('value');
         }
         // look for functions
         if ($node instanceof Twig_Node_Expression_Function) {
             $this->functions[] = $node->getAttribute('name');
         }
         // wrap print to check __toString() calls
         if ($node instanceof Twig_Node_Print) {
             return new Twig_Node_SandboxedPrint($node->getNode('expr'), $node->getLine(), $node->getNodeTag());
         }
     }
     return $node;
 }
コード例 #19
0
 /**
  * Called before child nodes are visited.
  *
  * @param Twig_NodeInterface $node The node to visit
  * @param Twig_Environment   $env  The Twig environment instance
  *
  * @return Twig_NodeInterface The modified node
  */
 public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
 {
     if ($node instanceof Twig_Node_Module) {
         if ($env->hasExtension('escaper') && ($defaultStrategy = $env->getExtension('escaper')->getDefaultStrategy($node->getAttribute('filename')))) {
             $this->defaultStrategy = $defaultStrategy;
         }
         $this->safeVars = array();
     } elseif ($node instanceof Twig_Node_AutoEscape) {
         $this->statusStack[] = $node->getAttribute('value');
     } elseif ($node instanceof Twig_Node_Block) {
         $this->statusStack[] = isset($this->blocks[$node->getAttribute('name')]) ? $this->blocks[$node->getAttribute('name')] : $this->needEscaping($env);
     } elseif ($node instanceof Twig_Node_Import) {
         $this->safeVars[] = $node->getNode('var')->getAttribute('name');
     }
     return $node;
 }
コード例 #20
0
 /**
  * Called before child nodes are visited.
  *
  * @param \Twig_NodeInterface $node The node to visit
  * @param \Twig_Environment $env  The Twig environment instance
  *
  * @return Twig_NodeInterface The modified node
  */
 public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
 {
     if ($node instanceof \Twig_Node_Expression_Filter) {
         $name = $node->getNode('filter')->getAttribute('value');
         if ('trans' === $name) {
             $node->setAttribute('file', $this->file->getRelativePathname());
             $this->stack = $this->dataExtractor->collectDataFromTransFilterNode($node);
         }
     }
     if ($node instanceof \Twig_Node_Set) {
         $this->dataExtractor->addSetCollection($node);
     }
     if ($node instanceof \Twig_Node_For) {
         $this->dataExtractor->addForCollection($node);
     }
     return $node;
 }
コード例 #21
0
 public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
 {
     $this->stack[] = $node;
     if ($node instanceof \Twig_Node_Expression_Function) {
         $name = $node->getAttribute('name');
         if (in_array($name, $this->methodNames)) {
             $args = $node->getNode('arguments');
             switch ($name) {
                 case '_n':
                 case '_fn':
                     $id = $args->getNode(0)->getAttribute('value') . '|' . $args->getNode(1)->getAttribute('value');
                     break;
                 default:
                 case '__f':
                 case '__':
                     $id = $args->getNode(0)->getAttribute('value');
                     break;
             }
             // obtain translation domain from composer file
             $composerPath = str_replace($this->file->getRelativePathname(), '', $this->file->getPathname());
             if (isset(self::$domainCache[$composerPath])) {
                 $domain = self::$domainCache[$composerPath];
             } else {
                 $scanner = new Scanner();
                 $scanner->scan(array($composerPath), 1);
                 $metaData = $scanner->getModulesMetaData(true);
                 $domains = array_keys($metaData);
                 if (isset($domains[0])) {
                     $domain = strtolower($domains[0]);
                     // cache result of file lookup
                     self::$domainCache[$composerPath] = $domain;
                 } else {
                     $domain = 'messages';
                 }
             }
             $domainNode = array_search($name, $this->methodNames);
             $domain = $args->hasNode($domainNode) ? $args->getNode($domainNode)->getAttribute('value') : $domain;
             $message = new Message($id, $domain);
             $message->addSource(new FileSource((string) $this->file, $node->getLine()));
             $this->catalogue->add($message);
         }
     }
     return $node;
 }
コード例 #22
0
ファイル: Escaper.php プロジェクト: kuzmichus/schoolreg
 protected function escapeNode(Twig_NodeInterface $node, Twig_Environment $env, $type)
 {
     if (false === $type) {
         return $node;
     }
     $expression = $node instanceof Twig_Node_Print ? $node->getNode('expr') : $node;
     if ($expression instanceof Twig_Node_Expression_Filter) {
         // don't escape if the primary node of the filter is not a variable
         if (!$expression->getNode('node') instanceof Twig_Node_Expression_GetAttr && !$expression->getNode('node') instanceof Twig_Node_Expression_Name) {
             return $node;
         }
         // don't escape if there is already an "escaper" in the filter chain
         $filterMap = $env->getFilters();
         for ($i = 0; $i < count($expression->getNode('filters')); $i += 2) {
             $name = $expression->getNode('filters')->getNode($i)->getAttribute('value');
             if (isset($filterMap[$name]) && $filterMap[$name]->isEscaper()) {
                 return $node;
             }
         }
     } elseif (!$expression instanceof Twig_Node_Expression_GetAttr && !$expression instanceof Twig_Node_Expression_Name) {
         // don't escape if the node is not a variable
         return $node;
     }
     // escape
     if ($expression instanceof Twig_Node_Expression_Filter) {
         // escape all variables in filters arguments
         for ($i = 0; $i < count($expression->getNode('filters')); $i += 2) {
             foreach ($expression->getNode('filters')->getNode($i + 1) as $j => $n) {
                 $expression->getNode('filters')->getNode($i + 1)->setNode($j, $this->escapeNode($n, $env, $type));
             }
         }
         $filter = $this->getEscaperFilter($type, $expression->getLine());
         $expression->prependFilter($filter[0], $filter[1]);
         return $node;
     } elseif ($node instanceof Twig_Node_Print) {
         return new Twig_Node_Print(new Twig_Node_Expression_Filter($expression, new Twig_Node($this->getEscaperFilter($type, $node->getLine())), $node->getLine()), $node->getLine());
     } else {
         return new Twig_Node_Expression_Filter($node, new Twig_Node($this->getEscaperFilter($type, $node->getLine())), $node->getLine());
     }
 }
コード例 #23
0
 protected function escapeNode(Twig_NodeInterface $node, Twig_Environment $env, $type)
 {
     if (false === $type) {
         return $node;
     }
     $expression = $node instanceof Twig_Node_Print ? $node->getNode('expr') : $node;
     $safe = $this->safeAnalysis->getSafe($expression);
     if (null === $safe) {
         if (null === $this->traverser) {
             $this->traverser = new Twig_NodeTraverser($env, array($this->safeAnalysis));
         }
         $this->traverser->traverse($expression);
         $safe = $this->safeAnalysis->getSafe($expression);
     }
     if (false !== in_array($type, $safe) || false !== in_array('all', $safe)) {
         return $node;
     }
     if ($node instanceof Twig_Node_Print) {
         return new Twig_Node_Print($this->getEscaperFilter($type, $expression), $node->getLine());
     }
     return $this->getEscaperFilter($type, $node);
 }
コード例 #24
0
 /**
  * Checks whether a node is a default filter that needs to be wrapped with Twig_Node_Expression_DefaultFilter.
  *
  * The default filter needs to be wrapped with an instance of Twig_Node_Expression_DefaultFilter
  * when the filtered value is a name (like obj) or an attribute (like obj.attr).
  *
  * In such a case, it's compiled to {{ obj is defined ? obj|default('bar') : 'bar' }}
  *
  * @param Twig_NodeInterface $node A Twig_NodeInterface instance
  *
  * @return Boolean true if the node must be wrapped with a Twig_Node_Expression_DefaultFilter, false otherwise
  */
 public static function isDefaultFilter(Twig_NodeInterface $node)
 {
     return $node instanceof Twig_Node_Expression_Filter && 'default' === $node->getNode('filter')->getAttribute('value') && ($node->getNode('node') instanceof Twig_Node_Expression_Name || $node->getNode('node') instanceof Twig_Node_Expression_GetAttr);
 }
コード例 #25
0
ファイル: Extractor.php プロジェクト: KacerCZ/Twig-extensions
 /**
  * @param Twig_NodeInterface $node The node to be checked.
  * @return bool True if $node is a `format` filter.
  */
 protected function isFormatFilter(Twig_NodeInterface $node)
 {
     return $node instanceof Twig_Node_Expression_Filter && $node->getNode('filter')->getAttribute('value') == 'format';
 }
コード例 #26
0
 /**
  * Optimizes print nodes.
  *
  * It replaces:
  *
  *   * "echo $this->render(Parent)Block()" with "$this->display(Parent)Block()"
  *
  * @param Twig_NodeInterface $node A Node
  * @param Twig_Environment   $env  The current Twig environment
  */
 protected function optimizePrintNode(Twig_NodeInterface $node, Twig_Environment $env)
 {
     if (!$node instanceof Twig_Node_Print) {
         return $node;
     }
     if ($node->getNode('expr') instanceof Twig_Node_Expression_BlockReference || $node->getNode('expr') instanceof Twig_Node_Expression_Parent) {
         $node->getNode('expr')->setAttribute('output', true);
         return $node->getNode('expr');
     }
     return $node;
 }
コード例 #27
0
ファイル: ModuleCompiler.php プロジェクト: mi-la01/kt_twig-js
 protected function compileIsTraitable(JsCompiler $compiler, \Twig_NodeInterface $node)
 {
     // A template can be used as a trait if all of the following is true:
     //   * it has no parent
     //   * it has no macros
     //   * it has no body
     //
     // Put another way, a template can be used as a trait if it
     // only contains blocks and use statements.
     $traitable = null === $node->getNode('parent') && 0 === count($node->getNode('macros'));
     if ($traitable) {
         if (!count($nodes = $node->getNode('body'))) {
             $nodes = new Twig_Node(array($node->getNode('body')));
         }
         foreach ($nodes as $node) {
             if (!count($node)) {
                 continue;
             }
             if ($node instanceof Twig_Node_Text && ctype_space($node->getAttribute('data'))) {
                 continue;
             }
             if ($node instanceof Twig_Node_BlockReference) {
                 continue;
             }
             $traitable = false;
             break;
         }
     }
     $compiler->write("/**\n", " * Returns whether this template can be used as trait.\n", " *\n", " * @return {boolean}\n", " */\n")->write($this->functionName . ".prototype.isTraitable = function() {\n")->indent()->write(sprintf("return %s;\n", $traitable ? 'true' : 'false'))->outdent()->write("};\n");
 }
コード例 #28
0
 /**
  * @param \Twig_NodeInterface $node
  * @return bool
  */
 protected static function hasNoEscapeFilter($node)
 {
     return $node instanceof Filter && SafeTransExtension::DO_NOT_ESCAPE == $node->getNode('filter')->getAttribute('value');
 }