Skip to content

Commit 4c3b02c

Browse files
mnapolifabpot
authored andcommitted
[HttpKernel] Add $kernel->getBuildDir() to separate it from the cache directory
1 parent 94c1c83 commit 4c3b02c

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

Kernel.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public function getContainer()
314314
*/
315315
public function setAnnotatedClassCache(array $annotatedClasses)
316316
{
317-
file_put_contents(($this->warmupDir ?: $this->getCacheDir()).'/annotations.map', sprintf('<?php return %s;', var_export($annotatedClasses, true)));
317+
file_put_contents(($this->warmupDir ?: $this->getBuildDir()).'/annotations.map', sprintf('<?php return %s;', var_export($annotatedClasses, true)));
318318
}
319319

320320
/**
@@ -333,6 +333,15 @@ public function getCacheDir()
333333
return $this->getProjectDir().'/var/cache/'.$this->environment;
334334
}
335335

336+
/**
337+
* Gets the build directory.
338+
*/
339+
public function getBuildDir(): string
340+
{
341+
// Returns $this->getCacheDir() for backward compatibility
342+
return $this->getCacheDir();
343+
}
344+
336345
/**
337346
* {@inheritdoc}
338347
*/
@@ -419,14 +428,14 @@ protected function getContainerBaseClass()
419428
/**
420429
* Initializes the service container.
421430
*
422-
* The cached version of the service container is used when fresh, otherwise the
431+
* The built version of the service container is used when fresh, otherwise the
423432
* container is built.
424433
*/
425434
protected function initializeContainer()
426435
{
427436
$class = $this->getContainerClass();
428-
$cacheDir = $this->warmupDir ?: $this->getCacheDir();
429-
$cache = new ConfigCache($cacheDir.'/'.$class.'.php', $this->debug);
437+
$buildDir = $this->warmupDir ?: $this->getBuildDir();
438+
$cache = new ConfigCache($buildDir.'/'.$class.'.php', $this->debug);
430439
$cachePath = $cache->getPath();
431440

432441
// Silence E_WARNING to ignore "include" failures - don't use "@" to prevent silencing fatal errors
@@ -448,7 +457,7 @@ protected function initializeContainer()
448457
$oldContainer = \is_object($this->container) ? new \ReflectionClass($this->container) : $this->container = null;
449458

450459
try {
451-
is_dir($cacheDir) ?: mkdir($cacheDir, 0777, true);
460+
is_dir($buildDir) ?: mkdir($buildDir, 0777, true);
452461

453462
if ($lock = fopen($cachePath.'.lock', 'w')) {
454463
flock($lock, LOCK_EX | LOCK_NB, $wouldBlock);
@@ -533,8 +542,8 @@ protected function initializeContainer()
533542
if ($collectDeprecations) {
534543
restore_error_handler();
535544

536-
file_put_contents($cacheDir.'/'.$class.'Deprecations.log', serialize(array_values($collectedLogs)));
537-
file_put_contents($cacheDir.'/'.$class.'Compiler.log', null !== $container ? implode("\n", $container->getCompiler()->getLog()) : '');
545+
file_put_contents($buildDir.'/'.$class.'Deprecations.log', serialize(array_values($collectedLogs)));
546+
file_put_contents($buildDir.'/'.$class.'Compiler.log', null !== $container ? implode("\n", $container->getCompiler()->getLog()) : '');
538547
}
539548
}
540549

@@ -570,7 +579,7 @@ protected function initializeContainer()
570579
$preload = array_merge($preload, (array) $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir')));
571580
}
572581

573-
if ($preload && method_exists(Preloader::class, 'append') && file_exists($preloadFile = $cacheDir.'/'.$class.'.preload.php')) {
582+
if ($preload && method_exists(Preloader::class, 'append') && file_exists($preloadFile = $buildDir.'/'.$class.'.preload.php')) {
574583
Preloader::append($preloadFile, $preload);
575584
}
576585
}
@@ -597,7 +606,8 @@ protected function getKernelParameters()
597606
'kernel.project_dir' => realpath($this->getProjectDir()) ?: $this->getProjectDir(),
598607
'kernel.environment' => $this->environment,
599608
'kernel.debug' => $this->debug,
600-
'kernel.cache_dir' => realpath($cacheDir = $this->warmupDir ?: $this->getCacheDir()) ?: $cacheDir,
609+
'kernel.build_dir' => realpath($buildDir = $this->warmupDir ?: $this->getBuildDir()) ?: $buildDir,
610+
'kernel.cache_dir' => realpath($this->getCacheDir()) ?: $this->getCacheDir(),
601611
'kernel.logs_dir' => realpath($this->getLogDir()) ?: $this->getLogDir(),
602612
'kernel.bundles' => $bundles,
603613
'kernel.bundles_metadata' => $bundlesMetadata,
@@ -615,7 +625,7 @@ protected function getKernelParameters()
615625
*/
616626
protected function buildContainer()
617627
{
618-
foreach (['cache' => $this->warmupDir ?: $this->getCacheDir(), 'logs' => $this->getLogDir()] as $name => $dir) {
628+
foreach (['cache' => $this->getCacheDir(), 'build' => $this->warmupDir ?: $this->getBuildDir(), 'logs' => $this->getLogDir()] as $name => $dir) {
619629
if (!is_dir($dir)) {
620630
if (false === @mkdir($dir, 0777, true) && !is_dir($dir)) {
621631
throw new \RuntimeException(sprintf('Unable to create the "%s" directory (%s).', $name, $dir));

KernelInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
*
2121
* It manages an environment made of application kernel and bundles.
2222
*
23+
* @method string getBuildDir() Returns the build directory - not implementing it is deprecated since Symfony 5.2.
24+
* This directory should be used to store build artifacts, and can be read-only at runtime.
25+
* Caches written at runtime should be stored in the "cache directory" ({@see KernelInterface::getCacheDir()}).
26+
*
2327
* @author Fabien Potencier <fabien@symfony.com>
2428
*/
2529
interface KernelInterface extends HttpKernelInterface
@@ -121,6 +125,10 @@ public function getStartTime();
121125
/**
122126
* Gets the cache directory.
123127
*
128+
* Since Symfony 5.2, the cache directory should be used for caches that are written at runtime.
129+
* For caches and artifacts that can be warmed at compile-time and deployed as read-only,
130+
* use the new "build directory" returned by the {@see getBuildDir()} method.
131+
*
124132
* @return string The cache directory
125133
*/
126134
public function getCacheDir();

RebootableInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ interface RebootableInterface
2121
/**
2222
* Reboots a kernel.
2323
*
24-
* The getCacheDir() method of a rebootable kernel should not be called
25-
* while building the container. Use the %kernel.cache_dir% parameter instead.
24+
* The getBuildDir() method of a rebootable kernel should not be called
25+
* while building the container. Use the %kernel.build_dir% parameter instead.
2626
*
27-
* @param string|null $warmupDir pass null to reboot in the regular cache directory
27+
* @param string|null $warmupDir pass null to reboot in the regular build directory
2828
*/
2929
public function reboot(?string $warmupDir);
3030
}

0 commit comments

Comments
 (0)