54 lines
1.5 KiB
PHP
54 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Wersja statycznego pliku w public/ — timestamp modyfikacji (cache busting po FTP).
|
|
*/
|
|
function publicAssetVersion(string $publicDir, string $relativePath): string
|
|
{
|
|
$path = $publicDir . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, ltrim($relativePath, '/'));
|
|
if (!is_file($path)) {
|
|
return '0';
|
|
}
|
|
|
|
$mtime = filemtime($path);
|
|
|
|
return $mtime !== false ? (string) $mtime : '0';
|
|
}
|
|
|
|
/**
|
|
* Najnowszy mtime wśród pliku głównego i wszystkich .js w katalogach modules/ i locales/.
|
|
*/
|
|
function publicJsBundleVersion(string $publicDir, string $entryRelativePath): string
|
|
{
|
|
$versions = [(int) publicAssetVersion($publicDir, $entryRelativePath)];
|
|
$dirs = [
|
|
'assets/js/modules',
|
|
'assets/js/locales',
|
|
];
|
|
|
|
foreach ($dirs as $relativeDir) {
|
|
$modulesDir = $publicDir . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, ltrim($relativeDir, '/'));
|
|
if (!is_dir($modulesDir)) {
|
|
continue;
|
|
}
|
|
|
|
$iterator = new RecursiveIteratorIterator(
|
|
new RecursiveDirectoryIterator($modulesDir, FilesystemIterator::SKIP_DOTS)
|
|
);
|
|
foreach ($iterator as $file) {
|
|
if ($file->isFile() && strtolower($file->getExtension()) === 'js') {
|
|
$versions[] = (int) $file->getMTime();
|
|
}
|
|
}
|
|
}
|
|
|
|
return (string) max($versions);
|
|
}
|
|
|
|
function assetVersionAttr(string $version): string
|
|
{
|
|
return htmlspecialchars($version, ENT_QUOTES, 'UTF-8');
|
|
}
|