diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php
index 0dada3d7d172f..4181ccd8fd30f 100644
--- a/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php
+++ b/src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php
@@ -83,19 +83,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$realCacheDir = $kernel->getContainer()->getParameter('kernel.cache_dir');
// the old cache dir name must not be longer than the real one to avoid exceeding
// the maximum length of a directory or file path within it (esp. Windows MAX_PATH)
- $oldBuildDir = substr($realBuildDir, 0, -1).('~' === substr($realBuildDir, -1) ? '+' : '~');
$oldCacheDir = substr($realCacheDir, 0, -1).('~' === substr($realCacheDir, -1) ? '+' : '~');
- $fs->remove([$oldBuildDir, $oldCacheDir]);
+ $fs->remove($oldCacheDir);
- if (!is_writable($realBuildDir)) {
- throw new RuntimeException(sprintf('Unable to write in the "%s" directory.', $realBuildDir));
- }
if (!is_writable($realCacheDir)) {
throw new RuntimeException(sprintf('Unable to write in the "%s" directory.', $realCacheDir));
}
+ $useBuildDir = $realBuildDir !== $realCacheDir;
+ if ($useBuildDir) {
+ $oldBuildDir = substr($realBuildDir, 0, -1).('~' === substr($realBuildDir, -1) ? '+' : '~');
+ $fs->remove($oldBuildDir);
+
+ if (!is_writable($realBuildDir)) {
+ throw new RuntimeException(sprintf('Unable to write in the "%s" directory.', $realBuildDir));
+ }
+ }
+
$io->comment(sprintf('Clearing the cache for the %s environment with debug %s', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
- $this->cacheClearer->clear($realBuildDir);
+ if ($useBuildDir) {
+ $this->cacheClearer->clear($realBuildDir);
+ }
$this->cacheClearer->clear($realCacheDir);
// The current event dispatcher is stale, let's not use it anymore
@@ -141,7 +149,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
if (!$fs->exists($warmupDir.'/'.$containerDir)) {
- $fs->rename($realCacheDir.'/'.$containerDir, $warmupDir.'/'.$containerDir);
+ $fs->rename($realBuildDir.'/'.$containerDir, $warmupDir.'/'.$containerDir);
touch($warmupDir.'/'.$containerDir.'.legacy');
}
@@ -161,31 +169,33 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}
}
- if ($oldBuildDir) {
- $fs->rename($realBuildDir, $oldBuildDir);
- } else {
- $fs->remove($realBuildDir);
- }
if ($oldCacheDir) {
$fs->rename($realCacheDir, $oldCacheDir);
} else {
$fs->remove($realCacheDir);
}
$fs->rename($warmupDir, $realCacheDir);
- // Copy the content of the warmed cache in the build dir
- $fs->copy($realCacheDir, $realBuildDir);
+
+ if ($useBuildDir) {
+ $fs->rename($realBuildDir, $oldBuildDir);
+ // Copy the content of the warmed cache in the build dir
+ $fs->mirror($realCacheDir, $realBuildDir);
+ }
if ($output->isVerbose()) {
$io->comment('Removing old build and cache directory...');
}
- try {
- $fs->remove($oldBuildDir);
- } catch (IOException $e) {
- if ($output->isVerbose()) {
- $io->warning($e->getMessage());
+ if ($useBuildDir) {
+ try {
+ $fs->remove($oldBuildDir);
+ } catch (IOException $e) {
+ if ($output->isVerbose()) {
+ $io->warning($e->getMessage());
+ }
}
}
+
try {
$fs->remove($oldCacheDir);
} catch (IOException $e) {