8000 [FrameworkBundle][HttpKernel] Introduce `$buildDir` argument to `Warm… · symfony/symfony@8b604ff · GitHub
[go: up one dir, main page]

Skip to content

Commit 8b604ff

Browse files
okhoshinicolas-grekas
authored andcommitted
[FrameworkBundle][HttpKernel] Introduce $buildDir argument to WarmableInterface::warmup to warm read-only artefacts in build_dir
1 parent 934aea0 commit 8b604ff

28 files changed

+136
-50
lines changed

.github/expected-missing-return-types.diff

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8109,11 +8109,11 @@ diff --git a/src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerInterface.p
81098109
diff --git a/src/Symfony/Component/HttpKernel/CacheWarmer/WarmableInterface.php b/src/Symfony/Component/HttpKernel/CacheWarmer/WarmableInterface.php
81108110
--- a/src/Symfony/Component/HttpKernel/CacheWarmer/WarmableInterface.php
81118111
+++ b/src/Symfony/Component/HttpKernel/CacheWarmer/WarmableInterface.php
8112-
@@ -24,4 +24,4 @@ interface WarmableInterface
8112+
@@ -27,4 +27,4 @@ interface WarmableInterface
81138113
* @return string[] A list of classes or files to preload on PHP 7.4+
81148114
*/
8115-
- public function warmUp(string $cacheDir);
8116-
+ public function warmUp(string $cacheDir): array;
8115+
- public function warmUp(string $cacheDir /* , string $buildDir = null */);
8116+
+ public function warmUp(string $cacheDir /* , string $buildDir = null */): array;
81178117
}
81188118
diff --git a/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php b/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php
81198119
--- a/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php

src/Symfony/Bridge/Doctrine/CacheWarmer/ProxyCacheWarmer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ public function isOptional(): bool
3838
}
3939

4040
/**
41-
* @return string[] A list of files to preload on PHP 7.4+
41+
* @param string|null $buildDir
4242
*/
43-
public function warmUp(string $cacheDir): array
43+
public function warmUp(string $cacheDir /* , string $buildDir = null */): array
4444
{
4545
$files = [];
4646
foreach ($this->registry->getManagers() as $em) {

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ CHANGELOG
3232
* Add `--exclude` option to the `cache:pool:clear` command
3333
* Add parameters deprecations to the output of `debug:container` command
3434
* Change `framework.asset_mapper.importmap_polyfill` from a URL to the name of an item in the importmap
35+
* Provide `$buildDir` when running `CacheWarmer` to build read-only resources
3536

3637
6.3
3738
---

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AbstractPhpFileCacheWarmer.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,16 @@ public function isOptional(): bool
3535
}
3636

3737
/**
38-
* @return string[] A list of classes to preload on PHP 7.4+
38+
* @param string|null $buildDir
3939
*/
40-
public function warmUp(string $cacheDir): array
40+
public function warmUp(string $cacheDir /* , string $buildDir = null */): array
4141
{
42+
$buildDir = 1 < \func_num_args() ? func_get_arg(1) : null;
4243
$arrayAdapter = new ArrayAdapter();
4344

4445
spl_autoload_register([ClassExistenceResource::class, 'throwOnRequiredClass']);
4546
try {
46-
if (!$this->doWarmUp($cacheDir, $arrayAdapter)) {
47+
if (!$this->doWarmUp($cacheDir, $arrayAdapter, $buildDir)) {
4748
return [];
4849
}
4950
} finally {
@@ -78,7 +79,9 @@ final protected function ignoreAutoloadException(string $class, \Exception $exce
7879
}
7980

8081
/**
82+
* @param string|null $buildDir
83+
*
8184
* @return bool false if there is nothing to warm-up
8285
*/
83-
abstract protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter): bool;
86+
abstract protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter /* , string $buildDir = null */): bool;
8487
}

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ public function __construct(
4444
parent::__construct($phpArrayFile);
4545
}
4646

47-
protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter): bool
47+
/**
48+
* @param string|null $buildDir
49+
*/
50+
protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter /* , string $buildDir = null */): bool
4851
{
4952
$annotatedClassPatterns = $cacheDir.'/annotations.map';
5053

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/CachePoolClearerCacheWarmer.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,7 @@ public function __construct(Psr6CacheClearer $poolClearer, array $pools = [])
3737
$this->pools = $pools;
3838
}
3939

40-
/**
41-
* @return string[]
42-
*/
43-
public function warmUp(string $cacheDirectory): array
40+
public function warmUp(string $cacheDir, string $buildDir = null): array
4441
{
4542
foreach ($this->pools as $pool) {
4643
if ($this->poolClearer->hasPool($pool)) {

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ConfigBuilderCacheWarmer.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,17 @@ public function __construct(KernelInterface $kernel, LoggerInterface $logger = n
3838
}
3939

4040
/**
41-
* @return string[]
41+
* @param string|null $buildDir
4242
*/
43-
public function warmUp(string $cacheDir): array
43+
public function warmUp(string $cacheDir /* , string $buildDir = null */): array
4444
{
45-
$generator = new ConfigBuilderGenerator($this->kernel->getBuildDir());
45+
$buildDir = 1 < \func_num_args() ? func_get_arg(1) : null;
46+
47+
if (!$buildDir) {
48+
return [];
49+
}
50+
51+
$generator = new ConfigBuilderGenerator($buildDir);
4652

4753
foreach ($this->kernel->getBundles() as $bundle) {
4854
$extension = $bundle->getContainerExtension();

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/RouterCacheWarmer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public function __construct(ContainerInterface $container)
3434
$this->container = $container;
3535
}
3636

37-
public function warmUp(string $cacheDir): array
37+
public function warmUp(string $cacheDir, string $buildDir = null): array
3838
{
3939
$router = $this->container->get('router');
4040

4141
if ($router instanceof WarmableInterface) {
42-
return (array) $router->warmUp($cacheDir);
42+
return (array) $router->warmUp($cacheDir, $buildDir);
4343
}
4444

4545
throw new \LogicException(sprintf('The router "%s" cannot be warmed up because it does not implement "%s".', get_debug_type($router), WarmableInterface::class));

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/SerializerCacheWarmer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ public function __construct(array $loaders, string $phpArrayFile)
3939
$this->loaders = $loaders;
4040
}
4141

42-
protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter): bool
42+
/**
43+
* @param string|null $buildDir
44+
*/
45+
protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter /* , string $buildDir = null */): bool
4346
{
4447
if (!$this->loaders) {
4548
return true;

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/TranslationsCacheWarmer.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ public function __construct(ContainerInterface $container)
3434
}
3535

3636
/**
37-
* @return string[]
37+
* @param string|null $buildDir
3838
*/
39-
public function warmUp(string $cacheDir): array
39+
public function warmUp(string $cacheDir /* , string $buildDir = null */): array
4040
{
4141
$this->translator ??= $this->container->get('translator');
4242

4343
if ($this->tr 10000 anslator instanceof WarmableInterface) {
44-
return (array) $this->translator->warmUp($cacheDir);
44+
$buildDir = 1 < \func_num_args() ? func_get_arg(1) : null;
45+
46+
return (array) $this->translator->warmUp($cacheDir, $buildDir);
4547
}
4648

4749
return [];

src/Symfony/Bundle/FrameworkBundle/CacheWarmer/ValidatorCacheWarmer.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ public function __construct(ValidatorBuilder $validatorBuilder, string $phpArray
3939
$this->validatorBuilder = $validatorBuilder;
4040
}
4141

42-
protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter): bool
42+
/**
43+
* @param string|null $buildDir
44+
*/
45+
protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter /* , string $buildDir = null */): bool
4346
{
4447
$loaders = $this->validatorBuilder->getLoaders();
4548
$metadataFactory = new LazyLoadingMetadataFactory(new LoaderChain($loaders), $arrayAdapter);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ private function warmupOptionals(string $cacheDir, string $warmupDir, SymfonySty
245245
$warmer = $kernel->getContainer()->get('cache_warmer');
246246
// non optional warmers already ran during container compilation
247247
$warmer->enableOnlyOptionalWarmers();
248-
$preload = (array) $warmer->warmUp($cacheDir, $io);
248+
$preload = (array) $warmer->warmUp($cacheDir, $warmupDir, $io);
249249

250250
if ($preload && file_exists($preloadFile = $warmupDir.'/'.$kernel->getContainer()->getParameter('kernel.container_class').'.preload.php')) {
251251
Preloader::append($preloadFile, $preload);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7474

7575
$preload = $this->cacheWarmer->warmUp($cacheDir);
7676

77-
if ($preload && file_exists($preloadFile = $cacheDir.'/'.$kernel->getContainer()->getParameter('kernel.container_class').'.preload.php')) {
77+
$buildDir = $kernel->getContainer()->getParameter('kernel.build_dir');
78+
if ($preload && $cacheDir === $buildDir && file_exists($preloadFile = $buildDir.'/'.$kernel->getContainer()->getParameter('kernel.container_class').'.preload.php')) {
7879
Preloader::append($preloadFile, $preload);
7980
}
8081

src/Symfony/Bundle/FrameworkBundle/Routing/Router.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ public function getRouteCollection(): RouteCollection
8181
}
8282

8383
/**
84-
* @return string[] A list of classes to preload on PHP 7.4+
84+
* @param string|null $buildDir
8585
*/
86-
public function warmUp(string $cacheDir): array
86+
public function warmUp(string $cacheDir /* , string $buildDir = null */): array
8787
{
8888
$currentDir = $this->getOption('cache_dir');
8989

src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/ConfigBuilderCacheWarmerTest.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
1616
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
1717
use Symfony\Component\Config\Loader\LoaderInterface;
18+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1819
use Symfony\Component\DependencyInjection\ContainerBuilder;
1920
use Symfony\Component\Filesystem\Filesystem;
2021
use Symfony\Component\HttpKernel\Kernel;
@@ -39,7 +40,7 @@ protected function tearDown(): void
3940

4041
public function testBuildDirIsUsedAsConfigBuilderOutputDir()
4142
{
42-
$kernel = new class($this->varDir) extends Kernel {
43+
$kernel = new class($this->varDir) extends Kernel implements CompilerPassInterface {
4344
private $varDir;
4445

4546
public function __construct(string $varDir)
@@ -75,12 +76,25 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
7576
]);
7677
});
7778
}
79+
80+
public function process(ContainerBuilder $container): void
81+
{
82+
$container->removeDefinition('config_builder.warmer');
83+
}
7884
};
7985
$kernel->boot();
8086

87+
self::assertDirectoryDoesNotExist($kernel->getBuildDir().'/Symfony');
88+
self::assertDirectoryDoesNotExist($kernel->getCacheDir().'/Symfony');
89+
8190
$warmer = new ConfigBuilderCacheWarmer($kernel);
8291
$warmer->warmUp($kernel->getCacheDir());
8392

93+
self::assertDirectoryDoesNotExist($kernel->getBuildDir().'/Symfony');
94+
self::assertDirectoryDoesNotExist($kernel->getCacheDir().'/Symfony');
95+
96+
$warmer->warmUp($kernel->getCacheDir(), $kernel->getBuildDir());
97+
8498
self::assertDirectoryExists($kernel->getBuildDir().'/Symfony');
8599
self::assertDirectoryDoesNotExist($kernel->getCacheDir().'/Symfony');
86100
}

src/Symfony/Bundle/FrameworkBundle/Tests/Command/CacheClearCommand/Fixture/TestAppKernel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function isOptional(): bool
5252
return false;
5353
}
5454

55-
public function warmUp(string $cacheDir): array
55+
public function warmUp(string $cacheDir, string $buildDir = null): array
5656
{
5757
file_put_contents($cacheDir.'/dummy.txt', 'Hello');
5858

src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ public function __construct(ContainerInterface $container, MessageFormatterInter
9595
}
9696

9797
/**
98-
* @return string[]
98+
* @param string|null $buildDir
9999
*/
100-
public function warmUp(string $cacheDir): array
100+
public function warmUp(string $cacheDir /* , string $buildDir = null */): array
101101
{
102102
// skip warmUp when translator doesn't use cache
103103
if (null === $this->options['cache_dir']) {

src/Symfony/Bundle/SecurityBundle/CacheWarmer/ExpressionCacheWarmer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ public function isOptional(): bool
3535
}
3636

3737
/**
38-
* @return string[]
38+
* @param string|null $buildDir
3939
*/
40-
public function warmUp(string $cacheDir): array
40+
public function warmUp(string $cacheDir /* , string $buildDir = null */): array
4141
{
4242
foreach ($this->expressions as $expression) {
4343
$this->expressionLanguage->parse($expression, ['token', 'user', 'object', 'subject', 'role_names', 'request', 'trust_resolver']);

src/Symfony/Bundle/TwigBundle/CacheWarmer/TemplateCacheWarmer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public function __construct(ContainerInterface $container, iterable $iterator)
3636
}
3737

3838
/**
39-
* @return string[] A list of template files to preload on PHP 7.4+
39+
* @param string|null $buildDir
4040
*/
41-
public function warmUp(string $cacheDir): array
41+
public function warmUp(string $cacheDir /* , string $buildDir = null */): array
4242
{
4343
$this->twig ??= $this->container->get('twig');
4444

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ CHANGELOG
1515
* Support the `!` character at the beginning of a string as a negation operator in the url filter of the profiler
1616
* Deprecate `UriSigner`, use `UriSigner` from the HttpFoundation component instead
1717
* Deprecate `FileLinkFormatter`, use `FileLinkFormatter` from the ErrorHandler component instead
18+
* Add argument `$buildDir` to `WarmableInterface`
1819

1920
6.3
2021
---

src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerAggregate.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,17 @@ public function enableOnlyOptionalWarmers(): void
4848
$this->onlyOptionalsEnabled = $this->optionalsEnabled = true;
4949
}
5050

51-
public function warmUp(string $cacheDir, SymfonyStyle $io = null): array
51+
/**
52+
* @param string|null $buildDir
53+
*/
54+
public function warmUp(string $cacheDir, string|SymfonyStyle $buildDir = null, SymfonyStyle $io = null): array
5255
{
56+
if ($buildDir instanceof SymfonyStyle) {
57+
trigger_deprecation('symfony/http-kernel', '6.4', 'Passing a "%s" as second argument of "%s()" is deprecated, pass it as third argument instead, after the build directory.', SymfonyStyle::class, __METHOD__);
58+
$io = $buildDir;
59+
$buildDir = null;
60+
}
61+
5362
if ($collectDeprecations = $this->debug && !\defined('PHPUNIT_COMPOSER_INSTALL')) {
5463
$collectedLogs = [];
5564
$previousHandler = set_error_handler(function ($type, $message, $file, $line) use (&$collectedLogs, &$previousHandler) {
@@ -96,8 +105,8 @@ public function warmUp(string $cacheDir, SymfonyStyle $io = null): array
96105
}
97106

98107
$start = microtime(true);
99-
foreach ((array) $warmer->warmUp($cacheDir) as $item) {
100-
if (is_dir($item) || (str_starts_with($item, \dirname($cacheDir)) && !is_file($item))) {
108+
foreach ((array) $warmer->warmUp($cacheDir, $buildDir) as $item) {
109+
if (is_dir($item) || (str_starts_with($item, \dirname($cacheDir)) && !is_file($item)) || ($buildDir && str_starts_with($item, \dirname($buildDir)) && !is_file($item))) {
101110
throw new \LogicException(sprintf('"%s::warmUp()" should return a list of files or classes but "%s" is none of them.', $warmer::class, $item));
102111
}
103112
$preload[] = $item;

src/Symfony/Component/HttpKernel/CacheWarmer/WarmableInterface.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ interface WarmableInterface
2121
/**
2222
* Warms up the cache.
2323
*
24+
* @param string $cacheDir Where warm-up artifacts should be stored
25+
* @param string|null $buildDir Where read-only artifacts should go; null when called after compile-time
26+
*
2427
* @return string[] A list of classes or files to preload on PHP 7.4+
2528
*/
26-
public function warmUp(string $cacheDir);
29+
public function warmUp(string $cacheDir /* , string $buildDir = null */);
2730
}

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,10 +539,10 @@ protected function initializeContainer()
539539
touch($oldContainerDir.'.legacy');
540540
}
541541

542-
$preload = $this instanceof WarmableInterface ? (array) $this->warmUp($this->container->getParameter('kernel.cache_dir')) : [];
542+
$preload = $this instanceof WarmableInterface ? (array) $this->warmUp($this->container->getParameter('kernel.cache_dir'), $buildDir) : [];
543543

544544
if ($this->container->has('cache_warmer')) {
545-
$preload = array_merge($preload, (array) $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir')));
545+
$preload = array_merge($preload, (array) $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'), $buildDir));
546546
}
547547

548548
if ($preload && file_exists($preloadFile = $buildDir.'/'.$class.'.preload.php')) {

0 commit comments

Comments
 (0)
0