Skip to content

Commit 8fa0573

Browse files
bug #38548 [FrameworkBundle] Bugfixes in buildDir in the CacheClear command (Nyholm)
This PR was squashed before being merged into the 5.x branch. Discussion ---------- [FrameworkBundle] Bugfixes in buildDir in the CacheClear command | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #38547 | License | MIT | Doc PR | n/a Making sure one can clear cache with and without a buildDir Commits ------- 2cad6bb [FrameworkBundle] Bugfixes in buildDir in the CacheClear command
2 parents e83ad8b + 2cad6bb commit 8fa0573

File tree

1 file changed

+29
-19
lines changed

1 file changed

+29
-19
lines changed

src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php

+29-19
Original file line numberDiff line numberDiff line change
@@ -83,19 +83,27 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8383
$realCacheDir = $kernel->getContainer()->getParameter('kernel.cache_dir');
8484
// the old cache dir name must not be longer than the real one to avoid exceeding
8585
// the maximum length of a directory or file path within it (esp. Windows MAX_PATH)
86-
$oldBuildDir = substr($realBuildDir, 0, -1).('~' === substr($realBuildDir, -1) ? '+' : '~');
8786
$oldCacheDir = substr($realCacheDir, 0, -1).('~' === substr($realCacheDir, -1) ? '+' : '~');
88-
$fs->remove([$oldBuildDir, $oldCacheDir]);
87+
$fs->remove($oldCacheDir);
8988

90-
if (!is_writable($realBuildDir)) {
91-
throw new RuntimeException(sprintf('Unable to write in the "%s" directory.', $realBuildDir));
92-
}
9389
if (!is_writable($realCacheDir)) {
9490
throw new RuntimeException(sprintf('Unable to write in the "%s" directory.', $realCacheDir));
9591
}
9692

93+
$useBuildDir = $realBuildDir !== $realCacheDir;
94+
if ($useBuildDir) {
95+
$oldBuildDir = substr($realBuildDir, 0, -1).('~' === substr($realBuildDir, -1) ? '+' : '~');
96+
$fs->remove($oldBuildDir);
97+
98+
if (!is_writable($realBuildDir)) {
99+
throw new RuntimeException(sprintf('Unable to write in the "%s" directory.', $realBuildDir));
100+
}
101+
}
102+
97103
$io->comment(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
98-
$this->cacheClearer->clear($realBuildDir);
104+
if ($useBuildDir) {
105+
$this->cacheClearer->clear($realBuildDir);
106+
}
99107
$this->cacheClearer->clear($realCacheDir);
100108

101109
// The current event dispatcher is stale, let's not use it anymore
@@ -141,7 +149,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
141149
}
142150

143151
if (!$fs->exists($warmupDir.'/'.$containerDir)) {
144-
$fs->rename($realCacheDir.'/'.$containerDir, $warmupDir.'/'.$containerDir);
152+
$fs->rename($realBuildDir.'/'.$containerDir, $warmupDir.'/'.$containerDir);
145153
touch($warmupDir.'/'.$containerDir.'.legacy');
146154
}
147155

@@ -161,31 +169,33 @@ protected function execute(InputInterface $input, OutputInterface $output): int
161169
}
162170
}
163171

164-
if ($oldBuildDir) {
165-
$fs->rename($realBuildDir, $oldBuildDir);
166-
} else {
167-
$fs->remove($realBuildDir);
168-
}
169172
if ($oldCacheDir) {
170173
$fs->rename($realCacheDir, $oldCacheDir);
171174
} else {
172175
$fs->remove($realCacheDir);
173176
}
174177
$fs->rename($warmupDir, $realCacheDir);
175-
// Copy the content of the warmed cache in the build dir
176-
$fs->copy($realCacheDir, $realBuildDir);
178+
179+
if ($useBuildDir) {
180+
$fs->rename($realBuildDir, $oldBuildDir);
181+
// Copy the content of the warmed cache in the build dir
182+
$fs->mirror($realCacheDir, $realBuildDir);
183+
}
177184

178185
if ($output->isVerbose()) {
179186
$io->comment('Removing old build and cache directory...');
180187
}
181188

182-
try {
183-
$fs->remove($oldBuildDir);
184-
} catch (IOException $e) {
185-
if ($output->isVerbose()) {
186-
$io->warning($e->getMessage());
189+
if ($useBuildDir) {
190+
try {
191+
$fs->remove($oldBuildDir);
192+
} catch (IOException $e) {
193+
if ($output->isVerbose()) {
194+
$io->warning($e->getMessage());
195+
}
187196
}
188197
}
198+
189199
try {
190200
$fs->remove($oldCacheDir);
191201
} catch (IOException $e) {

0 commit comments

Comments
 (0)