Skip to content

Commit

Permalink
Merge pull request #6747 from GawainLynch/hotfix/race-around-the-cache
Browse files Browse the repository at this point in the history
[3.2] Fix edge-case cache:clear fails
  • Loading branch information
bobdenotter committed Jun 8, 2017
2 parents 5683744 + 03f0044 commit 568959e
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class Cache extends FilesystemCache

/** @var AggregateFilesystemInterface */
private $filesystem;
/** @var int */
private $umask;

/**
* Cache constructor.
Expand All @@ -38,6 +40,7 @@ public function __construct($directory, $extension = self::EXTENSION, $umask = 0
{
parent::__construct($directory, $extension, $umask);
$this->filesystem = $filesystem;
$this->umask = $umask;
}

/**
Expand Down Expand Up @@ -68,22 +71,25 @@ protected function doFlush()
$result = parent::doFlush();

if ($this->filesystem instanceof AggregateFilesystemInterface) {
try {
// Clear our cached configuration
$this->filesystem->getFilesystem('cache')->delete('config-cache.json');
} catch (Filesystem\Exception\FileNotFoundException $e) {
// Ç'est la vie
$cacheFs = $this->filesystem->getFilesystem('cache');
// Clear our cached configuration
if ($cacheFs->has('config-cache.json')) {
$cacheFs->delete('config-cache.json');
}

// Clear our own cache folder.
$this->flushDirectory($this->filesystem->getFilesystem('cache')->getDir('/development'));
$this->flushDirectory($this->filesystem->getFilesystem('cache')->getDir('/exception'));
$this->flushDirectory($this->filesystem->getFilesystem('cache')->getDir('/production'));
$this->flushDirectory($this->filesystem->getFilesystem('cache')->getDir('/profiler'));
$this->flushDirectory($this->filesystem->getFilesystem('cache')->getDir('/trans'));
$this->flushDirectory($cacheFs->getDir('/development'));
$this->flushDirectory($cacheFs->getDir('/exception'));
$this->flushDirectory($cacheFs->getDir('/production'));
$this->flushDirectory($cacheFs->getDir('/profiler'));
$this->flushDirectory($cacheFs->getDir('/trans'));

// Clear the thumbs folder.
$this->flushDirectory($this->filesystem->getFilesystem('web')->getDir('/thumbs'));

// We need to recreate our base Doctrine cache directory, as it
// will be a subdirectory of one of the ones we just wiped.
$this->createPathIfNeeded();
}

return $result;
Expand Down Expand Up @@ -113,4 +119,18 @@ private function flushDirectory(DirectoryInterface $directory)
}
}
}

/**
* Create base path path if needed.
*
* @return bool
*/
private function createPathIfNeeded()
{
if (!is_dir($this->directory)) {
return @mkdir($this->directory, 0777 & (~$this->umask), true) && !is_dir($this->directory);
}

return true;
}
}

0 comments on commit 568959e

Please sign in to comment.