Skip to content

[FrameworkBundle] Fix missing kernel.build_dir on cache clear #39478

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

Merged
merged 1 commit into from
Dec 14, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ 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');
$realBuildDir = $kernel->getContainer()->hasParameter('kernel.build_dir') ? $kernel->getContainer()->getParameter('kernel.build_dir') : $realCacheDir;
// 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)
$oldCacheDir = substr($realCacheDir, 0, -1).('~' === substr($realCacheDir, -1) ? '+' : '~');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\KernelInterface;

class CacheClearCommandTest extends TestCase
{
Expand All @@ -40,17 +41,18 @@ protected function tearDown(): void
$this->fs->remove($this->kernel->getProjectDir());
}

public function testCacheIsFreshAfterCacheClearedWithWarmup()
/** @dataProvider getKernel */
public function testCacheIsFreshAfterCacheClearedWithWarmup(KernelInterface $kernel)
{
$input = new ArrayInput(['cache:clear']);
$application = new Application($this->kernel);
$application = new Application($kernel);
$application->setCatchExceptions(false);

$application->doRun($input, new NullOutput());

// Ensure that all *.meta files are fresh
$finder = new Finder();
$metaFiles = $finder->files()->in($this->kernel->getCacheDir())->name('*.php.meta');
$metaFiles = $finder->files()->in($kernel->getCacheDir())->name('*.php.meta');
// check that cache is warmed up
$this->assertNotEmpty($metaFiles);
$configCacheFactory = new ConfigCacheFactory(true);
Expand All @@ -62,11 +64,11 @@ public function testCacheIsFreshAfterCacheClearedWithWarmup()
}

// check that app kernel file present in meta file of container's cache
$containerClass = $this->kernel->getContainer()->getParameter('kernel.container_class');
$containerClass = $kernel->getContainer()->getParameter('kernel.container_class');
$containerRef = new \ReflectionClass($containerClass);
$containerFile = \dirname($containerRef->getFileName(), 2).'/'.$containerClass.'.php';
$containerMetaFile = $containerFile.'.meta';
$kernelRef = new \ReflectionObject($this->kernel);
$kernelRef = new \ReflectionObject($kernel);
$kernelFile = $kernelRef->getFileName();
/** @var ResourceInterface[] $meta */
$meta = unserialize(file_get_contents($containerMetaFile));
Expand All @@ -83,4 +85,21 @@ public function testCacheIsFreshAfterCacheClearedWithWarmup()
$containerFile = str_replace('tes_'.\DIRECTORY_SEPARATOR, 'test'.\DIRECTORY_SEPARATOR, $containerRef->getFileName());
$this->assertMatchesRegularExpression(sprintf('/\'kernel.container_class\'\s*=>\s*\'%s\'/', $containerClass), file_get_contents($containerFile), 'kernel.container_class is properly set on the dumped container');
}

public function getKernel()
{
yield [new TestAppKernel('test', true)];
yield [new NoBuildDirKernel('test', true)];
}
}

class NoBuildDirKernel extends TestAppKernel
{
protected function getKernelParameters()
{
$parameters = parent::getKernelParameters();
unset($parameters['kernel.build_dir']);

return $parameters;
}
}