Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.2] Fix edge-case cache:clear fails #6747

Merged
merged 1 commit into from
Jun 8, 2017
Merged
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
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;
}
}