Beispiel #1
0
 /**
  * {@inheritdoc}
  */
 public function check(\Twig_TokenStream $tokens)
 {
     $this->reset();
     $macros = [];
     while (!$tokens->isEOF()) {
         $token = $tokens->getCurrent();
         if ($token->getType() === \Twig_Token::NAME_TYPE && $token->getValue() === 'import') {
             while ($tokens->getCurrent()->getValue() !== 'as') {
                 $tokens->next();
             }
             $tokens->next();
             while (in_array($tokens->getCurrent()->getType(), [\Twig_Token::NAME_TYPE, \Twig_Token::PUNCTUATION_TYPE, Token::WHITESPACE_TYPE])) {
                 $next = $tokens->getCurrent();
                 if ($next->getType() === \Twig_Token::NAME_TYPE) {
                     $macros[$next->getValue()] = $next;
                 }
                 $tokens->next();
             }
         } elseif ($token->getType() === \Twig_Token::NAME_TYPE && array_key_exists($token->getValue(), $macros)) {
             unset($macros[$token->getValue()]);
         }
         $tokens->next();
     }
     foreach ($macros as $name => $originalToken) {
         $this->addViolation($tokens->getFilename(), $originalToken->getLine(), $originalToken->getColumn(), sprintf('Unused macro "%s".', $name));
     }
     return $this->violations;
 }
 /**
  * @throws \Twig_Error_Syntax
  * @param  $filename
  * @param  \Twig_TokenStream $stream
  */
 private function getAssetFromStream($filename, \Twig_TokenStream $stream)
 {
     $this->expect($filename, $stream->next(), \Twig_Token::PUNCTUATION_TYPE, '(');
     $token = $stream->next();
     $this->expect($filename, $token, \Twig_Token::STRING_TYPE);
     $this->expect($filename, $stream->next(), \Twig_Token::PUNCTUATION_TYPE, ')');
     return $token->getValue();
 }
 /**
  * @expectedException Twig_Error_Syntax
  * @expectedMessage   Unexpected end of template
  */
 public function testEndOfTemplateLook()
 {
     $stream = new Twig_TokenStream(array(new Twig_Token(Twig_Token::BLOCK_START_TYPE, 1, 1)));
     while (!$stream->isEOF()) {
         $stream->look();
         $stream->next();
     }
 }
Beispiel #4
0
 /**
  * Método que revisa cada l�nea de la plantilla
  * @param \Twig_TokenStream $stream
  * @return \Twig_TokenStream
  */
 protected function checkTemplateLine(\Twig_TokenStream $stream)
 {
     $value = $stream->getCurrent();
     switch ($value->getType()) {
         case \Twig_Token::STRING_TYPE:
             $this->values[] = $this->parser->getExpressionParser()->parseExpression();
             break;
         case \Twig_Token::BLOCK_END_TYPE:
             $this->end = true;
             $stream->next();
             break;
         default:
             $stream->next();
             break;
     }
     return $stream;
 }
 public function testNext()
 {
     $stream = new Twig_TokenStream(self::$tokens);
     $repr = array();
     while (!$stream->isEOF()) {
         $token = $stream->next();
         $repr[] = $token->getValue();
     }
     $this->assertEquals('1, 2, 3, 4, 5, 6, 7', implode(', ', $repr), '->next() advances the pointer and returns the current token');
 }
Beispiel #6
0
 /**
  * {@inheritdoc}
  */
 public function check(\Twig_TokenStream $tokens)
 {
     $this->reset();
     while (!$tokens->isEOF()) {
         $token = $tokens->getCurrent();
         if ($token->getType() === \Twig_Token::NAME_TYPE && preg_match('/[A-Z]/', $token->getValue())) {
             if ($tokens->look(Lexer::PREVIOUS_TOKEN)->getType() === Token::WHITESPACE_TYPE && $tokens->look(-2)->getValue() === 'set') {
                 $this->addViolation($tokens->getFilename(), $token->getLine(), $token->getColumn(), sprintf('The "%s" variable should be in lower case (use _ as a separator).', $token->getValue()));
             }
         }
         $tokens->next();
     }
     return $this->violations;
 }
Beispiel #7
0
 /**
  * {@inheritdoc}
  */
 public function check(\Twig_TokenStream $tokens)
 {
     $this->reset();
     while (!$tokens->isEOF()) {
         $token = $tokens->getCurrent();
         if ($token->getType() === Token::NEWLINE_TYPE && $tokens->look(-1)->getType() === Token::WHITESPACE_TYPE || $token->getType() === Token::TEXT_TYPE) {
             if (preg_match("/[[:blank:]]+\n/", $token->getValue())) {
                 $this->addViolation($tokens->getFilename(), $token->getLine(), $token->getColumn(), 'A line should not end with blank space(s).');
             }
         }
         $tokens->next();
     }
     return $this->violations;
 }
 public function testRewind()
 {
     $stream = new Twig_TokenStream(self::$tokens, '', false);
     $this->assertEquals(2, $stream->look()->getValue(), '->look() returns the next token');
     $this->assertEquals(3, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
     $this->assertEquals(4, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
     $this->assertEquals(5, $stream->look()->getValue(), '->look() can be called several times to look more than one upcoming token');
     $stream->rewind();
     $repr = array();
     while (!$stream->isEOF()) {
         $token = $stream->next(false);
         $repr[] = $token->getValue();
     }
     $this->assertEquals('1, 2, 3, 4, 5, 6, 7', implode(', ', $repr), '->rewind() pushes all pushed tokens to the token array');
 }
Beispiel #9
0
 /**
  * {@inheritdoc}
  */
 public function check(\Twig_TokenStream $tokens)
 {
     $this->reset();
     $variables = [];
     while (!$tokens->isEOF()) {
         $token = $tokens->getCurrent();
         if ($token->getType() === \Twig_Token::NAME_TYPE) {
             if ($tokens->look(Lexer::PREVIOUS_TOKEN)->getType() === Token::WHITESPACE_TYPE && $tokens->look(-2)->getValue() === 'set') {
                 $variables[$token->getValue()] = $token;
             } else {
                 unset($variables[$token->getValue()]);
             }
         }
         $tokens->next();
     }
     foreach ($variables as $name => $originalToken) {
         $this->addViolation($tokens->getFilename(), $originalToken->getLine(), $originalToken->getColumn(), sprintf('Unused variable "%s".', $name));
     }
     return $this->violations;
 }
 /**
  * @param \Twig_TokenStream $stream
  * @param int               $lineno
  * @return WebpackInlineNode
  * @throws \Twig_Error_Syntax
  */
 private function parseInline(\Twig_TokenStream $stream, $lineno)
 {
     if ($stream->test(\Twig_Token::NAME_TYPE)) {
         $stream->next();
     }
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     $this->parser->subparse(function ($token) {
         return $token->test(['end' . $this->getTag()]);
     }, true);
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     $file = $this->parser->getEnvironment()->getLoader()->getCacheKey($stream->getFilename());
     if (!isset($this->inline_blocks[$file])) {
         $this->inline_blocks[$file] = 0;
     }
     $file_name = md5($file . $this->inline_blocks[$file]) . '.js';
     $assets = $this->extension->webpackAsset('cache.' . $file_name);
     $this->inline_blocks[$file]++;
     return new WebpackInlineNode(['js_file' => $assets['js'], 'css_file' => $assets['css']], $lineno, $this->getTag());
 }
 /**
  * Get value from stream
  *
  * @param \Twig_TokenStream $stream
  * @param bool $isBool
  *
  * @return bool|string
  */
 protected function parseValue(\Twig_TokenStream $stream, $isBool = true)
 {
     $stream->next();
     $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
     if ($isBool) {
         return 'true' == $stream->expect(\Twig_Token::NAME_TYPE, ['true', 'false'])->getValue();
     }
     return $stream->expect(\Twig_Token::STRING_TYPE)->getValue();
 }
Beispiel #12
0
 /**
  * Get string value from stream
  *
  * @param \Twig_TokenStream $stream
  * @return string
  */
 protected function parseStringValue(\Twig_TokenStream $stream)
 {
     $stream->next();
     $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
     return $stream->expect(\Twig_Token::STRING_TYPE)->getValue();
 }
Beispiel #13
0
$repr = array();
while (!$stream->isEOF()) {
    $token = $stream->next();
    $repr[] = $token->getValue();
}
$t->is(implode(', ', $repr), '1, 2, 3, 4, 5, 6, 7', '->look() pushes the token to the stack');
$stream = new Twig_TokenStream($tokens, '', false);
$t->is($stream->look()->getValue(), 2, '->look() returns the next token');
$t->is($stream->look()->getValue(), 3, '->look() can be called several times to look more than one upcoming token');
$t->is($stream->look()->getValue(), 4, '->look() can be called several times to look more than one upcoming token');
$t->is($stream->look()->getValue(), 5, '->look() can be called several times to look more than one upcoming token');
$repr = array();
while (!$stream->isEOF()) {
    $token = $stream->next();
    $repr[] = $token->getValue();
}
$t->is(implode(', ', $repr), '1, 2, 3, 4, 5, 6, 7', '->look() pushes the token to the stack');
// ->rewind()
$t->diag('->rewind()');
$stream = new Twig_TokenStream($tokens, '', false);
$t->is($stream->look()->getValue(), 2, '->look() returns the next token');
$t->is($stream->look()->getValue(), 3, '->look() can be called several times to look more than one upcoming token');
$t->is($stream->look()->getValue(), 4, '->look() can be called several times to look more than one upcoming token');
$t->is($stream->look()->getValue(), 5, '->look() can be called several times to look more than one upcoming token');
$stream->rewind();
$repr = array();
while (!$stream->isEOF()) {
    $token = $stream->next(false);
    $repr[] = $token->getValue();
}
$t->is(implode(', ', $repr), '1, 2, 3, 4, 5, 6, 7', '->rewind() pushes all pushed tokens to the token array');
 /**
  * Helper method for the common operation of grabbing the next boolean value
  * from the stream
  *
  * @param \Twig_TokenStream $stream
  * @param string $optionName
  * @return string
  */
 protected function getNextExpectedBoolValueFromStream(\Twig_TokenStream $stream, $optionName)
 {
     $stream->next();
     $stream->expect(\Twig_Token::PUNCTUATION_TYPE);
     $expr = $this->parser->getExpressionParser()->parseExpression();
     if (!$expr instanceof \Twig_Node_Expression_Constant || !is_bool($expr->getAttribute('value'))) {
         throw new SyntaxException(sprintf('The %s option must be boolean true or false (i.e. %s:false)', $optionName, $optionName), $stream->getCurrent()->getLine(), $stream->getFilename());
     }
     return $expr->getAttribute('value');
 }
Beispiel #15
0
 protected function getTwigArgument(\Twig_TokenStream $stream)
 {
     $buffer = "";
     // 1. Move to open bracket
     while (!$stream->isEOF()) {
         if ($stream->next()->getValue() === '(') {
             break;
         }
     }
     // 2. Fetch until close bracket
     while (!$stream->isEOF()) {
         $token = $stream->next()->getValue();
         if ($token === ')' || $token === ',') {
             break;
         }
         $buffer .= $token;
     }
     return $buffer;
 }