-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[HttpKernel] Add $kernel->getBuildDir()
to separate it from the cache directory
#36515
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,17 +79,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int | |
$io = new SymfonyStyle($input, $output); | ||
|
||
$kernel = $this->getApplication()->getKernel(); | ||
$realBuildDir = $kernel->getContainer()->getParameter('kernel.build_dir'); | ||
$realCacheDir = $kernel->getContainer()->getParameter('kernel.cache_dir'); | ||
// the old cache dir name must not be longer than the real one to avoid exceeding | ||
// the maximum length of a directory or file path within it (esp. Windows MAX_PATH) | ||
$oldBuildDir = substr($realBuildDir, 0, -1).('~' === substr($realBuildDir, -1) ? '+' : '~'); | ||
$oldCacheDir = substr($realCacheDir, 0, -1).('~' === substr($realCacheDir, -1) ? '+' : '~'); | ||
$fs->remove($oldCacheDir); | ||
$fs->remove([$oldBuildDir, $oldCacheDir]); | ||
|
||
if (!is_writable($realBuildDir)) { | ||
throw new RuntimeException(sprintf('Unable to write in the "%s" directory.', $realBuildDir)); | ||
} | ||
if (!is_writable($realCacheDir)) { | ||
throw new RuntimeException(sprintf('Unable to write in the "%s" directory.', $realCacheDir)); | ||
} | ||
|
||
$io->comment(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true))); | ||
$this->cacheClearer->clear($realBuildDir); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this command avoid removing the build dir by default? What about adding a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a first step, we must clear everything like today. |
||
$this->cacheClearer->clear($realCacheDir); | ||
|
||
// The current event dispatcher is stale, let's not use it anymore | ||
|
@@ -155,17 +161,31 @@ protected function execute(InputInterface $input, OutputInterface $output): int | |
} | ||
} | ||
|
||
if ($oldBuildDir) { | ||
$fs->rename($realBuildDir, $oldBuildDir); | ||
} else { | ||
$fs->remove($realBuildDir); | ||
} | ||
if ($oldCacheDir) { | ||
$fs->rename($realCacheDir, $oldCacheDir); | ||
} else { | ||
$fs->remove($realCacheDir); | ||
} | ||
$fs->rename($warmupDir, $realCacheDir); | ||
// Copy the content of the warmed cache in the build dir | ||
$fs->copy($realCacheDir, $realBuildDir); | ||
|
||
if ($output->isVerbose()) { | ||
$io->comment('Removing old cache directory...'); | ||
$io->comment('Removing old build and cache directory...'); | ||
} | ||
|
||
try { | ||
$fs->remove($oldBuildDir); | ||
} catch (IOException $e) { | ||
if ($output->isVerbose()) { | ||
$io->warning($e->getMessage()); | ||
} | ||
} | ||
try { | ||
$fs->remove($oldCacheDir); | ||
} catch (IOException $e) { | ||
|
@@ -184,7 +204,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int | |
return 0; | ||
} | ||
|
||
private function warmup(string $warmupDir, string $realCacheDir, bool $enableOptionalWarmers = true) | ||
private function warmup(string $warmupDir, string $realBuildDir, bool $enableOptionalWarmers = true) | ||
{ | ||
// create a temporary kernel | ||
$kernel = $this->getApplication()->getKernel(); | ||
|
@@ -207,7 +227,7 @@ private function warmup(string $warmupDir, string $realCacheDir, bool $enableOpt | |
|
||
// fix references to cached files with the real cache directory name | ||
$search = [$warmupDir, str_replace('\\', '\\\\', $warmupDir)]; | ||
$replace = str_replace('\\', '/', $realCacheDir); | ||
$replace = str_replace('\\', '/', $realBuildDir); | ||
foreach (Finder::create()->files()->in($warmupDir) as $file) { | ||
$content = str_replace($search, $replace, file_get_contents($file), $count); | ||
if ($count) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -314,7 +314,7 @@ public function getContainer() | |
*/ | ||
public function setAnnotatedClassCache(array $annotatedClasses) | ||
{ | ||
file_put_contents(($this->warmupDir ?: $this->getCacheDir()).'/annotations.map', sprintf('<?php return %s;', var_export($annotatedClasses, true))); | ||
file_put_contents(($this->warmupDir ?: $this->getBuildDir()).'/annotations.map', sprintf('<?php return %s;', var_export($annotatedClasses, true))); | ||
} | ||
|
||
/** | ||
|
@@ -333,6 +333,15 @@ public function getCacheDir() | |
return $this->getProjectDir().'/var/cache/'.$this->environment; | ||
} | ||
|
||
/** | ||
* Gets the build directory. | ||
*/ | ||
public function getBuildDir(): string | ||
{ | ||
// Returns $this->getCacheDir() for backward compatibility | ||
return $this->getCacheDir(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here is the core of the PR :) For backward compatibility, I return the current cache directory. In future versions, this could be for example |
||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
|
@@ -419,14 +428,14 @@ protected function getContainerBaseClass() | |
/** | ||
* Initializes the service container. | ||
* | ||
* The cached version of the service container is used when fresh, otherwise the | ||
* The built version of the service container is used when fresh, otherwise the | ||
* container is built. | ||
*/ | ||
protected function initializeContainer() | ||
{ | ||
$class = $this->getContainerClass(); | ||
$cacheDir = $this->warmupDir ?: $this->getCacheDir(); | ||
$cache = new ConfigCache($cacheDir.'/'.$class.'.php', $this->debug); | ||
$buildDir = $this->warmupDir ?: $this->getBuildDir(); | ||
$cache = new ConfigCache($buildDir.'/'.$class.'.php', $this->debug); | ||
$cachePath = $cache->getPath(); | ||
|
||
// Silence E_WARNING to ignore "include" failures - don't use "@" to prevent silencing fatal errors | ||
|
@@ -448,7 +457,7 @@ protected function initializeContainer() | |
$oldContainer = \is_object($this->container) ? new \ReflectionClass($this->container) : $this->container = null; | ||
|
||
try { | ||
is_dir($cacheDir) ?: mkdir($cacheDir, 0777, true); | ||
is_dir($buildDir) ?: mkdir($buildDir, 0777, true); | ||
|
||
if ($lock = fopen($cachePath.'.lock', 'w')) { | ||
flock($lock, LOCK_EX | LOCK_NB, $wouldBlock); | ||
|
@@ -533,8 +542,8 @@ protected function initializeContainer() | |
if ($collectDeprecations) { | ||
restore_error_handler(); | ||
|
||
file_put_contents($cacheDir.'/'.$class.'Deprecations.log', serialize(array_values($collectedLogs))); | ||
file_put_contents($cacheDir.'/'.$class.'Compiler.log', null !== $container ? implode("\n", $container->getCompiler()->getLog()) : ''); | ||
file_put_contents($buildDir.'/'.$class.'Deprecations.log', serialize(array_values($collectedLogs))); | ||
file_put_contents($buildDir.'/'.$class.'Compiler.log', null !== $container ? implode("\n", $container->getCompiler()->getLog()) : ''); | ||
} | ||
} | ||
|
||
|
@@ -570,7 +579,7 @@ protected function initializeContainer() | |
$preload = array_merge($preload, (array) $this->container->get('cache_warmer')->warmUp($this->container->getParameter('kernel.cache_dir'))); | ||
} | ||
|
||
if ($preload && method_exists(Preloader::class, 'append') && file_exists($preloadFile = $cacheDir.'/'.$class.'.preload.php')) { | ||
if ($preload && method_exists(Preloader::class, 'append') && file_exists($preloadFile = $buildDir.'/'.$class.'.preload.php')) { | ||
Preloader::append($preloadFile, $preload); | ||
} | ||
} | ||
|
@@ -597,7 +606,8 @@ protected function getKernelParameters() | |
'kernel.project_dir' => realpath($this->getProjectDir()) ?: $this->getProjectDir(), | ||
'kernel.environment' => $this->environment, | ||
'kernel.debug' => $this->debug, | ||
'kernel.cache_dir' => realpath($cacheDir = $this->warmupDir ?: $this->getCacheDir()) ?: $cacheDir, | ||
'kernel.build_dir' => realpath($buildDir = $this->warmupDir ?: $this->getBuildDir()) ?: $buildDir, | ||
'kernel.cache_dir' => realpath($this->getCacheDir()) ?: $this->getCacheDir(), | ||
'kernel.logs_dir' => realpath($this->getLogDir()) ?: $this->getLogDir(), | ||
'kernel.bundles' => $bundles, | ||
'kernel.bundles_metadata' => $bundlesMetadata, | ||
|
@@ -615,7 +625,7 @@ protected function getKernelParameters() | |
*/ | ||
protected function buildContainer() | ||
{ | ||
foreach (['cache' => $this->warmupDir ?: $this->getCacheDir(), 'logs' => $this->getLogDir()] as $name => $dir) { | ||
foreach (['cache' => $this->getCacheDir(), 'build' => $this->warmupDir ?: $this->getBuildDir(), 'logs' => $this->getLogDir()] as $name => $dir) { | ||
if (!is_dir($dir)) { | ||
if (false === @mkdir($dir, 0777, true) && !is_dir($dir)) { | ||
throw new \RuntimeException(sprintf('Unable to create the "%s" directory (%s).', $name, $dir)); | ||
|
Uh oh!
There was an error while loading. Please reload this page.