diff --git a/Cache.php b/Cache.php index 9deb51d..728ebd2 100755 --- a/Cache.php +++ b/Cache.php @@ -145,6 +145,44 @@ protected function mkdir($directory) * @return string */ public function getCacheFile($filename, $actual = false, $mkdir = false) + { + return $this->getFullPath($this->buildPath($filename), $filename, $actual, $mkdir); + } + + /** + * Get the full path to the cache file. + * + * @param string $path + * @param string $filename + * @param bool $actual + * @param bool $mkdir + * @return string + */ + protected function getFullPath($path, $filename, $actual, $mkdir = false) + { + $actualDir = $this->getActualCacheDirectory() . '/' . $path; + if ($mkdir) { + $this->mkdir($actualDir, $this->directoryMode, true); + } + + $path .= '/' . $filename; + + if ($actual) { + return $this->getActualCacheDirectory() . '/' . $path; + } else { + return $this->getCacheDirectory() . '/' . $path; + } + + return $path; + } + + /** + * Build the path for the given filename. + * + * @param string $filename + * @return string + */ + protected function buildPath($filename) { $path = array(); @@ -158,18 +196,7 @@ public function getCacheFile($filename, $actual = false, $mkdir = false) } $path = implode('/', $path); - if ($mkdir) { - $actualDir = $this->getActualCacheDirectory() . '/' . $path; - $this->mkdir($actualDir); - } - - $path .= '/' . $filename; - - if ($actual) { - return $this->getActualCacheDirectory() . '/' . $path; - } else { - return $this->getCacheDirectory() . '/' . $path; - } + return $path; } /** @@ -260,13 +287,22 @@ public function check($filename, array $conditions = array()) */ public function set($filename, $contents = '') { - $cacheFile = $this->getCacheFile($filename, true, true); - - file_put_contents($cacheFile, $contents, \LOCK_EX); + $this->setAbsolute($this->getCacheFile($filename, true, true), $contents); return $this; } + /** + * Write data to the given absolute file path. + * + * @param string $cacheFile + * @param string $contents + */ + protected function setAbsolute($cacheFile, $contents = '') + { + file_put_contents($cacheFile, $contents, \LOCK_EX); + } + /** * Alias for set() * @@ -327,10 +363,11 @@ public function getOrCreate($filename, array $conditions = array(), $function, $ throw new \InvalidArgumentException('The argument $function should be callable'); } - $cacheFile = $this->getCacheFile($filename, true, true); + $path = $this->buildPath($filename); + $cacheFile = $this->getFullPath($path, $filename, true, true); $data = null; - if (!$this->check($filename, $conditions)) { + if (!$this->checkConditions($cacheFile, $conditions)) { if(file_exists($cacheFile)) { unlink($cacheFile); } @@ -339,13 +376,13 @@ public function getOrCreate($filename, array $conditions = array(), $function, $ // Test if the closure wrote the file or if it returned the data if (!file_exists($cacheFile)) { - $this->set($filename, $data); + $this->setAbsolute($cacheFile, $data); } else { $data = file_get_contents($cacheFile); } } - return $file ? $this->getCacheFile($filename, $actual) : file_get_contents($cacheFile); + return $file ? $this->getFullPath($path, $filename, $actual) : file_get_contents($cacheFile); } /**