Skip to content

Commit

Permalink
added optimizied asset hashing function
Browse files Browse the repository at this point in the history
  • Loading branch information
schmunk42 committed Dec 14, 2018
1 parent 989532d commit 928bf3d
Showing 1 changed file with 47 additions and 6 deletions.
53 changes: 47 additions & 6 deletions src/AssetHash.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,10 @@ static public function byFileTimeAndLess()
$max = max($max, filemtime($file), filectime($file));
}

$hash = substr(hash('sha256', $path.$max), 0, 6).
'-'.APP_VERSION.'-c'.\Yii::$app->cache->get('prototype.less.changed_at');
Yii::trace(['byFileTimeAndLess', $path, count($files), Yii::$app->formatter->asRelativeTime($max), $hash], __METHOD__);
$hash = substr(hash('sha256', $path . $max), 0, 6) .
'-' . APP_VERSION . '-c' . \Yii::$app->cache->get('prototype.less.changed_at');
Yii::trace(['byFileTimeAndLess', $path, count($files), Yii::$app->formatter->asRelativeTime($max), $hash],
__METHOD__);

return $hash;
};
Expand Down Expand Up @@ -67,12 +68,52 @@ static public function byFileTimeAndLessDevelopment()

$modificationHash = substr(hash('sha256', $max), 0, 6);

$hash = 'dev-v'.APP_VERSION.
'-l'.\Yii::$app->cache->get('prototype.less.changed_at').'/'.strtr($path,['/'=>'|']).'-t'.$modificationHash;
Yii::trace(['byFileTimeAndLess', $path, count($files), Yii::$app->formatter->asRelativeTime($max), $hash], __METHOD__);
$hash = 'dev' .
'-v' . APP_VERSION .
'-p' . strtr($path, ['/' => '|']) .
'-t' . $modificationHash;
Yii::trace(['byFileTimeAndLessDevelopment', $path, count($files), Yii::$app->formatter->asRelativeTime($max), $hash],
__METHOD__);

return $hash;
};
}

/**
* Returns asset hashes for production
*
* Checks mtime, ctime by folder
*
* @return \Closure
*/
static public function byFileTime($obfuscateHash = true)
{
return function ($path) use ($obfuscateHash) {
if (is_file($path)) {
$files[] = $path;
} else {
$files = \yii\helpers\FileHelper::findFiles($path, ['only' => ['*.js', '*.css', '*.less']]);
}


$max = 0;
foreach ($files as $file) {
$max = max($max, filemtime($file), filectime($file));
}

$hash = YII_ENV .
'-r' . PROJECT_VERSION .
'-v' . APP_VERSION .
'-p' . strtr($path, ['/' => '_']) .
'-t' . $max;
Yii::trace(['byFileTime', $path, count($files), Yii::$app->formatter->asRelativeTime($max), $hash],
__METHOD__);

if ($obfuscateHash) {
return md5($hash);
}

return $hash;
};
}
}

0 comments on commit 928bf3d

Please sign in to comment.