Hostnet\Component\Webpack\Asset\TwigParser::hashInlineFileName PHP Method

hashInlineFileName() public static method

Consistently calculate file name hashes.
public static hashInlineFileName ( $template_file, $block_index ) : string
$template_file
$block_index
return string
    public static function hashInlineFileName($template_file, $block_index)
    {
        // Work around path inconsistencies on Windows/XAMPP.
        if (DIRECTORY_SEPARATOR == '\\') {
            $template_file = str_replace('\\', '/', $template_file);
        }
        $hash = md5($template_file . $block_index);
        return $hash;
    }

Usage Example

Exemplo n.º 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;
 }
All Usage Examples Of Hostnet\Component\Webpack\Asset\TwigParser::hashInlineFileName