8000 [FrameworkBundle][HttpKernel] Fix and un-deprecate `cache:clear[ --no… · symfony/symfony@7a2f817 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7a2f817

Browse files
[FrameworkBundle][HttpKernel] Fix and un-deprecate cache:clear[ --no-warmup] with new RebootableInterface
1 parent 719e8c1 commit 7a2f817

File tree

14 files changed

+195
-46
lines changed

14 files changed

+195
-46
lines changed

UPGRADE-3.4.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,40 @@ FrameworkBundle
114114
class has been deprecated and will be removed in 4.0. Use the
115115
`Symfony\Component\Translation\DependencyInjection\TranslatorPass` class instead.
116116

117+
HttpKernel
118+
----------
119+
120+
* The `getCacheDir()` method of your kernel should return the non-empty value
121+
from `Kernel::getWarmupDir()` when one exists; not doing so is deprecated.
122+
123+
Before:
124+
125+
```php
126+
class AppKernel extends Kernel
127+
{
128+
public function getCacheDir()
129+
{
130+
return 'my-cache-dir';
131+
}
132+
133+
// ...
134+
}
135+
```
136+
137+
After:
138+
139+
```php
140+
class AppKernel extends Kernel
141+
{
142+
public function getCacheDir()
143+
{
144+
return $this->getWarmupDir() ?: 'my-cache-dir';
145+
}
146+
147+
// ...
148+
}
149+
```
150+
117151
Process
118152
-------
119153

UPGRADE-4.0.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,37 @@ HttpKernel
487487
by Symfony. Use the `%env()%` syntax to get the value of any environment
488488
variable from configuration files instead.
489489

490+
* The `getCacheDir()` method of your kernel must return the non-empty value
491+
from `Kernel::getWarmupDir()` when one exists.
492+
493+
Before:
494+
495+
```php
496+
class AppKernel extends Kernel
497+
{
498+
public function getCacheDir()
499+
{
500+
return 'my-cache-dir';
501+
}
502+
503+
// ...
504+
}
505+
```
506+
507+
After:
508+
509+
```php
510+
class AppKernel extends Kernel
511+
{
512+
public function getCacheDir()
513+
{
514+
return $this->getWarmupDir() ?: 'my-cache-dir';
515+
}
516+
517+
// ...
518+
}
519+
```
520+
490521
Ldap
491522
----
492523

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

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Console\Style\SymfonyStyle;
1818
use Symfony\Component\EventDispatcher\EventDispatcher;
1919
use Symfony\Component\HttpKernel\KernelInterface;
20+
use Symfony\Component\HttpKernel\RebootableInterface;
2021
use Symfony\Component\Finder\Finder;
2122

2223
/**
@@ -27,6 +28,8 @@
2728
*/
2829
class CacheClearCommand extends ContainerAwareCommand
2930
{
31+
private $warning;
32+
3033
/**
3134
* {@inheritdoc}
3235
*/
@@ -78,11 +81,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
7881
if ($input->getOption('no-warmup')) {
7982
$filesystem->rename($realCacheDir, $oldCacheDir);
8083
} else {
81-
$warning = 'Calling cache:clear without the --no-warmup option is deprecated since version 3.3. Cache warmup should be done with the cache:warmup command instead.';
82-
83-
@trigger_error($warning, E_USER_DEPRECATED);
84-
85-
$io->warning($warning);
84+
if ($this->warning) {
85+
@trigger_error($this->warning, E_USER_DEPRECATED);
86+
$io->warning($this->warning);
87+
$this->warning = null;
88+
}
8689

8790
$this->warmupCache($input, $output, $realCacheDir, $oldCacheDir);
8891
}
@@ -136,24 +139,37 @@ private function warmupCache(InputInterface $input, OutputInterface $output, $re
136139
* @param string $warmupDir
137140
* @param string $realCacheDir
138141
* @param bool $enableOptionalWarmers
139-
*
140-
* @internal to be removed in 4.0
141142
*/
142143
protected function warmup($warmupDir, $realCacheDir, $enableOptionalWarmers = true)
143144
{
144145
// create a temporary kernel
145146
$realKernel = $this->getContainer()->get('kernel');
146-
$realKernelClass = get_class($realKernel);
147-
$namespace = '';
148-
if (false !== $pos = strrpos($realKernelClass, '\\')) {
149-
$namespace = substr($realKernelClass, 0, $pos);
150-
$realKernelClass = substr($realKernelClass, $pos + 1);
147+
if (!$isRebootable = $realKernel instanceof RebootableInterface) {
148+
$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.';
149+
} else {
150+
$realKernel->reboot($warmupDir);
151+
152+
if (!$isRebootable = $realKernel->getCacheDir() === $realCacheDir) {
153+
$this->warning = 'Calling "cache:clear" with a kernel that ignores the warm-up directory is deprecated since version 3.4 and will be unsupported in 4.0. The "getCacheDir()" method of your kernel should return the non-empty value from "Kernel::getWarmupDir()" when one exists.';
154+
155+
$realKernel->reboot($realCacheDir);
156+
} else {
157+
$tempKernel = $realKernel;
158+
}
151159
}
152-
$tempKernel = $this->getTempKernel($realKernel, $namespace, $realKernelClass, $warmupDir);
153-
$tempKernel->boot();
160+
if (!$isRebootable) {
161+
$realKernelClass = get_class($realKernel);
162+
$namespace = '';
163+
if (false !== $pos = strrpos($realKernelClass, '\\')) {
164+
$namespace = substr($realKernelClass, 0, $pos);
165+
$realKernelClass = substr($realKernelClass, $pos + 1);
166+
}
167+
$tempKernel = $this->getTempKernel($realKernel, $namespace, $realKernelClass, $warmupDir);
168+
$tempKernel->boot();
154169

155-
$tempKernelReflection = new \ReflectionObject($tempKernel);
156-
$tempKernelFile = $tempKernelReflection->getFileName();
170+
$tempKernelReflection = new \ReflectionObject($tempKernel);
171+
$tempKernelFile = $tempKernelReflection->getFileName();
172+
}
157173

158174
// warmup temporary dir
159175
$warmer = $tempKernel->getContainer()->get('cache_warmer');
@@ -162,6 +178,20 @@ protected function warmup($warmupDir, $realCacheDir, $enableOptionalWarmers = tr
162178
}
163179
$warmer->warmUp($warmupDir);
164180

181+
// fix references to cached files with the real cache directory name
182+
$search = array($warmupDir, str_replace('\\', '\\\\', $warmupDir));
183+
$replace = str_replace('\\', '/', $realCacheDir);
184+
foreach (Finder::create()->files()->in($warmupDir) as $file) {
185+
$content = str_replace($search, $replace, file_get_contents($file), $count);
186+
if ($count) {
187+
file_put_contents($file, $content);
188+
}
189+
}
190+
191+
if ($isRebootable) {
192+
return;
193+
}
194+
165195
// fix references to the Kernel in .meta files
166196
$safeTempKernel = str_replace('\\', '\\\\', get_class($tempKernel));
167197
$realKernelFQN = get_class($realKernel);
@@ -174,16 +204,6 @@ protected function warmup($warmupDir, $realCacheDir, $enableOptionalWarmers = tr
174204
));
175205
}
176206

177-
// fix references to cached files with the real cache directory name
178-
$search = array($warmupDir, str_replace('\\', '\\\\', $warmupDir));
179-
$replace = str_replace('\\', '/', $realCacheDir);
180-
foreach (Finder::create()->files()->in($warmupDir) as $file) {
181-
$content = str_replace($search, $replace, file_get_contents($file), $count);
182-
if ($count) {
183-
file_put_contents($file, $content);
184-
}
185-
}
186-
187207
// fix references to container's class
188208
$tempContainerClass = $tempKernel->getContainerClass();
189209
$realContainerClass = $tempKernel->getRealContainerClass();
@@ -247,7 +267,7 @@ class $class extends $parentClass
247267
{
248268
public function getCacheDir()
249269
{
250-
return $cacheDir;
270+
return \$this->getWarmupDir() ?: $cacheDir;
251271
}
252272
253273
public function getRootDir()

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/AppKernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function getRootDir()
5959

6060
public function getCacheDir()
6161
{
62-
return sys_get_temp_dir().'/'.$this->varDir.'/'.$this->testCase.'/cache/'.$this->environment;
62+
return $this->getWarmupDir() ?: sys_get_temp_dir().'/'.$this->varDir.'/'.$this->testCase.'/cache/'.$this->environment;
6363
}
6464

6565
public function getLogDir()

src/Symfony/Bundle/FrameworkBundle/Tests/Kernel/ConcreteMicroKernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function registerBundles()
4040

4141
public function getCacheDir()
4242
{
43-
return $this->cacheDir = sys_get_temp_dir().'/sf_micro_kernel';
43+
return $this->getWarmupDir() ?: $this->cacheDir = sys_get_temp_dir().'/sf_micro_kernel';
4444
}
4545

4646
public function getLogDir()

src/Symfony/Bundle/SecurityBundle/Tests/Functional/app/AppKernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function getRootDir()
7171

7272
public function getCacheDir()
7373
{
74-
return sys_get_temp_dir().'/'.$this->varDir.'/'.$this->testCase.'/cache/'.$this->environment;
74+
return $this->getWarmupDir() ?: sys_get_temp_dir().'/'.$this->varDir.'/'.$this->testCase.'/cache/'.$this->environment;
7575
}
7676

7777
public function getLogDir()

src/Symfony/Bundle/SecurityBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"ext-xml": "*",
2121
"symfony/security": "~3.4|~4.0",
2222
"symfony/dependency-injection": "~3.4|~4.0",
23-
"symfony/http-kernel": "~3.3|~4.0",
23+
"symfony/http-kernel": "~3.4|~4.0",
2424
"symfony/polyfill-php70": "~1.0"
2525
},
2626
"require-dev": {

src/Symfony/Bundle/TwigBundle/Tests/Functional/CacheWarmingTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function registerContainerConfiguration(LoaderInterface $loader)
108108

109109
public function getCacheDir()
110110
{
111-
return sys_get_temp_dir().'/'.Kernel::VERSION.'/CacheWarmingKernel/cache/'.$this->environment;
111+
return $this->getWarmupDir() ?: sys_get_temp_dir().'/'.Kernel::VERSION.'/CacheWarmingKernel/cache/'.$this->environment;
112112
}
113113

114114
public function getLogDir()

src/Symfony/Bundle/TwigBundle/Tests/Functional/NoTemplatingEntryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function registerContainerConfiguration(LoaderInterface $loader)
6969

7070
public function getCacheDir()
7171
{
72-
return sys_get_temp_dir().'/'.Kernel::VERSION.'/NoTemplatingEntryKernel/cache/'.$this->environment;
72+
return $this->getWarmupDir() ?: sys_get_temp_dir().'/'.Kernel::VERSION.'/NoTemplatingEntryKernel/cache/'.$this->environment;
7373
}
7474

7575
public function getLogDir()

src/Symfony/Bundle/TwigBundle/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"symfony/config": "~3.2",
2121
"symfony/twig-bridge": "^3.3",
2222
"symfony/http-foundation": "~2.8|~3.0",
23-
"symfony/http-kernel": "^3.3",
23+
"symfony/http-kernel": "^3.4",
2424
"twig/twig": "~1.34|~2.4"
2525
},
2626
"require-dev": {

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG
44
3.4.0
55
-----
66

7+
* the `getCacheDir()` method of your kernel should return the non-empty value
8+
from `Kernel::getWarmupDir()` when one exists; not doing so is deprecated
79
* added `AddCacheClearerPass`
810
* added `AddCacheWarmerPass`
911

0 commit comments

Comments
 (0)
0