8000 [HttpKernel] Add `$kernel->getBuildDir()` to separate it from the cac… · GromNaN/symfony@ec945f1 · GitHub
[go: up one dir, main page]

Skip to content

Commit ec945f1

Browse files
mnapolifabpot
authored andcommitted
[HttpKernel] Add $kernel->getBuildDir() to separate it from the cache directory
1 parent 27d84db commit ec945f1

File tree

10 files changed

+67
-17
lines changed

10 files changed

+67
-17
lines changed

src/Symfony/Bridge/Doctrine/Tests/DependencyInjection/DoctrineExtensionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ protected function createContainer(array $data = []): ContainerBuilder
263263
return new ContainerBuilder(new ParameterBag(array_merge([
264264
'kernel.bundles' => ['FrameworkBundle' => 'Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle'],
265265
'kernel.cache_dir' => __DIR__,
266+
'kernel.build_dir' => __DIR__,
266267
'kernel.container_class' => 'kernel',
267268
'kernel.project_dir' => __DIR__,
268269
], $data)));

src/Symfony/Bundle/DebugBundle/Tests/DependencyInjection/DebugExtensionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ private function createContainer()
114114
{
115115
$container = new ContainerBuilder(new ParameterBag([
116116
'kernel.cache_dir' => __DIR__,
117+
'kernel.build_dir' => __DIR__,
117118
'kernel.charset' => 'UTF-8',
118119
'kernel.debug' => true,
119120
'kernel.project_dir' => __DIR__,

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6161
/** @var KernelInterface $kernel */
6262
$kernel = $this->getApplication()->getKernel();
6363

64+
if (method_exists($kernel, 'getBuildDir')) {
65+
$buildDir = $kernel->getBuildDir();
66+
} else {
67+
$buildDir = $kernel->getCacheDir();
68+
}
69+
6470
$rows = [
6571
['<info>Symfony</>'],
6672
new TableSeparator(),
@@ -76,6 +82,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7682
['Debug', $kernel->isDebug() ? 'true' : 'false'],
7783
['Charset', $kernel->getCharset()],
7884
['Cache directory', self::formatPath($kernel->getCacheDir(), $kernel->getProjectDir()).' (<comment>'.self::formatFileSize($kernel->getCacheDir()).'</>)'],
85+
['Build directory', self::formatPath($buildDir, $kernel->getProjectDir()).' (<comment>'.self::formatFileSize($buildDir).'</>)'],
7986
['Log directory', self::formatPath($kernel->getLogDir(), $kernel->getProjectDir()).' (<comment>'.self::formatFileSize($kernel->getLogDir()).'</>)'],
8087
new TableSeparator(),
8188
['<info>PHP</>'],

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
7979
$io = new SymfonyStyle($input, $output);
8080

8181
$kernel = $this->getApplication()->getKernel();
82+
$realBuildDir = $kernel->getContainer()->getParameter('kernel.build_dir');
8283
$realCacheDir = $kernel->getContainer()->getParameter('kernel.cache_dir');
8384
// the old cache dir name must not be longer than the real one to avoid exceeding
8485
// the maximum length of a directory or file path within it (esp. Windows MAX_PATH)
86+
$oldBuildDir = substr($realBuildDir, 0, -1).('~' === substr($realBuildDir, -1) ? '+' : '~');
8587
$oldCacheDir = substr($realCacheDir, 0, -1).('~' === substr($realCacheDir, -1) ? '+' : '~');
86-
$fs->remove($oldCacheDir);
88+
$fs->remove([$oldBuildDir, $oldCacheDir]);
8789

90+
if (!is_writable($realBuildDir)) {
91+
throw new RuntimeException(sprintf('Unable to write in the "%s" directory.', $realBuildDir));
92+
}
8893
if (!is_writable($realCacheDir)) {
8994
throw new RuntimeException(sprintf('Unable to write in the "%s" directory.', $realCacheDir));
9095
}
9196

9297
$io->comment(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
98+
$this->cacheClearer->clear($realBuildDir);
9399
$this->cacheClearer->clear($realCacheDir);
94100

95101
// The current event dispatcher is stale, let's not use it anymore
@@ -155,17 +161,31 @@ protected function execute(InputInterface $input, OutputInterface $output): int
155161
}
156162
}
157163

164+
if ($oldBuildDir) {
165+
$fs->rename($realBuildDir, $oldBuildDir);
166+
} else {
167+
$fs->remove($realBuildDir);
168+
}
158169
if ($oldCacheDir) {
159170
$fs->rename($realCacheDir, $oldCacheDir);
160171
} else {
161172
$fs->remove($realCacheDir);
162173
}
163174
$fs->rename($warmupDir, $realCacheDir);
175+
// Copy the content of the warmed cache in the build dir
176+
$fs->copy($realCacheDir, $realBuildDir);
164177

165178
if ($output->isVerbose()) {
166-
$io->comment('Removing old cache directory...');
179+
$io->comment('Removing old build and cache directory...');
167180
}
168181

182+
try {
183+
$fs->remove($oldBuildDir);
184+
} catch (IOException $e) {
185+
if ($output->isVerbose()) {
186+
$io->warning($e->getMessage());
187+
}
188+
}
169189
try {
170190
$fs->remove($oldCacheDir);
171191
} catch (IOException $e) {
@@ -184,7 +204,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
184204
return 0;
185205
}
186206

187-
private function warmup(string $warmupDir, string $realCacheDir, bool $enableOptionalWarmers = true)
207+
private function warmup(string $warmupDir, string $realBuildDir, bool $enableOptionalWarmers = true)
188208
{
189209
// create a temporary kernel
190210
$kernel = $this->getApplication()->getKernel();
@@ -207,7 +227,7 @@ private function warmup(string $warmupDir, string $realCacheDir, bool $enableOpt
207227

208228
// fix references to cached files with the real cache directory name
209229
$search = [$warmupDir, str_replace('\\', '\\\\', $warmupDir)];
210-
$replace = str_replace('\\', '/', $realCacheDir);
230+
$replace = str_replace('\\', '/', $realBuildDir);
211231
foreach (Finder::create()->files()->in($warmupDir) as $file) {
212232
$content = str_replace($search, $replace, file_get_contents($file), $count);
213233
if ($count) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,6 +1561,7 @@ protected function createContainer(array $data = [])
15611561
'kernel.bundles' => ['FrameworkBundle' => 'Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle'],
15621562
'kernel.bundles_metadata' => ['FrameworkBundle' => ['namespace' => 'Symfony\\Bundle\\FrameworkBundle', 'path' => __DIR__.'/../..']],
15631563
'kernel.cache_dir' => __DIR__,
1564+
'kernel.build_dir' => __DIR__,
15641565
'kernel.project_dir' => __DIR__,
15651566
'kernel.debug' => false,
15661567
'kernel.environment' => 'test',

src/Symfony/Bundle/SecurityBundle/Tests/DependencyInjection/Compiler/AddSessionDomainConstraintPassTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ private function createContainer($sessionStorageOptions)
125125
$container = new ContainerBuilder();
126126
$container->setParameter('kernel.bundles_metadata', []);
127127
$container->setParameter('kernel.cache_dir', __DIR__);
128+
$container->setParameter('kernel.build_dir', __DIR__);
128129
$container->setParameter('kernel.charset', 'UTF-8');
129130
$container->setParameter('kernel.container_class', 'cc');
130131
$container->setParameter('kernel.debug', true);

src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ protected function setUp(): void
6262
$this->container->register('twig', 'Twig\Environment')->addArgument(new Reference('twig_loader'))->setPublic(true);
6363
$this->container->setParameter('kernel.bundles', []);
6464
$this->container->setParameter('kernel.cache_dir', __DIR__);
65+
$this->container->setParameter('kernel.build_dir', __DIR__);
6566
$this->container->setParameter('kernel.debug', false);
6667
$this->container->setParameter('kernel.project_dir', __DIR__);
6768
$this->container->setParameter('kernel.charset', 'UTF-8');

src/Symfony/Component/HttpKernel/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));

src/Symfony/Component/HttpKernel/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();
121< D162 /code>125
/**
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();

src/Symfony/Component/HttpKernel/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)
0