Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b41a21c

Browse files
committed
Fix cache:clear with buildDir
1 parent 26e1e6b commit b41a21c

File tree

13 files changed

+68
-39
lines changed

13 files changed

+68
-39
lines changed

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

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9191
}
9292

9393
$useBuildDir = $realBuildDir !== $realCacheDir;
94+
$oldBuildDir = substr($realBuildDir, 0, -1).('~' === substr($realBuildDir, -1) ? '+' : '~');
9495
if ($useBuildDir) {
95-
$oldBuildDir = substr($realBuildDir, 0, -1).('~' === substr($realBuildDir, -1) ? '+' : '~');
9696
$fs->remove($oldBuildDir);
9797

9898
if (!is_writable($realBuildDir)) {
9999
throw new RuntimeException(sprintf('Unable to write in the "%s" directory.', $realBuildDir));
100100
}
101+
102+
if ($this->isNfs($realCacheDir)) {
103+
$fs->remove($realCacheDir);
104+
} else {
105+
$fs->rename($realCacheDir, $oldCacheDir);
106+
}
107+
$fs->mkdir($realCacheDir);
101108
}
102109

103110
$io->comment(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
@@ -114,7 +121,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
114121

115122
// the warmup cache dir name must have the same length as the real one
116123
// to avoid the many problems in serialized resources files
117-
$warmupDir = substr($realCacheDir, 0, -1).('_' === substr($realCacheDir, -1) ? '-' : '_');
124+
$warmupDir = substr($realBuildDir, 0, -1).('_' === substr($realBuildDir, -1) ? '-' : '_');
118125

119126
if ($output->isVerbose() && $fs->exists($warmupDir)) {
120127
$io->comment('Clearing outdated warmup directory...');
@@ -153,35 +160,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
153160
touch($warmupDir.'/'.$containerDir.'.legacy');
154161
}
155162

156-
if ('/' === \DIRECTORY_SEPARATOR && $mounts = @file('/proc/mounts')) {
157-
foreach ($mounts as $mount) {
158-
$mount = \array_slice(explode(' ', $mount), 1, -3);
159-
if (!\in_array(array_pop($mount), ['vboxsf', 'nfs'])) {
160-
continue;
161-
}
162-
$mount = implode(' ', $mount).'/';
163-
164-
if (0 === strpos($realCacheDir, $mount)) {
165-
$io->note('For better performances, you should move the cache and log directories to a non-shared folder of the VM.');
166-
$oldCacheDir = false;
167-
break;
168-
}
169-
}
170-
}
171-
172-
if ($oldCacheDir) {
173-
$fs->rename($realCacheDir, $oldCacheDir);
163+
if ($this->isNfs($realBuildDir)) {
164+
$io->note('For better performances, you should move the cache and log directories to a non-shared folder of the VM.');
165+
$fs->remove($realBuildDir);
174166
} else {
175-
$fs->remove($realCacheDir);
176-
}
177-
$fs->rename($warmupDir, $realCacheDir);
178-
179-
if ($useBuildDir) {
180167
$fs->rename($realBuildDir, $oldBuildDir);
181-
// Copy the content of the warmed cache in the build dir
182-
$fs->mirror($realCacheDir, $realBuildDir);
183168
}
184169

170+
$fs->rename($warmupDir, $realBuildDir);
171+
185172
if ($output->isVerbose()) {
186173
$io->comment('Removing old build and cache directory...');
187174
}
@@ -214,6 +201,31 @@ protected function execute(InputInterface $input, OutputInterface $output): int
214201
return 0;
215202
}
216203

204+
private function isNfs(string $dir): bool
205+
{
206+
static $mounts = null;
207+
208+
if (null === $mounts) {
209+
$mounts = [];
210+
if ('/' === \DIRECTORY_SEPARATOR && $mounts = @file('/proc/mounts')) {
211+
foreach ($mounts as $mount) {
212+
$mount = \array_slice(explode(' ', $mount), 1, -3);
213+
if (!\in_array(array_pop($mount), ['vboxsf', 'nfs'])) {
214+
continue;
215+
}
216+
$mounts[] = implode(' ', $mount).'/';
217+
}
218+
}
219+
}
220+
foreach ($mounts as $mount) {
221+
if (0 === strpos($dir, $mount)) {
222+
return true;
223+
}
224+
}
225+
226+
return false;
227+
}
228+
217229
private function warmup(string $warmupDir, string $realBuildDir, bool $enableOptionalWarmers = true)
218230
{
219231
// create a temporary kernel

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/JsonDescriptor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ protected function describeContainerEnvVars(array $envs, array $options = [])
157157

158158
protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void
159159
{
160-
$containerDeprecationFilePath = sprintf('%s/%sDeprecations.log', $builder->getParameter('kernel.cache_dir'), $builder->getParameter('kernel.container_class'));
160+
$containerDeprecationFilePath = sprintf('%s/%sDeprecations.log', $builder->getParameter('kernel.build_dir'), $builder->getParameter('kernel.container_class'));
161161
if (!file_exists($containerDeprecationFilePath)) {
162162
throw new RuntimeException('The deprecation file does not exist, please try warming the cache first.');
163163
}

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/MarkdownDescriptor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ protected function describeContainerService($service, array $options = [], Conta
107107

108108
protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void
109109
{
110-
$containerDeprecationFilePath = sprintf('%s/%sDeprecations.log', $builder->getParameter('kernel.cache_dir'), $builder->getParameter('kernel.container_class'));
110+
$containerDeprecationFilePath = sprintf('%s/%sDeprecations.log', $builder->getParameter('kernel.build_dir'), $builder->getParameter('kernel.container_class'));
111111
if (!file_exists($containerDeprecationFilePath)) {
112112
throw new RuntimeException('The deprecation file does not exist, please try warming the cache first.');
113113
}

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/TextDescriptor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ protected function describeContainerDefinition(Definition $definition, array $op
360360

361361
protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void
362362
{
363-
$containerDeprecationFilePath = sprintf('%s/%sDeprecations.log', $builder->getParameter('kernel.cache_dir'), $builder->getParameter('kernel.container_class'));
363+
$containerDeprecationFilePath = sprintf('%s/%sDeprecations.log', $builder->getParameter('kernel.build_dir'), $builder->getParameter('kernel.container_class'));
364364
if (!file_exists($containerDeprecationFilePath)) {
365365
$options['output']->warning('The deprecation file does not exist, please try warming the cache first.');
366366

src/Symfony/Bundle/FrameworkBundle/Console/Descriptor/XmlDescriptor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ protected function describeContainerEnvVars(array $envs, array $options = [])
109109

110110
protected function describeContainerDeprecations(ContainerBuilder $builder, array $options = []): void
111111
{
112-
$containerDeprecationFilePath = sprintf('%s/%sDeprecations.log', $builder->getParameter('kernel.cache_dir'), $builder->getParameter('kernel.container_class'));
112+
$containerDeprecationFilePath = sprintf('%s/%sDeprecations.log', $builder->getParameter('kernel.build_dir'), $builder->getParameter('kernel.container_class'));
113113
if (!file_exists($containerDeprecationFilePath)) {
114114
throw new RuntimeException('The deprecation file does not exist, please try warming the cache first.');
115115
}

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ private function registerDebugConfiguration(array $config, ContainerBuilder $con
899899
$debug = $container->getParameter('kernel.debug');
900900

901901
if ($debug) {
902-
$container->setParameter('debug.container.dump', '%kernel.cache_dir%/%kernel.container_class%.xml');
902+
$container->setParameter('debug.container.dump', '%kernel.build_dir%/%kernel.container_class%.xml');
903903
}
904904

905905
if ($debug && class_exists(Stopwatch::class)) {

src/Symfony/Bundle/FrameworkBundle/Resources/config/collectors.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
->set('data_collector.logger', LoggerDataCollector::class)
5656
->args([
5757
service('logger')->ignoreOnInvalid(),
58-
sprintf('%s/%s', param('kernel.cache_dir'), param('kernel.container_class')),
58+
sprintf('%s/%s', param('kernel.build_dir'), param('kernel.container_class')),
5959
service('request_stack')->ignoreOnInvalid(),
6060
])
6161
->tag('monolog.logger', ['channel' => 'profiler'])

src/Symfony/Bundle/FrameworkBundle/Resources/config/services.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class_exists(WorkflowEvents::class) ? WorkflowEvents::ALIASES : []
109109
->args([
110110
tagged_iterator('kernel.cache_warmer'),
111111
param('kernel.debug'),
112-
sprintf('%s/%sDeprecations.log', param('kernel.cache_dir'), param('kernel.container_class')),
112+
sprintf('%s/%sDeprecations.log', param('kernel.build_dir'), param('kernel.container_class')),
113113
])
114114
->tag('container.no_preload')
115115

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,19 @@ public function testCacheIsFreshAfterCacheClearedWithWarmup()
8383
$containerFile = str_replace('tes_'.\DIRECTORY_SEPARATOR, 'test'.\DIRECTORY_SEPARATOR, $containerRef->getFileName());
8484
$this->assertMatchesRegularExpression(sprintf('/\'kernel.container_class\'\s*=>\s*\'%s\'/', $containerClass), file_get_contents($containerFile), 'kernel.container_class is properly set on the dumped container');
8585
}
86+
87+
public function testCacheIsWarmedWhenCalledTwice()
88+
{
89+
$input = new ArrayInput(['cache:clear']);
90+
$application = new Application($this->kernel);
91+
$application->setCatchExceptions(false);
92+
$application->doRun($input, new NullOutput());
93+
94+
$_SERVER['REQUEST_TIME'] = time() + 1;
95+
$application = new Application(new TestAppKernel('test', true));
96+
$application->setCatchExceptions(false);
97+
$application->doRun($input, new NullOutput());
98+
99+
$this->assertTrue(is_file($this->kernel->getCacheDir().'/annotations.php'));
100+
}
86101
}

src/Symfony/Bundle/FrameworkBundle/Tests/Console/Descriptor/ObjectsProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,12 @@ public static function getContainerDeprecations()
9292
{
9393
$builderWithDeprecations = new ContainerBuilder();
9494
$builderWithDeprecations->setParameter('kernel.cache_dir', __DIR__.'/../../Fixtures/Descriptor/cache');
95+
$builderWithDeprecations->setParameter('kernel.build_dir', __DIR__.'/../../Fixtures/Descriptor/cache');
9596
$builderWithDeprecations->setParameter('kernel.container_class', 'KernelContainerWith');
9697

9798
$builderWithoutDeprecations = new ContainerBuilder();
9899
$builderWithoutDeprecations->setParameter('kernel.cache_dir', __DIR__.'/../../Fixtures/Descriptor/cache');
100+
$builderWithoutDeprecations->setParameter('kernel.build_dir', __DIR__.'/../../Fixtures/Descriptor/cache');
99101
$builderWithoutDeprecations->setParameter('kernel.container_class', 'KernelContainerWithout');
100102

101103
return [

0 commit comments

Comments
 (0)