kriswallsmith / assetic

Asset Management for PHP
MIT License
3.75k stars 555 forks source link

assetic writer with checksum? #857

Open featheredtoast opened 7 years ago

featheredtoast commented 7 years ago

Would you be up to add an md5 checksum to the AsseticWriter to prevent files from modifying with the same data? This would be great for development pipelines, as it'd prevent etags from being changed, and thus be able to more accurately predict when resources change.

My use case comes in from using assetic + live.js for development environments, in which I only want css to recompute and emit a new etag, while all the javascripts prevent a reload of the page.

Something like the following modifications in the AssetWriter:

  protected static function write($path, $contents)
    {
        if (!is_dir($dir = dirname($path)) && false === @mkdir($dir, 0777, true)) {
            throw new \RuntimeException('Unable to create directory '.$dir);
        }

        if(file_exists($path) && md5($contents) == md5(@file_get_contents($path)))
        {
            echo "Not modified: ".$path."\n";
            return;
        }

        if (false === @file_put_contents($path, $contents)) {
            throw new \RuntimeException('Unable to write file '.$path);
        }
    }

Due to the way the variables are private currently, there is no good way of extending this class, short of re-implementing it.