Skip to content

Commit 3b10824

Browse files
feature #40804 [Config][FrameworkBundle] Add CacheWarmer for ConfigBuilder (Nyholm)
This PR was squashed before being merged into the 5.3-dev branch. Discussion ---------- [Config][FrameworkBundle] Add CacheWarmer for ConfigBuilder | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | | Tickets | | License | MIT | Doc PR | Make sure ConfigBuilder exists before you write your first line of config. This is similar to #40803 Commits ------- c1d6c0e [Config][FrameworkBundle] Add CacheWarmer for ConfigBuilder
2 parents 48ae511 + c1d6c0e commit 3b10824

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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 Psr\Log\LoggerInterface;
15+
use Symfony\Bundle\FrameworkBundle\Command\BuildDebugContainerTrait;
16+
use Symfony\Component\Config\Builder\ConfigBuilderGenerator;
17+
use Symfony\Component\Config\Builder\ConfigBuilderGeneratorInterface;
18+
use Symfony\Component\Config\Definition\ConfigurationInterface;
19+
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
20+
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
21+
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
22+
use Symfony\Component\HttpKernel\KernelInterface;
23+
24+
/**
25+
* Generate all config builders.
26+
*
27+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
28+
*/
29+
class ConfigBuilderCacheWarmer implements CacheWarmerInterface
30+
{
31+
use BuildDebugContainerTrait;
32+
33+
private $kernel;
34+
private $logger;
35+
36+
public function __construct(KernelInterface $kernel, LoggerInterface $logger = null)
37+
{
38+
$this->kernel = $kernel;
39+
$this->logger = $logger;
40+
}
41+
42+
/**
43+
* {@inheritdoc}
44+
*
45+
* @return string[]
46+
*/
47+
public function warmUp(string $cacheDir)
48+
{
49+
$generator = new ConfigBuilderGenerator($cacheDir);
50+
51+
foreach ($this->kernel->getBundles() as $bundle) {
52+
$extension = $bundle->getContainerExtension();
53+
if (null === $extension) {
54+
continue;
55+
}
56+
57+
try {
58+
$this->dumpExtension($extension, $generator);
59+
} catch (\Exception $e) {
60+
if ($this->logger) {
61+
$this->logger->warning('Failed to generate ConfigBuilder for extension {extensionClass}.', ['exception' => $e, 'extensionClass' => \get_class($extension)]);
62+
}
63+
}
64+
}
65+
66+
// No need to preload anything
67+
return [];
68+
}
69+
70+
private function dumpExtension(ExtensionInterface $extension, ConfigBuilderGeneratorInterface $generator): void
71+
{
72+
if ($extension instanceof ConfigurationInterface) {
73+
$configuration = $extension;
74+
} elseif ($extension instanceof ConfigurationExtensionInterface) {
75+
$configuration = $extension->getConfiguration([], $this->getContainerBuilder($this->kernel));
76+
} else {
77+
throw new \LogicException(sprintf('Could not get configuration for extension "%s".', \get_class($extension)));
78+
}
79+
80+
$generator->build($configuration);
81+
}
82+
83+
/**
84+
* {@inheritdoc}
85+
*/
86+
public function isOptional()
87+
{
88+
return true;
89+
}
90+
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/services.php

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
1313

14+
use Symfony\Bundle\FrameworkBundle\CacheWarmer\ConfigBuilderCacheWarmer;
1415
use Symfony\Bundle\FrameworkBundle\HttpCache\HttpCache;
1516
use Symfony\Component\Config\Resource\SelfCheckingResourceChecker;
1617
use Symfony\Component\Config\ResourceCheckerConfigCacheFactory;
@@ -208,5 +209,8 @@ class_exists(WorkflowEvents::class) ? WorkflowEvents::ALIASES : []
208209
->args([
209210
service('container.getenv'),
210211
])
212+
->set('config_builder.warmer', ConfigBuilderCacheWarmer::class)
213+
->args([service(KernelInterface::class), service('logger')->nullOnInvalid()])
214+
->tag('kernel.cache_warmer')
211215
;
212216
};

0 commit comments

Comments
 (0)