Skip to content

Commit 7dd6295

Browse files
[HttpKernel][FrameworkBundle] Add RebootableInterface, fix and un-deprecate cache:clear with warmup
1 parent 2b16423 commit 7dd6295

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

Command/CacheClearCommand.php

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Filesystem\Filesystem;
2020
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
2121
use Symfony\Component\HttpKernel\KernelInterface;
22+
use Symfony\Component\HttpKernel\RebootableInterface;
2223
use Symfony\Component\Finder\Finder;
2324

2425
/**
@@ -33,6 +34,7 @@ class CacheClearCommand extends ContainerAwareCommand
3334
{
3435
private $cacheClearer;
3536
private $filesystem;
37+
private $warning;
3638

3739
/**
3840
* @param CacheClearerInterface $cacheClearer
@@ -112,6 +114,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
112114
$this->filesystem->rename($realCacheDir, $oldCacheDir);
113115
} else {
114116
$this->warmupCache($input, $output, $realCacheDir, $oldCacheDir);
117+
118+
if ($this->warning) {
119+
@trigger_error($this->warning, E_USER_DEPRECATED);
120+
$io->warning($this->warning);
121+
$this->warning = null;
122+
}
115123
}
116124

117125
if ($output->isVerbose()) {
@@ -167,17 +175,23 @@ protected function warmup($warmupDir, $realCacheDir, $enableOptionalWarmers = tr
167175
{
168176
// create a temporary kernel
169177
$realKernel = $this->getApplication()->getKernel();
170-
$realKernelClass = get_class($realKernel);
171-
$namespace = '';
172-
if (false !== $pos = strrpos($realKernelClass, '\\')) {
173-
$namespace = substr($realKernelClass, 0, $pos);
174-
$realKernelClass = substr($realKernelClass, $pos + 1);
175-
}
176-
$tempKernel = $this->getTempKernel($realKernel, $namespace, $realKernelClass, $warmupDir);
177-
$tempKernel->boot();
178+
if ($realKernel instanceof RebootableInterface) {
179+
$realKernel->reboot($warmupDir);
180+
$tempKernel = $realKernel;
181+
} else {
182+
$this->warning = 'Calling "cache:clear" with a kernel that does not implement "Symfony\Component\HttpKernel\RebootableInterface" is deprecated since version 3.4 and will be unsupported in 4.0.';
183+
$realKernelClass = get_class($realKernel);
184+
$namespace = '';
185+
if (false !== $pos = strrpos($realKernelClass, '\\')) {
186+
$namespace = substr($realKernelClass, 0, $pos);
187+
$realKernelClass = substr($realKernelClass, $pos + 1);
188+
}
189+
$tempKernel = $this->getTempKernel($realKernel, $namespace, $realKernelClass, $warmupDir);
190+
$tempKernel->boot();
178191

179-
$tempKernelReflection = new \ReflectionObject($tempKernel);
180-
$tempKernelFile = $tempKernelReflection->getFileName();
192+
$tempKernelReflection = new \ReflectionObject($tempKernel);
193+
$tempKernelFile = $tempKernelReflection->getFileName();
194+
}
181195

182196
// warmup temporary dir
183197
$warmer = $tempKernel->getContainer()->get('cache_warmer');
@@ -186,6 +200,20 @@ protected function warmup($warmupDir, $realCacheDir, $enableOptionalWarmers = tr
186200
}
187201
$warmer->warmUp($warmupDir);
188202

203+
// fix references to cached files with the real cache directory name
204+
$search = array($warmupDir, str_replace('\\', '\\\\', $warmupDir));
205+
$replace = str_replace('\\', '/', $realCacheDir);
206+
foreach (Finder::create()->files()->in($warmupDir) as $file) {
207+
$content = str_replace($search, $replace, file_get_contents($file), $count);
208+
if ($count) {
209+
file_put_contents($file, $content);
210+
}
211+
}
212+
213+
if ($realKernel instanceof RebootableInterface) {
214+
return;
215+
}
216+
189217
// fix references to the Kernel in .meta files
190218
$safeTempKernel = str_replace('\\', '\\\\', get_class($tempKernel));
191219
$realKernelFQN = get_class($realKernel);
@@ -198,16 +226,6 @@ protected function warmup($warmupDir, $realCacheDir, $enableOptionalWarmers = tr
198226
));
199227
}
200228

201-
// fix references to cached files with the real cache directory name
202-
$search = array($warmupDir, str_replace('\\', '\\\\', $warmupDir));
203-
$replace = str_replace('\\', '/', $realCacheDir);
204-
foreach (Finder::create()->files()->in($warmupDir) as $file) {
205-
$content = str_replace($search, $replace, file_get_contents($file), $count);
206-
if ($count) {
207-
file_put_contents($file, $content);
208-
}
209-
}
210-
211229
// fix references to container's class
212230
$tempContainerClass = $tempKernel->getContainerClass();
213231
$realContainerClass = $tempKernel->getRealContainerClass();

0 commit comments

Comments
 (0)