summaryrefslogtreecommitdiff
path: root/view/Versioning.php
blob: f75afe13731c686988a159efa3dd57dff9affca6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
// view/Versioning.php

class Versioning {
    static function insertCSS(string $name): string
    {
        return '<link rel="stylesheet" href="' . self::versionedFileURL('css', $name) . '">' . "\n";
    }
    static function insertJS(string $name, bool $module = false): string
    {
        return '<script ' . ($module ? 'type="module"' : '') . ' src="' . self::versionedFileURL('js', $name) . '"></script>' . "\n";
    }

    static function versionedFileURL(string $type, string $filename, ?string $custom_path = null): string
    {
        $path = $custom_path ?? 'public/' . $type . '/' . $filename . '.' . $type; // si custom_path, $type et $filename ne servent à rien

        if(file_exists(getcwd() . '/' . $path)){
            $version = substr(md5_file($path), 0, 8);
            return $path . '?v=' . $version;
        }
        return $path; // sécurité fichier absent
    }
}