twigphp / Twig

Twig, the flexible, fast, and secure template language for PHP
https://twig.symfony.com/
BSD 3-Clause "New" or "Revised" License
8.18k stars 1.25k forks source link

Force recompile of cached twig files when PHP version changes #2013

Closed mahagr closed 8 years ago

mahagr commented 8 years ago

Many cached PHP template files contain code that is specific to the PHP version you are using. This causes fatal errors for example in test sites, which have multiple different PHP versions installed.

Right now Twig_Cache_Filesystem has this code:

    public function generateKey($name, $className)
    {
        $hash = hash('sha256', $className);
        return $this->directory.$hash[0].$hash[1].'/'.$hash.'.php';
    }

Should something like PHP_VERSION be added to the key, maybe inside the $hash?

mahagr commented 8 years ago

Unfortunately $this->directory has private access, so I cannot just extend the class either.

mahagr commented 8 years ago

I copied the class and used the following code myself:

    public function generateKey($name, $className)
    {
        $hash = hash('sha256', $className . '-' . PHP_VERSION);
        return $this->directory.$hash[0].$hash[1].'/'.$hash.'.php';
    }
fabpot commented 8 years ago

Doing this with PHP_VERSION is not a good idea as people might generate the cache on PHP 5.4.3 and then deploy this on a PHP 5.4.4 production server, which would invalidate the cache for no reasons as the cache is exactly the same in this case. So, we need to only differentiate the cache when Twig actually generates a different PHP code.

See #2125

mahagr commented 8 years ago

Agreed, but see my comment/concern in the pull request.