Example #1
0
 /**
  * {@inheritDoc}
  */
 public function parse(\Twig_Token $token)
 {
     $filters = array();
     $attributes = array('output' => $this->output, 'var_name' => 'asset_url', 'vars' => array());
     $stream = $this->parser->getStream();
     while (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
         if ($stream->test(\Twig_Token::NAME_TYPE, 'filter')) {
             $filters = array_merge($filters, array_filter(array_map('trim', explode(',', $this->parseStringValue($stream)))));
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'output')) {
             $attributes['output'] = $this->parseStringValue($stream);
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'debug')) {
             $attributes['debug'] = $this->parseBooleanValue($stream);
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'combine')) {
             $attributes['combine'] = $this->parseBooleanValue($stream);
         } else {
             $token = $stream->getCurrent();
             throw new \Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', \Twig_Token::typeToEnglish($token->getType(), $token->getLine()), $token->getValue()), $token->getLine());
         }
     }
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     $body = $this->parser->subparse(array($this, 'testEndTag'), true);
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     $lineno = $token->getLine();
     return new \Twig_Node(array($this->createAsseticNode($body, $filters, $attributes, $lineno), $this->createDebugAsseticNode($body, $filters, $attributes, $lineno)), array(), $lineno);
 }
 public function parse(\Twig_Token $token)
 {
     $inputs = array();
     $attrs = array('filters' => array());
     $stream = $this->parser->getStream();
     while (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
         if ($stream->test(\Twig_Token::STRING_TYPE)) {
             $inputs[] = $stream->next()->getValue();
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'filter')) {
             $stream->next();
             $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
             $attrs['filters'] = array_merge($attrs['filters'], array_filter(array_map('trim', explode(',', $stream->expect(\Twig_Token::STRING_TYPE)->getValue()))));
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'output')) {
             $stream->next();
             $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
             $attributes['output'] = $stream->expect(\Twig_Token::STRING_TYPE)->getValue();
         } else {
             $token = $stream->getCurrent();
             throw new \Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', \Twig_Token::typeToEnglish($token->getType(), $token->getLine()), $token->getValue()), $token->getLine());
         }
     }
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     $content = $this->parser->subparse(array($this, 'testEndTag'), true);
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     $this->manager->load($inputs, $attrs, $this->getTag());
     $params['assets'] = $this->manager->getAssetsPath();
     return new TlAssetsNode(array('content' => $content), $params, $token->getLine(), $this->getTag());
 }
 /**
  * {@inheritDoc}
  */
 public function parse(\Twig_Token $token)
 {
     $inputs = $this->assets;
     $filters = [];
     $attributes = ['output' => $this->output, 'var_name' => 'asset_url', 'vars' => []];
     $stream = $this->parser->getStream();
     while (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
         if ($stream->test(\Twig_Token::NAME_TYPE, 'filter')) {
             $filters = array_merge($filters, array_filter(array_map('trim', explode(',', $this->parseValue($stream, false)))));
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'output')) {
             $attributes['output'] = $this->parseValue($stream, false);
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'debug')) {
             $attributes['debug'] = $this->parseValue($stream);
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'combine')) {
             $attributes['combine'] = $this->parseValue($stream);
         } else {
             $token = $stream->getCurrent();
             throw new \Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', \Twig_Token::typeToEnglish($token->getType(), $token->getLine()), $token->getValue()), $token->getLine());
         }
     }
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     $body = $this->parser->subparse([$this, 'testEndTag'], true);
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     $nameUnCompress = $this->factory->generateAssetName($inputs['compress'][0], $filters, $attributes);
     $nameCompress = substr(sha1(serialize($inputs['uncompress'][0]) . 'oro_assets'), 0, 7);
     return new OroAsseticNode(['compress' => $this->factory->createAsset($inputs['compress'][0], $filters, $attributes + ['name' => $nameCompress, 'debug' => false]), 'un_compress' => $this->factory->createAsset($inputs['uncompress'][0], [], $attributes + ['name' => $nameUnCompress, 'debug' => true])], ['un_compress' => $nameUnCompress, 'compress' => $nameCompress], $filters, $inputs, $body, $attributes, $token->getLine(), $this->getTag());
 }
 public function parse(\Twig_Token $token)
 {
     $name = null;
     $stream = $this->parser->getStream();
     while (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
         if ($stream->test(\Twig_Token::STRING_TYPE)) {
             $name = $stream->next()->getValue();
             if (!$this->manager->has($name)) {
                 throw new FeatureToggleNotFoundException('The feature "%s" does not exist.', $name);
             } else {
                 $feature = $this->manager->get($name);
             }
         } else {
             $token = $stream->getCurrent();
             throw new \Twig_Error_Syntax(sprintf('Unexpected token "%s" of value %s".', \Twig_Token::typeToEnglish($token->getType(), $token->getLine()), $token->getValue()), $token->getLine());
         }
     }
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     // Store the body of the feature.
     $body = $this->parser->subparse(array($this, 'decideFeatureEnd'), true);
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     if ($feature->isEnabled()) {
         return $body;
     }
     return;
 }
Example #5
0
 /**
  * {@inheritdoc}
  */
 public function parse(Twig_Token $token)
 {
     $dirs = [];
     $name = null;
     $output = null;
     $stream = $this->parser->getStream();
     while (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
         if ($stream->test(\Twig_Token::STRING_TYPE)) {
             // 'web/css', 'web/js'
             $dirs[] = $stream->next()->getValue();
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'pattern')) {
             // patern='\.js$'
             $stream->next();
             $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
             $pattern = $stream->next()->getValue();
         } else {
             $token = $stream->getCurrent();
             throw new \Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', \Twig_Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $stream->getFilename());
         }
     }
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     $body = $this->parser->subparse([$this, 'testEndTag'], true);
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     $matches = $this->matchPatterns($this->baseDir, $dirs, $pattern);
     return new GlobNode($matches, ['body' => $body], [], $token->getLine(), $this->getTag());
 }
Example #6
0
 public static function typeToEnglish($type, $line = -1)
 {
     switch ($type) {
         case self::COMMENT:
             return 'comment';
         default:
             return parent::typeToEnglish($type, $line);
     }
 }
 /**
  * Tests a token and returns it or throws a syntax error.
  *
  * @return Twig_Token
  */
 public function expect($type, $value = null, $message = null)
 {
     $token = $this->tokens[$this->current];
     if (!$token->test($type, $value)) {
         throw new Twig_Error_Syntax(sprintf('%sUnexpected token "%s" of value "%s" ("%s" expected%s)', $message ? $message . '. ' : '', Twig_Token::typeToEnglish($token->getType()), $token->getValue(), Twig_Token::typeToEnglish($type), $value ? sprintf(' with value "%s"', $value) : ''), $token->getLine());
     }
     $this->next();
     return $token;
 }
 public function parse(\Twig_Token $token)
 {
     $files = array();
     $stream = $this->parser->getStream();
     // parse string values until the tag end
     while (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
         if ($stream->test(\Twig_Token::STRING_TYPE)) {
             // 'bundle/file', 'bundle/file2'
             $files[] = $stream->next()->getValue();
         } else {
             $token = $stream->getCurrent();
             throw new \Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', \Twig_Token::typeToEnglish($token->getType(), $token->getLine()), $token->getValue()), $token->getLine());
         }
     }
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     return new RequireJsNode($this->manifest, $this->config, $files, $token->getLine(), $this->getTag());
 }
Example #9
0
 public function parse(\Twig_Token $token)
 {
     $node = new TwigJsNode(array(), array(), $token->getLine(), $this->getTag());
     $stream = $this->parser->getStream();
     while (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
         if ($stream->test(\Twig_Token::NAME_TYPE, 'name')) {
             $stream->next();
             $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
             $node->setAttribute('name', $stream->expect(\Twig_Token::STRING_TYPE)->getValue());
             continue;
         }
         $token = $stream->getCurrent();
         throw new \Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', \Twig_Token::typeToEnglish($token->getType(), $token->getLine()), $token->getValue()), $token->getLine());
     }
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     return $node;
 }
 public function parse(\Twig_Token $token)
 {
     $files = array();
     $stream = $this->parser->getStream();
     // parse string values until the tag end
     while (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
         if ($stream->test(\Twig_Token::STRING_TYPE)) {
             // 'css/myfile.css', 'css/llamas.css'
             $files[] = $stream->next()->getValue();
         } else {
             $token = $stream->getCurrent();
             throw new \Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', \Twig_Token::typeToEnglish($token->getType(), $token->getLine()), $token->getValue()), $token->getLine());
         }
     }
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     $body = $this->parser->subparse(array($this, 'testEndTag'), true);
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     return $this->createNode($body, $this->getTag(), $token->getLine(), $files);
 }
 public function parse(Token $token)
 {
     $inputs = array();
     $stream = $this->parser->getStream();
     while (!$stream->test(Token::BLOCK_END_TYPE)) {
         if ($stream->test(Token::STRING_TYPE)) {
             $inputs[] = $stream->next()->getValue();
         } else {
             $token = $stream->getCurrent();
             throw new \Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $stream->getFilename());
         }
     }
     $stream->expect(Token::BLOCK_END_TYPE);
     $endTag = 'end_' . $this->getTag();
     $body = $this->parser->subparse(function (Token $token) use($endTag) {
         return $token->test(array($endTag));
     }, true);
     $stream->expect(Token::BLOCK_END_TYPE);
     $nodes = array();
     foreach ($inputs as $input) {
         $nodes[] = $this->createNodeForInput($input, $body, $token->getLine());
     }
     return new \Twig_Node($nodes);
 }
 public function parse(\Twig_Token $token)
 {
     $inputs = array();
     $filters = array();
     $name = null;
     $attributes = array('output' => $this->output, 'var_name' => 'asset_url', 'vars' => array());
     $stream = $this->parser->getStream();
     while (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
         if ($stream->test(\Twig_Token::STRING_TYPE)) {
             // '@jquery', 'js/src/core/*', 'js/src/extra.js'
             $inputs[] = $stream->next()->getValue();
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'filter')) {
             // filter='yui_js'
             $stream->next();
             $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
             $filters = array_merge($filters, array_filter(array_map('trim', explode(',', $stream->expect(\Twig_Token::STRING_TYPE)->getValue()))));
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'output')) {
             // output='js/packed/*.js' OR output='js/core.js'
             $stream->next();
             $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
             $attributes['output'] = $stream->expect(\Twig_Token::STRING_TYPE)->getValue();
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'name')) {
             // name='core_js'
             $stream->next();
             $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
             $name = $stream->expect(\Twig_Token::STRING_TYPE)->getValue();
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'as')) {
             // as='the_url'
             $stream->next();
             $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
             $attributes['var_name'] = $stream->expect(\Twig_Token::STRING_TYPE)->getValue();
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'debug')) {
             // debug=true
             $stream->next();
             $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
             $attributes['debug'] = 'true' == $stream->expect(\Twig_Token::NAME_TYPE, array('true', 'false'))->getValue();
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'combine')) {
             // combine=true
             $stream->next();
             $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
             $attributes['combine'] = 'true' == $stream->expect(\Twig_Token::NAME_TYPE, array('true', 'false'))->getValue();
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'vars')) {
             // vars=['locale','browser']
             $stream->next();
             $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
             $stream->expect(\Twig_Token::PUNCTUATION_TYPE, '[');
             while ($stream->test(\Twig_Token::STRING_TYPE)) {
                 $attributes['vars'][] = $stream->expect(\Twig_Token::STRING_TYPE)->getValue();
                 if (!$stream->test(\Twig_Token::PUNCTUATION_TYPE, ',')) {
                     break;
                 }
                 $stream->next();
             }
             $stream->expect(\Twig_Token::PUNCTUATION_TYPE, ']');
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, $this->extensions)) {
             // an arbitrary configured attribute
             $key = $stream->next()->getValue();
             $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
             $attributes[$key] = $stream->expect(\Twig_Token::STRING_TYPE)->getValue();
         } else {
             $token = $stream->getCurrent();
             throw new \Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', \Twig_Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $stream->getFilename());
         }
     }
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     $body = $this->parser->subparse(array($this, 'testEndTag'), true);
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     if ($this->single && 1 < count($inputs)) {
         $inputs = array_slice($inputs, -1);
     }
     if (!$name) {
         $name = $this->factory->generateAssetName($inputs, $filters, $attributes);
     }
     $asset = $this->factory->createAsset($inputs, $filters, $attributes + array('name' => $name));
     return $this->createBodyNode($asset, $body, $inputs, $filters, $name, $attributes, $token->getLine(), $this->getTag());
 }
Example #13
0
 private function expect($filename, \Twig_Token $token, $type, $value = null)
 {
     if ($token->getType() !== $type) {
         throw new \RuntimeException(sprintf('Parse error in %s at line %d. Expected %s%s, got %s.', $filename, $token->getLine(), \Twig_Token::typeToEnglish($type), $value !== null ? ' "' . $value . '"' : '', \Twig_Token::typeToEnglish($token->getType())));
     }
 }
Example #14
0
 public function parseHashExpression()
 {
     $stream = $this->parser->getStream();
     $stream->expect(Twig_Token::PUNCTUATION_TYPE, '{', 'A hash element was expected');
     $elements = array();
     while (!$stream->test(Twig_Token::PUNCTUATION_TYPE, '}')) {
         if (!empty($elements)) {
             $stream->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'A hash value must be followed by a comma');
             // trailing ,?
             if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '}')) {
                 break;
             }
         }
         if (!$stream->test(Twig_Token::STRING_TYPE) && !$stream->test(Twig_Token::NUMBER_TYPE)) {
             $current = $stream->getCurrent();
             throw new Twig_Error_Syntax(sprintf('A hash key must be a quoted string or a number (unexpected token "%s" of value "%s"', Twig_Token::typeToEnglish($current->getType(), $current->getLine()), $current->getValue()), $current->getLine());
         }
         $key = $stream->next()->getValue();
         $stream->expect(Twig_Token::PUNCTUATION_TYPE, ':', 'A hash key must be followed by a colon (:)');
         $elements[$key] = $this->parseExpression();
     }
     $stream->expect(Twig_Token::PUNCTUATION_TYPE, '}', 'An opened hash is not properly closed');
     return new Twig_Node_Expression_Array($elements, $stream->getCurrent()->getLine());
 }
Example #15
0
 /**
  * Parses a string attribute token, saving it as either the title, link URL, or link text
  *
  * @param \Twig_Token $token The token to parse
  * @param \Twig_TokenStream $stream The token stream being traversed
  * @return void
  */
 protected function parseStringAttribute(\Twig_Token $token, \Twig_TokenStream $stream)
 {
     if (!$token->test(\Twig_Token::STRING_TYPE)) {
         throw new RuntimeException(sprintf("Expected string token but received '%s' instead", \Twig_Token::typeToEnglish($token->getType())), $stream->getCurrent()->getLine(), $stream->getFilename());
     }
     // If any of these values has already been set, then set the next
     // property with this string. The string order that must be followed
     // in the codeblock tag is: title, url, link text
     if (empty($this->attributes['title'])) {
         $this->attributes['title'] = $token->getValue();
     } elseif (empty($this->attributes['linkUrl'])) {
         $this->attributes['linkUrl'] = $token->getValue();
     } elseif (empty($this->attributes['linkText'])) {
         $this->attributes['linkText'] = $token->getValue();
     }
 }
 /**
  * @param \Twig_TokenStream $stream
  *
  * @throws \Twig_Error_Syntax
  */
 private function throwSyntaxError(\Twig_TokenStream $stream)
 {
     $token = $stream->getCurrent();
     throw new \Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', \Twig_Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $stream->getFilename());
 }
 public function parse(\Twig_Token $token)
 {
     $inputs = array();
     $filters = array();
     $attributes = array('name' => null, 'output' => $this->output, 'debug' => $this->debug, 'var_name' => 'asset_url');
     $stream = $this->parser->getStream();
     while (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
         if ($stream->test(\Twig_Token::STRING_TYPE)) {
             // '@jquery', 'js/src/core/*', 'js/src/extra.js'
             $inputs[] = $stream->next()->getValue();
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'filter')) {
             // filter='yui_js'
             $stream->next();
             $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
             $filters = array_merge($filters, array_filter(array_map('trim', explode(',', $stream->expect(\Twig_Token::STRING_TYPE)->getValue()))));
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'output')) {
             // output='js/packed/*.js' OR output='js/core.js'
             $stream->next();
             $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
             $attributes['output'] = $stream->expect(\Twig_Token::STRING_TYPE)->getValue();
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'name')) {
             // name='core_js'
             $stream->next();
             $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
             $attributes['name'] = $stream->expect(\Twig_Token::STRING_TYPE)->getValue();
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'as')) {
             // as='the_url'
             $stream->next();
             $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
             $attributes['var_name'] = $stream->expect(\Twig_Token::STRING_TYPE)->getValue();
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'debug')) {
             // debug=true
             $stream->next();
             $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
             $attributes['debug'] = 'true' == $stream->expect(\Twig_Token::NAME_TYPE, array('true', 'false'))->getValue();
         } elseif ($stream->test(\Twig_Token::NAME_TYPE, $this->extensions)) {
             // an arbitrary configured attribute
             $key = $stream->next()->getValue();
             $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
             $attributes[$key] = $stream->expect(\Twig_Token::STRING_TYPE)->getValue();
         } else {
             $token = $stream->getCurrent();
             throw new \Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', \Twig_Token::typeToEnglish($token->getType(), $token->getLine()), $token->getValue()), $token->getLine());
         }
     }
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     $endtag = 'end' . $this->getTag();
     $test = function (\Twig_Token $token) use($endtag) {
         return $token->test($endtag);
     };
     $body = $this->parser->subparse($test, true);
     $stream->expect(\Twig_Token::BLOCK_END_TYPE);
     if ($this->single && 1 < count($inputs)) {
         $inputs = array_slice($inputs, -1);
     }
     if (!isset($attributes['name'])) {
         $attributes['name'] = $this->factory->generateAssetName($inputs, $filters);
     }
     $coll = $this->factory->createAsset($inputs, $filters, $attributes);
     if (!$attributes['debug']) {
         return static::createNode($body, $inputs, $filters, array_replace($attributes, array('output' => $coll->getTargetUrl())), $token->getLine(), $this->getTag());
     }
     $nodes = array();
     foreach ($coll as $leaf) {
         $nodes[] = static::createNode($body, array($leaf->getSourceUrl()), $filters, array_replace($attributes, array('output' => $leaf->getTargetUrl(), 'name' => $attributes['name'] . '_' . count($nodes))), $token->getLine(), $this->getTag());
     }
     return new \Twig_Node($nodes, array(), $token->getLine(), $this->getTag());
 }
Example #18
0
 public function parseHashExpression()
 {
     $stream = $this->parser->getStream();
     $stream->expect(Twig_Token::PUNCTUATION_TYPE, '{', 'A hash element was expected');
     $node = new Twig_Node_Expression_Array(array(), $stream->getCurrent()->getLine());
     $first = true;
     while (!$stream->test(Twig_Token::PUNCTUATION_TYPE, '}')) {
         if (!$first) {
             $stream->expect(Twig_Token::PUNCTUATION_TYPE, ',', 'A hash value must be followed by a comma');
             // trailing ,?
             if ($stream->test(Twig_Token::PUNCTUATION_TYPE, '}')) {
                 break;
             }
         }
         $first = false;
         // a hash key can be:
         //
         //  * a number -- 12
         //  * a string -- 'a'
         //  * a name, which is equivalent to a string -- a
         //  * an expression, which must be enclosed in parentheses -- (1 + 2)
         if (($token = $stream->nextIf(Twig_Token::STRING_TYPE)) || ($token = $stream->nextIf(Twig_Token::NAME_TYPE)) || ($token = $stream->nextIf(Twig_Token::NUMBER_TYPE))) {
             $key = new Twig_Node_Expression_Constant($token->getValue(), $token->getLine());
         } elseif ($stream->test(Twig_Token::PUNCTUATION_TYPE, '(')) {
             $key = $this->parseExpression();
         } else {
             $current = $stream->getCurrent();
             throw new Twig_Error_Syntax(sprintf('A hash key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "%s" of value "%s"', Twig_Token::typeToEnglish($current->getType(), $current->getLine()), $current->getValue()), $current->getLine(), $this->parser->getFilename());
         }
         $stream->expect(Twig_Token::PUNCTUATION_TYPE, ':', 'A hash key must be followed by a colon (:)');
         $value = $this->parseExpression();
         $node->addElement($value, $key);
     }
     $stream->expect(Twig_Token::PUNCTUATION_TYPE, '}', 'An opened hash is not properly closed');
     return $node;
 }
Example #19
0
    public function parse(\Twig_Token $token)
    {
        $inputs  = array();
        $output  = $this->defaultOutput;
        $filters = array();
        $name    = null;
        $debug   = $this->debug;

        $stream = $this->parser->getStream();
        while (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) {
            if ($stream->test(\Twig_Token::STRING_TYPE)) {
                // '@jquery', 'js/src/core/*', 'js/src/extra.js'
                $inputs[] = $stream->next()->getValue();
            } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'filter')) {
                // filter='yui_js'
                $stream->next();
                $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
                $filters = array_merge($filters, array_map('trim', explode(',', $stream->expect(\Twig_Token::STRING_TYPE)->getValue())));
            } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'output')) {
                // output='js' OR output='js/packed/*.js' OR output='js/core.js'
                $stream->next();
                $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
                $output = $stream->expect(\Twig_Token::STRING_TYPE)->getValue();
            } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'name')) {
                // name='core_js'
                $stream->next();
                $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
                $name = $stream->expect(\Twig_Token::STRING_TYPE)->getValue();
            } elseif ($stream->test(\Twig_Token::NAME_TYPE, 'debug')) {
                // debug=true
                $stream->next();
                $stream->expect(\Twig_Token::OPERATOR_TYPE, '=');
                $debug = 'true' == $stream->expect(\Twig_Token::NAME_TYPE, array('true', 'false'))->getValue();
            } else {
                $token = $stream->getCurrent();
                throw new \Twig_Error_Syntax(sprintf('Unexpected token "%s" of value "%s"', \Twig_Token::typeToEnglish($token->getType(), $token->getLine()), $token->getValue()), $token->getLine());
            }
        }

        $stream->expect(\Twig_Token::BLOCK_END_TYPE);

        $endtag = 'end'.$this->getTag();
        $test = function(\Twig_Token $token) use($endtag) { return $token->test($endtag); };
        $body = $this->parser->subparse($test, true);

        $stream->expect(\Twig_Token::BLOCK_END_TYPE);

        if (null === $name) {
            $name = $this->factory->generateAssetName($inputs, $filters);
        }

        $coll = $this->factory->createAsset($inputs, $filters, array(
            'output' => $output,
            'name'   => $name,
            'debug'  => $debug,
        ));

        if (!$debug) {
            return static::createNode($body, $inputs, $coll->getTargetUrl(), $filters, $name, $debug, $token->getLine(), $this->getTag());
        }

        $nodes = array();
        foreach ($coll as $asset) {
            $nodes[] = static::createNode($body, array($asset->getSourceUrl()), $asset->getTargetUrl(), $filters, $name.'_'.count($nodes), $debug, $token->getLine(), $this->getTag());
        }

        return new \Twig_Node($nodes, array(), $token->getLine(), $this->getTag());
    }