Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 56 additions & 19 deletions Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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()
*
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}

/**
Expand Down