8000 [FrameworkBundle] cache ClassMetadataFactory in debug · symfony/symfony@536338a · GitHub
[go: up one dir, main page]

Skip to content

Commit 536338a

Browse files
committed
[FrameworkBundle] cache ClassMetadataFactory in debug
We already track modification in serialization/validator config directory so we just need to clear the cache at warmup. Idea taken from apip: https://github.com/api-platform/core/blob/master/src/Bridge/Symfony/Bundle/CacheWarmer/CachePoolClearerCacheWarmer.php
1 parent 4e5b153 commit 536338a

File tree

4 files changed

+70
-6
lines changed

4 files changed

+70
-6
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
13+
14+
use Symfony\Component\HttpKernel\CacheClearer\Psr6CacheClearer;
15+
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
16+
17+
/**
18+
* Clears the cache pools when warming up the cache.
19+
*
20+
* Do not use in production!
21+
*
22+
* @author Kévin Dunglas <dunglas@gmail.com>
23+
*
24+
* @internal
25+
*/
26+
final class CachePoolClearerCacheWarmer implements CacheWarmerInterface
27+
{
28+
private $poolClearer;
29+
private $pools;
30+
31+
public function __construct(Psr6CacheClearer $poolClearer, array $pools = [])
32+
{
33+
$this->poolClearer = $poolClearer;
34+
$this->pools = $pools;
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function warmUp($cacheDirectory): void
41+
{
42+
foreach ($this->pools as $pool) {
43+
if ($this->poolClearer->hasPool($pool)) {
44+
$this->poolClearer->clearPool($pool);
45+
}
46+
}
47+
}
48+
49+
/**
50+
* {@inheritdoc}
51+
*/
52+
public function isOptional(): bool
53+
{
54+
// optional cache warmers are not run when handling the request
55+
return false;
56+
}
57+
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,10 +1457,6 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
14571457
$chainLoader->replaceArgument(0, $serializerLoaders);
14581458
$container->getDefinition('serializer.mapping.cache_warmer')->replaceArgument(0, $serializerLoaders);
14591459

1460-
if ($container->getParameter('kernel.debug')) {
1461-
$container->removeDefinition('serializer.mapping.cache_class_metadata_factory');
1462-
}
1463-
14641460
if (isset($config['name_converter']) && $config['name_converter']) {
14651461
$container->getDefinition('serializer.name_converter.metadata_aware')->setArgument(1, new Reference($config['name_converter']));
14661462
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/cache_debug.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,15 @@
1111
<service id="data_collector.cache" class="Symfony\Component\Cache\DataCollector\CacheDataCollector" public="true">
1212
<tag name="data_collector" template="@WebProfiler/Collector/cache.html.twig" id="cache" priority="275" />
1313
</service>
14+
15+
<!-- CacheWarmer used in dev to clear cache pool -->
16+
<service id="cache_pool_clearer.cache_warmer" class="Symfony\Bundle\FrameworkBundle\CacheWarmer\CachePoolClearerCacheWarmer" public="false">
17+
<argument type="service" id="cache.system_clearer" />
18+
<argument type="collection">
19+
<argument>cache.validator</argument>
20+
<argument>cache.serializer</argument>
21+
</argument>
22+
<tag name="kernel.cache_warmer" priority="64" />
23+
</service>
1424
</services>
1525
</container>

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,10 +1102,11 @@ public function testSerializerCacheActivated()
11021102
$this->assertEquals(new Reference('serializer.mapping.cache.symfony'), $cache);
11031103
}
11041104

1105-
public function testSerializerCacheDisabled()
1105+
public function testCachePoolClearerCacheWarmer()
11061106
{
11071107
$container = $this->createContainerFromFile('serializer_enabled', ['kernel.debug' => true, 'kernel.container_class' => __CLASS__]);
1108-
$this->assertFalse($container->hasDefinition('serializer.mapping.cache_class_metadata_factory'));
1108+
$this->assertTrue($container->hasDefinition('serializer.mapping.cache_class_metadata_factory'));
1109+
$this->assertTrue($container->hasDefinition('cache_pool_clearer.cache_warmer'));
11091110
}
11101111

11111112
public function testSerializerMapping()

0 commit comments

Comments
 (0)
0