From: Fabien Potencier Date: Wed, 5 Jan 2011 19:33:01 +0000 (+0100) Subject: changed cache file names so that they are spread over a directory tree X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=dbc5bdbd875decb7623e2c73a60e3b0f6ad41354;p=web%2Fkonrad%2Ftwig.git changed cache file names so that they are spread over a directory tree --- diff --git a/lib/Twig/Environment.php b/lib/Twig/Environment.php index a839674..3a5c034 100644 --- a/lib/Twig/Environment.php +++ b/lib/Twig/Environment.php @@ -240,7 +240,13 @@ class Twig_Environment */ public function getCacheFilename($name) { - return $this->getCache() ? $this->getCache().'/'.$this->getTemplateClass($name).'.php' : false; + if (false === $this->cache) { + return false; + } + + $class = substr($this->getTemplateClass($name), strlen($this->templateClassPrefix)); + + return $this->getCache().'/'.substr($class, 0, 2).'/'.substr($class, 2, 4).'/'.substr($class, 4).'.php'; } /** @@ -302,11 +308,13 @@ class Twig_Environment */ public function clearCacheFiles() { - if ($this->cache) { - foreach(new DirectoryIterator($this->cache) as $fileInfo) { - if (0 === strpos($fileInfo->getFilename(), $this->templateClassPrefix)) { - @unlink($fileInfo->getPathname()); - } + if (false === $this->cache) { + return; + } + + foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->cache), RecursiveIteratorIterator::LEAVES_ONLY) as $file) { + if ($file->isFile()) { + @unlink($file->getPathname()); } } } @@ -816,6 +824,10 @@ class Twig_Environment protected function writeCacheFile($file, $content) { + if (!is_dir(dirname($file))) { + mkdir(dirname($file), 0777, true); + } + $tmpFile = tempnam(dirname($file), basename($file)); if (false !== @file_put_contents($tmpFile, $content)) { // rename does not work on Win32 before 5.2.6