Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FrameworkBundle][HttpKernel] Introduce $buildDir argument to WarmableInterface::warmup to warm read-only artefacts in build_dir #50391

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/expected-missing-return-types.diff
Original file line number Diff line number Diff line change
Expand Up @@ -8109,11 +8109,11 @@ diff --git a/src/Symfony/Component/HttpKernel/CacheWarmer/CacheWarmerInterface.p
diff --git a/src/Symfony/Component/HttpKernel/CacheWarmer/WarmableInterface.php b/src/Symfony/Component/HttpKernel/CacheWarmer/WarmableInterface.php
--- a/src/Symfony/Component/HttpKernel/CacheWarmer/WarmableInterface.php
+++ b/src/Symfony/Component/HttpKernel/CacheWarmer/WarmableInterface.php
@@ -24,4 +24,4 @@ interface WarmableInterface
@@ -27,4 +27,4 @@ interface WarmableInterface
* @return string[] A list of classes or files to preload on PHP 7.4+
*/
- public function warmUp(string $cacheDir);
+ public function warmUp(string $cacheDir): array;
- public function warmUp(string $cacheDir /* , string $buildDir = null */);
+ public function warmUp(string $cacheDir /* , string $buildDir = null */): array;
}
diff --git a/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php b/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php
--- a/src/Symfony/Component/HttpKernel/DataCollector/DataCollector.php
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bridge/Doctrine/CacheWarmer/ProxyCacheWarmer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public function isOptional(): bool
}

/**
* @return string[] A list of files to preload on PHP 7.4+
* @param string|null $buildDir
*/
public function warmUp(string $cacheDir): array
public function warmUp(string $cacheDir /* , string $buildDir = null */): array
{
$files = [];
foreach ($this->registry->getManagers() as $em) {
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ CHANGELOG
* Add `--exclude` option to the `cache:pool:clear` command
* Add parameters deprecations to the output of `debug:container` command
* Change `framework.asset_mapper.importmap_polyfill` from a URL to the name of an item in the importmap
* Provide `$buildDir` when running `CacheWarmer` to build read-only resources

6.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ public function isOptional(): bool
}

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

spl_autoload_register([ClassExistenceResource::class, 'throwOnRequiredClass']);
try {
if (!$this->doWarmUp($cacheDir, $arrayAdapter)) {
if (!$this->doWarmUp($cacheDir, $arrayAdapter, $buildDir)) {
return [];
}
} finally {
Expand Down Expand Up @@ -78,7 +79,9 @@ final protected function ignoreAutoloadException(string $class, \Exception $exce
}

/**
* @param string|null $buildDir
*
* @return bool false if there is nothing to warm-up
*/
abstract protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter): bool;
abstract protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter /* , string $buildDir = null */): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ public function __construct(
parent::__construct($phpArrayFile);
}

protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter): bool
/**
* @param string|null $buildDir
*/
protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter /* , string $buildDir = null */): bool
{
$annotatedClassPatterns = $cacheDir.'/annotations.map';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ public function __construct(Psr6CacheClearer $poolClearer, array $pools = [])
$this->pools = $pools;
}

/**
* @return string[]
*/
public function warmUp(string $cacheDirectory): array
public function warmUp(string $cacheDir, string $buildDir = null): array
{
foreach ($this->pools as $pool) {
if ($this->poolClearer->hasPool($pool)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@ public function __construct(KernelInterface $kernel, LoggerInterface $logger = n
}

/**
* @return string[]
* @param string|null $buildDir
*/
public function warmUp(string $cacheDir): array
public function warmUp(string $cacheDir /* , string $buildDir = null */): array
{
$generator = new ConfigBuilderGenerator($this->kernel->getBuildDir());
$buildDir = 1 < \func_num_args() ? func_get_arg(1) : null;

if (!$buildDir) {
return [];
}

$generator = new ConfigBuilderGenerator($buildDir);

foreach ($this->kernel->getBundles() as $bundle) {
$extension = $bundle->getContainerExtension();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public function __construct(ContainerInterface $container)
$this->container = $container;
}

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

if ($router instanceof WarmableInterface) {
return (array) $router->warmUp($cacheDir);
return (array) $router->warmUp($cacheDir, $buildDir);
}

throw new \LogicException(sprintf('The router "%s" cannot be warmed up because it does not implement "%s".', get_debug_type($router), WarmableInterface::class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public function __construct(array $loaders, string $phpArrayFile)
$this->loaders = $loaders;
}

protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter): bool
/**
* @param string|null $buildDir
*/
protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter /* , string $buildDir = null */): bool
{
if (!$this->loaders) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ public function __construct(ContainerInterface $container)
}

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

if ($this->translator instanceof WarmableInterface) {
return (array) $this->translator->warmUp($cacheDir);
$buildDir = 1 < \func_num_args() ? func_get_arg(1) : null;

return (array) $this->translator->warmUp($cacheDir, $buildDir);
}

return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public function __construct(ValidatorBuilder $validatorBuilder, string $phpArray
$this->validatorBuilder = $validatorBuilder;
}

protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter): bool
/**
* @param string|null $buildDir
*/
protected function doWarmUp(string $cacheDir, ArrayAdapter $arrayAdapter /* , string $buildDir = null */): bool
{
$loaders = $this->validatorBuilder->getLoaders();
$metadataFactory = new LazyLoadingMetadataFactory(new LoaderChain($loaders), $arrayAdapter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ private function warmupOptionals(string $cacheDir, string $warmupDir, SymfonySty
$warmer = $kernel->getContainer()->get('cache_warmer');
// non optional warmers already ran during container compilation
$warmer->enableOnlyOptionalWarmers();
$preload = (array) $warmer->warmUp($cacheDir, $io);
$preload = (array) $warmer->warmUp($cacheDir, $warmupDir, $io);

if ($preload && file_exists($preloadFile = $warmupDir.'/'.$kernel->getContainer()->getParameter('kernel.container_class').'.preload.php')) {
Preloader::append($preloadFile, $preload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int

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

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

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ public function getRouteCollection(): RouteCollection
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Kernel;
Expand All @@ -39,7 +40,7 @@ protected function tearDown(): void

public function testBuildDirIsUsedAsConfigBuilderOutputDir()
{
$kernel = new class($this->varDir) extends Kernel {
$kernel = new class($this->varDir) extends Kernel implements CompilerPassInterface {
private $varDir;

public function __construct(string $varDir)
Expand Down Expand Up @@ -75,12 +76,25 @@ public function registerContainerConfiguration(LoaderInterface $loader): void
]);
});
}

public function process(ContainerBuilder $container): void
{
$container->removeDefinition('config_builder.warmer');
}
};
$kernel->boot();

self::assertDirectoryDoesNotExist($kernel->getBuildDir().'/Symfony');
self::assertDirectoryDoesNotExist($kernel->getCacheDir().'/Symfony');

$warmer = new ConfigBuilderCacheWarmer($kernel);
$warmer->warmUp($kernel->getCacheDir());

self::assertDirectoryDoesNotExist($kernel->getBuildDir().'/Symfony');
self::assertDirectoryDoesNotExist($kernel->getCacheDir().'/Symfony');

$warmer->warmUp($kernel->getCacheDir(), $kernel->getBuildDir());

self::assertDirectoryExists($kernel->getBuildDir().'/Symfony');
self::assertDirectoryDoesNotExist($kernel->getCacheDir().'/Symfony');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function isOptional(): bool
return false;
}

public function warmUp(string $cacheDir): array
public function warmUp(string $cacheDir, string $buildDir = null): array
{
file_put_contents($cacheDir.'/dummy.txt', 'Hello');

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bundle/FrameworkBundle/Translation/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ public function __construct(ContainerInterface $container, MessageFormatterInter
}

/**
* @return string[]
* @param string|null $buildDir
*/
public function warmUp(string $cacheDir): array
public function warmUp(string $cacheDir /* , string $buildDir = null */): array
{
// skip warmUp when translator doesn't use cache
if (null === $this->options['cache_dir']) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public function isOptional(): bool
}

/**
* @return string[]
* @param string|null $buildDir
*/
public function warmUp(string $cacheDir): array
public function warmUp(string $cacheDir /* , string $buildDir = null */): array
{
foreach ($this->expressions as $expression) {
$this->expressionLanguage->parse($expression, ['token', 'user', 'object', 'subject', 'role_names', 'request', 'trust_resolver']);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public function __construct(ContainerInterface $container, iterable $iterator)
}

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

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ CHANGELOG
* Support the `!` character at the beginning of a string as a negation operator in the url filter of the profiler
* Deprecate `UriSigner`, use `UriSigner` from the HttpFoundation component instead
* Deprecate `FileLinkFormatter`, use `FileLinkFormatter` from the ErrorHandler component instead
* Add argument `$buildDir` to `WarmableInterface`

6.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,17 @@ public function enableOnlyOptionalWarmers(): void
$this->onlyOptionalsEnabled = $this->optionalsEnabled = true;
}

public function warmUp(string $cacheDir, SymfonyStyle $io = null): array
/**
* @param string|null $buildDir
*/
public function warmUp(string $cacheDir, string|SymfonyStyle $buildDir = null, SymfonyStyle $io = null): array
{
if ($buildDir instanceof SymfonyStyle) {
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__);
$io = $buildDir;
$buildDir = null;
}

if ($collectDeprecations = $this->debug && !\defined('PHPUNIT_COMPOSER_INSTALL')) {
$collectedLogs = [];
$previousHandler = set_error_handler(function ($type, $message, $file, $line) use (&$collectedLogs, &$previousHandler) {
Expand Down Expand Up @@ -96,8 +105,8 @@ public function warmUp(string $cacheDir, SymfonyStyle $io = null): array
}

$start = microtime(true);
foreach ((array) $warmer->warmUp($cacheDir) as $item) {
if (is_dir($item) || (str_starts_with($item, \dirname($cacheDir)) && !is_file($item))) {
foreach ((array) $warmer->warmUp($cacheDir, $buildDir) as $item) {
if (is_dir($item) || (str_starts_with($item, \dirname($cacheDir)) && !is_file($item)) || ($buildDir && str_starts_with($item, \dirname($buildDir)) && !is_file($item))) {
throw new \LogicException(sprintf('"%s::warmUp()" should return a list of files or classes but "%s" is none of them.', $warmer::class, $item));
}
$preload[] = $item;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ interface WarmableInterface
/**
* Warms up the cache.
*
* @param string $cacheDir Where warm-up artifacts should be stored
* @param string|null $buildDir Where read-only artifacts should go; null when called after compile-time
*
* @return string[] A list of classes or files to preload on PHP 7.4+
*/
public function warmUp(string $cacheDir);
public function warmUp(string $cacheDir /* , string $buildDir = null */);
}
4 changes: 2 additions & 2 deletions src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,10 +539,10 @@ protected function initializeContainer()
touch($oldContainerDir.'.legacy');
}

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

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

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