function AssetGroupSetHashTrait::generateHash

Same name and namespace in other branches
  1. 10 core/lib/Drupal/Core/Asset/AssetGroupSetHashTrait.php \Drupal\Core\Asset\AssetGroupSetHashTrait::generateHash()

Generates a hash for an array of asset groups.

Parameters

array $group: An asset group.

Return value

string A hash to uniquely identify the groups.

3 calls to AssetGroupSetHashTrait::generateHash()
AssetControllerBase::deliver in core/modules/system/src/Controller/AssetControllerBase.php
Generates an aggregate, given a filename.
CssCollectionOptimizerLazy::optimize in core/lib/Drupal/Core/Asset/CssCollectionOptimizerLazy.php
Optimizes a collection of assets.
JsCollectionOptimizerLazy::optimize in core/lib/Drupal/Core/Asset/JsCollectionOptimizerLazy.php
Optimizes a collection of assets.

File

core/lib/Drupal/Core/Asset/AssetGroupSetHashTrait.php, line 22

Class

AssetGroupSetHashTrait
Provides a method to generate a normalized hash of a given asset group set.

Namespace

Drupal\Core\Asset

Code

protected function generateHash(array $group) : string {
    $normalized = [];
    $group_keys = [
        'type' => NULL,
        'group' => NULL,
        'media' => NULL,
        'browsers' => NULL,
    ];
    $normalized['asset_group'] = array_intersect_key($group, $group_keys);
    $normalized['asset_group']['items'] = [];
    // Remove some keys to make the hash more stable.
    $omit_keys = [
        'weight' => NULL,
    ];
    foreach ($group['items'] as $key => $asset) {
        $normalized['asset_group']['items'][$key] = array_diff_key($asset, $group_keys, $omit_keys);
        // If the version is set to -1, this means there is no version in the
        // library definition. To ensure unique hashes when unversioned files
        // change, replace the version with a hash of the file contents.
        if ($asset['version'] === -1) {
            $normalized['asset_group']['items'][$key]['version'] = hash('xxh64', file_get_contents($asset['data']));
        }
    }
    // The asset array ensures that a valid hash can only be generated via the
    // same code base. Additionally use the hash salt to ensure that hashes are
    // not re-usable between different installations.
    return Crypt::hmacBase64(serialize($normalized), Settings::getHashSalt());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.