Author: Harold Iedema (hiedema@hostnet.nl)
Example #1
0
 /**
  * Returns an array of split points from the given template file.
  *
  * @param  string $template_file
  * @return array
  */
 public function findSplitPoints($template_file)
 {
     $inline_blocks = 0;
     $source = new \Twig_Source(file_get_contents($template_file), $template_file);
     $stream = $this->twig->tokenize($source);
     $points = [];
     while (!$stream->isEOF() && ($token = $stream->next())) {
         // {{ webpack_asset(...) }}
         if ($token->test(\Twig_Token::NAME_TYPE, 'webpack_asset')) {
             // We found the webpack function!
             $asset = $this->getAssetFromStream($template_file, $stream);
             $points[$asset] = $this->resolveAssetPath($asset, $template_file, $token);
         }
         // {% webpack_javascripts %} and {% webpack_stylesheets %}
         if ($token->test(\Twig_Token::BLOCK_START_TYPE) && $stream->getCurrent()->test(WebpackTokenParser::TAG_NAME)) {
             $stream->next();
             if ($stream->getCurrent()->getValue() === 'inline') {
                 $stream->next();
                 $token = $stream->next();
                 $file_name = TwigParser::hashInlineFileName($template_file, $inline_blocks);
                 // Are we dealing with a custom extension? If not, fallback to javascript.
                 $extension = 'js';
                 // Default
                 if ($token->test(\Twig_Token::NAME_TYPE)) {
                     $extension = $token->getValue();
                     $stream->next();
                 }
                 file_put_contents($this->cache_dir . '/' . $file_name . '.' . $extension, $this->stripScript($stream->getCurrent()->getValue()));
                 $asset = $file_name . '.' . $extension;
                 $id = 'cache/' . $asset;
                 $points[$id] = $this->resolveAssetPath($this->cache_dir . '/' . $asset, $template_file, $token);
                 $inline_blocks++;
             } else {
                 $stream->next();
                 while (!$stream->isEOF() && !$stream->getCurrent()->test(\Twig_Token::BLOCK_END_TYPE)) {
                     $asset = $stream->expect(\Twig_Token::STRING_TYPE)->getValue();
                     $points[$asset] = $this->resolveAssetPath($asset, $template_file, $token);
                 }
             }
         }
     }
     return $points;
 }
 /**
  * @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 = TwigParser::hashInlineFileName($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());
 }