8000 [FrameworkBundle] fixed cahe:clear command's warmup · symfony/symfony@7d87ecd · GitHub
[go: up one dir, main page]

Skip to content

Commit 7d87ecd

Browse files
committed
[FrameworkBundle] fixed cahe:clear command's warmup
1 parent 940d591 commit 7d87ecd

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

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

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
7575
if ($input->getOption('no-warmup')) {
7676
$filesystem->rename($realCacheDir, $oldCacheDir);
7777
} else {
78-
$warmupDir = $realCacheDir.'_new';
78+
// the warmup cache dir name must have the have length than the real one
79+
// to avoid the many problems in serialized resources files
80+
$warmupDir = substr($realCacheDir, 0, -1).'_';
7981

8082
if ($filesystem->exists($warmupDir)) {
8183
$filesystem->remove($warmupDir);
8284
}
8385

84-
$this->warmup($warmupDir, !$input->getOption('no-optional-warmers'));
86+
$this->warmup($warmupDir, $realCacheDir, !$input->getOption('no-optional-warmers'));
8587

8688
$filesystem->rename($realCacheDir, $oldCacheDir);
8789
$filesystem->rename($warmupDir, $realCacheDir);
@@ -90,7 +92,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
9092
$filesystem->remove($oldCacheDir);
9193
}
9294

93-
protected function warmup($warmupDir, $enableOptionalWarmers = true)
95+
protected function warmup($warmupDir, $realCacheDir, $enableOptionalWarmers = true)
9496
{
9597
$this->getContainer()->get('filesystem')->remove($warmupDir);
9698

@@ -113,40 +115,28 @@ protected function warmup($warmupDir, $enableOptionalWarmers = true)
113115

114116
$warmer->warmUp($warmupDir);
115117

118+
// fix references to the Kernel in .meta files
116119
foreach (Finder::create()->files()->name('*.meta')->in($warmupDir) as $file) {
117-
// fix meta references to the Kernel
118-
$content = preg_replace(
120+
file_put_contents($file, preg_replace(
119121
'/C\:\d+\:"'.preg_quote($class.$this->getTempKernelSuffix(), '"/').'"/',
120122
sprintf('C:%s:"%s"', strlen($class), $class),
121123
file_get_contents($file)
122-
);
123-
124-
// fix meta references to cache files
125-
$realWarmupDir = substr($warmupDir, 0, -4);
126-
$content = preg_replace_callback(
127-
'/s\:\d+\:"'.preg_quote($warmupDir, '/').'([^"]+)"/',
128-
function (array $matches) use ($realWarmupDir) {
129-
$path = $realWarmupDir.$matches[1];
130-
return sprintf('s:%s:"%s"', strlen($path), $path);
131-
},
132-
$content
133-
);
134-
135-
file_put_contents($file, $content);
124+
));
136125
}
137126

138-
// fix container files and classes
139-
$regex = '/'.preg_quote($this->getTempKernelSuffix(), '/').'/';
127+
// fix kernel class names in container-specific cache classes
128+
// and rename those classes by removing temp suffix
140129
foreach (Finder::create()->files()->name(get_class($kernel->getContainer()).'*')->in($warmupDir) as $file) {
141-
$content = file_get_contents($file);
142-
$content = preg_replace($regex, '', $content);
143-
144-
// fix absolute paths to the cache directory
145-
$content = preg_replace('/'.preg_quote($warmupDir, '/').'/', preg_replace('/_new$/', '', $warmupDir), $content);
146-
147-
file_put_contents(preg_replace($regex, '', $file), $content);
130+
$content = str_replace($this->getTempKernelSuffix(), '', file_get_contents($file));
131+
file_put_contents(str_replace($this->getTempKernelSuffix(), '', $file), $content);
148132
unlink($file);
149133
}
134+
135+
// fix references to cached files with the real cache directory name
136+
foreach (Finder::create()->files()->in($warmupDir) as $file) {
137+
$content = str_replace($warmupDir, $realCacheDir, file_get_contents($file));
138+
file_put_contents($file, $content);
139+
}
150140
}
151141

152142
protected function getTempKernelSuffix()

0 commit comments

Comments
 (0)
0