/**
  * {@inheritdoc}
  */
 public function enterNode(\Twig_NodeInterface $node, \Twig_Environment $env)
 {
     if ($node instanceof \Twig_Node_Block || $node instanceof \Twig_Node_Module) {
         $this->scope = $this->scope->enter();
     }
     if ($node instanceof TransDefaultDomainNode) {
         if ($node->getNode('expr') instanceof \Twig_Node_Expression_Constant) {
             $this->scope->set('domain', $node->getNode('expr'));
             return $node;
         } else {
             $var = $env->getParser()->getVarName();
             $name = new \Twig_Node_Expression_AssignName($var, $node->getLine());
             $this->scope->set('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 (!$this->scope->has('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->scope->get('domain'));
         }
     } elseif ($node instanceof TransNode) {
         if (null === $node->getNode('domain')) {
             $node->setNode('domain', $this->scope->get('domain'));
         }
     }
     return $node;
 }
Exemple #2
0
 protected function compileString(\Twig_NodeInterface $body, \Twig_Node_Expression_Array $vars)
 {
     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);
     if (version_compare(\Twig_Environment::VERSION, '1.5', '>=')) {
         foreach ($matches[1] as $var) {
             $key = new \Twig_Node_Expression_Constant('%' . $var . '%', $body->getLine());
             if (!$vars->hasElement($key)) {
                 $vars->addElement(new \Twig_Node_Expression_Name($var, $body->getLine()), $key);
             }
         }
     } else {
         $current = array();
         foreach ($vars as $name => $var) {
             $current[$name] = true;
         }
         foreach ($matches[1] as $var) {
             if (!isset($current['%' . $var . '%'])) {
                 $vars->setNode('%' . $var . '%', new \Twig_Node_Expression_Name($var, $body->getLine()));
             }
         }
     }
     return array(new \Twig_Node_Expression_Constant(str_replace('%%', '%', trim($msg)), $body->getLine()), $vars);
 }
Exemple #3
0
 /**
  * Renders the asset URL using Symfony's asset() function.
  */
 protected function getAssetUrlNode(\Twig_NodeInterface $body)
 {
     return new \Twig_Node_Expression_Function(
         new \Twig_Node_Expression_Name('asset', $body->getLine()),
         new \Twig_Node(array(
             new \Twig_Node_Expression_Constant($this->getAttribute('target_url'), $body->getLine()),
         )),
         $body->getLine()
     );
 }
Exemple #4
0
 public function __construct(Twig_NodeInterface $node, Twig_Node_Expression_Constant $filterName, Twig_NodeInterface $arguments, $lineno, $tag = null)
 {
     $default = new Twig_Node_Expression_Filter($node, new Twig_Node_Expression_Constant('_default', $node->getLine()), $arguments, $node->getLine());
     if ('default' === $filterName->getAttribute('value') && ($node instanceof Twig_Node_Expression_Name || $node instanceof Twig_Node_Expression_GetAttr)) {
         $test = new Twig_Node_Expression_Test_Defined(clone $node, 'defined', new Twig_Node(), $node->getLine());
         $false = count($arguments) ? $arguments->getNode(0) : new Twig_Node_Expression_Constant('', $node->getLine());
         $node = new Twig_Node_Expression_Conditional($test, $default, $false, $node->getLine());
     } else {
         $node = $default;
     }
     parent::__construct($node, $filterName, $arguments, $lineno, $tag);
 }
 /**
  * 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;
 }
 /**
  * Replaces "echo $this->renderBlock()" with "$this->displayBlock()".
  *
  * @param Twig_NodeInterface $node A Node
  * @param Twig_Environment   $env  The current Twig environment
  */
 protected function optimizeRenderBlock($node, $env)
 {
     if ($node instanceof Twig_Node_Print && $node->getNode('expr') instanceof Twig_Node_Expression_BlockReference) {
         return new Twig_Node_BlockReference($node->getNode('expr')->getNode('name')->getAttribute('value'), $node->getLine(), $node->getNodeTag());
     }
     return $node;
 }
Exemple #7
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';
         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;
 }
Exemple #8
0
 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()] = true;
         }
         // look for filters
         if ($node instanceof Twig_Node_Expression_Filter) {
             foreach ($node->getFilters() as $filter) {
                 $this->filters[$filter[0]] = true;
             }
         }
         // look for simple print statement ({{ article }})
         if ($node instanceof Twig_Node_Print && $node->getExpression() instanceof Twig_Node_Expression_Name) {
             return new Twig_Node_SandboxPrint($node->getExpression(), $node->getLine(), $node->getNodeTag());
         }
     }
     return $node;
 }
Exemple #9
0
 protected function applyFilters(Twig_NodeInterface $node)
 {
     if (false === ($filters = $this->getCurrentFilters())) {
         return $node;
     }
     if ($node instanceof Twig_Node_Text) {
         $expression = new Twig_Node_Expression_Constant($node->getData(), $node->getLine());
     } else {
         $expression = $node->getExpression();
     }
     // filters
     if ($expression instanceof Twig_Node_Expression_Filter) {
         $expression->appendFilters($filters);
         return $node;
     } else {
         return new Twig_Node_Print(new Twig_Node_Expression_Filter($expression, $filters, $node->getLine()), $node->getLine());
     }
 }
Exemple #10
0
 protected function compileString(\Twig_NodeInterface $body, \Twig_Node_Expression_Array $vars)
 {
     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)) {
             $vars->addElement(new \Twig_Node_Expression_Name($var, $body->getLine()), $key);
         }
     }
     return array(new \Twig_Node_Expression_Constant(str_replace('%%', '%', trim($msg)), $body->getLine()), $vars);
 }
Exemple #11
0
 protected function escapeNode(Twig_NodeInterface $node, Twig_Environment $env, $type)
 {
     if (false === $type) {
         return $node;
     }
     $expression = $node instanceof Twig_Node_Print ? $node->getExpression() : $node;
     if ($expression instanceof Twig_Node_Expression_Filter) {
         // don't escape if the primary node of the filter is not a variable
         $nodes = $expression->getNodes();
         if (!$nodes[0] instanceof Twig_Node_Expression_Name) {
             return $node;
         }
         // don't escape if there is already an "escaper" in the filter chain
         $filterMap = $env->getFilters();
         foreach ($expression->getFilters() as $filter) {
             if (isset($filterMap[$filter[0]]) && $filterMap[$filter[0]]->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
         $filters = $expression->getFilters();
         foreach ($filters as $i => $filter) {
             foreach ($filter[1] as $j => $argument) {
                 $filters[$i][1][$j] = $this->escapeNode($argument, $env, $type);
             }
         }
         $expression->setFilters($filters);
         $expression->prependFilter($this->getEscaperFilter($type));
         return $node;
     } elseif ($node instanceof Twig_Node_Print) {
         return new Twig_Node_Print(new Twig_Node_Expression_Filter($expression, array($this->getEscaperFilter($type)), $node->getLine()), $node->getLine());
     } else {
         return new Twig_Node_Expression_Filter($node, array($this->getEscaperFilter($type)), $node->getLine());
     }
 }
 protected function compileString(\Twig_NodeInterface $body, \Twig_Node_Expression_Array $vars)
 {
     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);
     }
     $current = array();
     foreach ($vars as $name => $var) {
         $current[$name] = true;
     }
     preg_match_all('/\\%([^\\%]+)\\%/', $msg, $matches);
     foreach ($matches[1] as $var) {
         if (!isset($current['%' . $var . '%'])) {
             $vars->setNode('%' . $var . '%', new \Twig_Node_Expression_Name($var, $body->getLine()));
         }
     }
     return array(new \Twig_Node_Expression_Constant(trim($msg), $body->getLine()), $vars);
 }
Exemple #13
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);
 }
 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;
 }
Exemple #15
0
 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);
     }
 }
Exemple #16
0
 protected function escapeNode(Twig_NodeInterface $node, Twig_Environment $env, $type)
 {
     if (false === $type) {
         return $node;
     }
     $expression = $node instanceof Twig_Node_Print ? $node->expr : $node;
     if (!$expression instanceof Zwig_Node_Expression_ViewHelper) {
         return $node;
     }
     if ($this->isViewHelperEscaper($env, $expression->getHelper())) {
         return $node;
     }
     $escaped = $node instanceof Twig_Node_Print ? $expression : $node;
     $escaped = $this->getFilterNode($escaped, $this->getCastFilter($escaped->getLine()));
     $escaped = $this->getFilterNode($escaped, $this->getEscaperFilter($type, $escaped->getLine()));
     if ($node instanceof Twig_Node_Print) {
         return new Twig_Node_Print($escaped, $node->getLine());
     } else {
         return $escaped;
     }
 }
Exemple #17
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)
 {
     // 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;
 }
Exemple #18
0
 protected function filterBodyNodes(Twig_NodeInterface $node)
 {
     // check that the body does not contain non-empty output nodes
     if ($node instanceof Twig_Node_Text && !ctype_space($node->getAttribute('data')) || !$node instanceof Twig_Node_Text && !$node instanceof Twig_Node_BlockReference && $node instanceof Twig_NodeOutputInterface) {
         if (false !== strpos((string) $node, chr(0xef) . chr(0xbb) . chr(0xbf))) {
             throw new Twig_Error_Syntax('A template that extends another one cannot have a body but a byte order mark (BOM) has been detected; it must be removed.', $node->getLine(), $this->stream->getFilename());
         } else {
             throw new Twig_Error_Syntax('A template that extends another one cannot have a body.', $node->getLine(), $this->stream->getFilename());
         }
     }
     // bypass "set" nodes as they "capture" the output
     if ($node instanceof Twig_Node_Set) {
         return $node;
     }
     if ($node instanceof Twig_NodeOutputInterface) {
         return;
     }
     foreach ($node as $k => $n) {
         if (null !== $n && null === ($n = $this->filterBodyNodes($n))) {
             $node->removeNode($k);
         }
     }
     return $node;
 }
Exemple #19
0
 protected function escapeNode(Twig_NodeInterface $node, Twig_Environment $env, $type)
 {
     if (false === $type) {
         return $node;
     }
     $expression = $node instanceof Twig_Node_Print ? $node->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->node instanceof Twig_Node_Expression_GetAttr && !$expression->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->filters); $i += 2) {
             $name = $expression->filters->{$i}['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->filters); $i += 2) {
             foreach ($expression->filters->{$i + 1} as $j => $n) {
                 $expression->filters->{$i + 1}->{$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());
     }
 }
Exemple #20
0
 protected function getEscaperFilter($type, Twig_NodeInterface $node)
 {
     $line = $node->getLine();
     $name = new Twig_Node_Expression_Constant('escape', $line);
     $args = new Twig_Node(array(new Twig_Node_Expression_Constant((string) $type, $line), new Twig_Node_Expression_Constant(null, $line), new Twig_Node_Expression_Constant(true, $line)));
     return new Twig_Node_Expression_Filter($node, $name, $args, $line);
 }
Exemple #21
0
 /**
  * Adds debugging information.
  *
  * @param Twig_NodeInterface $node The related twig node
  *
  * @return Twig_Compiler The current compiler instance
  */
 public function addDebugInfo(Twig_NodeInterface $node)
 {
     if ($node->getLine() != $this->lastLine) {
         // when mbstring.func_overload is set to 2
         // mb_substr_count() replaces substr_count()
         // but they have different signatures!
         if ((int) ini_get('mbstring.func_overload') & 2) {
             // this is much slower than the "right" version
             $this->sourceLine += mb_substr_count(mb_substr($this->source, $this->sourceOffset), "\n");
         } else {
             $this->sourceLine += substr_count($this->source, "\n", $this->sourceOffset);
         }
         $this->sourceOffset = strlen($this->source);
         $this->debugInfo[$this->sourceLine] = $node->getLine();
         $this->lastLine = $node->getLine();
         $this->write("// line {$node->getLine()}\n");
     }
     return $this;
 }
Exemple #22
0
 public function parseTestExpression(Twig_Parser $parser, Twig_NodeInterface $node)
 {
     $stream = $parser->getStream();
     list($name, $test) = $this->getTest($parser, $node->getLine());
     if ($test instanceof Twig_SimpleTest && $test->isDeprecated()) {
         $message = sprintf('Twig Test "%s" is deprecated', $name);
         if (!is_bool($test->getDeprecatedVersion())) {
             $message .= sprintf(' since version %s', $test->getDeprecatedVersion());
         }
         if ($test->getAlternative()) {
             $message .= sprintf('. Use "%s" instead', $test->getAlternative());
         }
         $message .= sprintf(' in %s at line %d.', $stream->getFilename(), $stream->getCurrent()->getLine());
         @trigger_error($message, E_USER_DEPRECATED);
     }
     $class = $this->getTestNodeClass($parser, $test);
     $arguments = null;
     if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
         $arguments = $parser->getExpressionParser()->parseArguments(true);
     }
     return new $class($node, $name, $arguments, $parser->getCurrentToken()->getLine());
 }
Exemple #23
0
 public function __construct(Twig_NodeInterface $left, Twig_NodeInterface $right, $lineno)
 {
     $test = new Twig_Node_Expression_Binary_And(new Twig_Node_Expression_Test_Defined(clone $left, 'defined', new Twig_Node(), $left->getLine()), new Twig_Node_Expression_Unary_Not(new Twig_Node_Expression_Test_Null($left, 'null', new Twig_Node(), $left->getLine()), $left->getLine()), $left->getLine());
     parent::__construct($test, $left, $right, $lineno);
 }
Exemple #24
0
 /**
  * Adds debugging information.
  *
  * @param Twig_NodeInterface $node The related twig node
  *
  * @return Twig_Compiler The current compiler instance
  */
 public function addDebugInfo(Twig_NodeInterface $node)
 {
     if ($node->getLine() != $this->lastLine) {
         $this->debugInfo[substr_count($this->source, "\n")] = $node->getLine();
         $this->lastLine = $node->getLine();
         $this->write("// line {$node->getLine()}\n");
     }
     return $this;
 }
Exemple #25
0
 /**
  * Renders the asset URL using Symfony's path() function.
  */
 protected function getAssetUrlNode(\Twig_NodeInterface $body)
 {
     return new \Twig_Node_Expression_Function(new \Twig_Node_Expression_Name('path', $body->getLine()), new \Twig_Node(array(new \Twig_Node_Expression_Constant('assetic_' . $this->getAttribute('asset_name'), $body->getLine()))), $body->getLine());
 }
Exemple #26
0
    protected function compileString(Twig_NodeInterface $body)
    {
        if ($body instanceof Twig_Node_Expression_Name || $body instanceof Twig_Node_Expression_Constant) {
            return array($body, array());
        }

        $msg = '';
        $vars = array();
        foreach ($body as $node) {
            if ($node instanceof Twig_Node_Print) {
                $n = $node->getNode('expr');
                while ($n instanceof Twig_Node_Expression_Filter) {
                    $n = $n->getNode('node');
                }
                $msg .= sprintf('%%%s%%', $n->getAttribute('name'));
                $vars[] = new Twig_Node_Expression_Name($n->getAttribute('name'), $n->getLine());
            } else {
                $msg .= $node->getAttribute('data');
            }
        }

        return array(new Twig_Node(array(new Twig_Node_Expression_Constant(trim($msg), $body->getLine()))), $vars);
    }
 /**
  * Renders the asset URL using Symfony's asset() function.
  */
 protected function getAssetUrlNode(\Twig_NodeInterface $body)
 {
     return new \Twig_Node_Expression_Function(new \Twig_Node_Expression_Name('asset', $body->getLine()), new \Twig_Node(array(new \Twig_Node_Expression_Constant($this->getAttribute('output'), $body->getLine()), new \Twig_Node_Expression_Constant($this->hasAttribute('package') ? $this->getAttribute('package') : null, $body->getLine()))), $body->getLine());
 }
Exemple #28
0
 public function parseTestExpression(Twig_Parser $parser, Twig_NodeInterface $node)
 {
     $stream = $parser->getStream();
     $name = $this->getTestName($parser, $node->getLine());
     $class = $this->getTestNodeClass($parser, $name);
     $arguments = null;
     if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
         $arguments = $parser->getExpressionParser()->parseArguments(true);
     }
     return new $class($node, $name, $arguments, $parser->getCurrentToken()->getLine());
 }
 protected function getAssetUrlNode(\Twig_NodeInterface $body)
 {
     return new \Twig_Node_Expression_Constant($this->getAttribute('output'), $body->getLine());
 }
Exemple #30
0
 /**
  * Adds debugging information.
  *
  * @param Twig_NodeInterface $node The related twig node
  *
  * @return Twig_Compiler The current compiler instance
  */
 public function addDebugInfo(Twig_NodeInterface $node)
 {
     if ($node->getLine() != $this->lastLine) {
         $this->lastLine = $node->getLine();
         $this->write("// line {$node->getLine()}\n");
     }
     return $this;
 }