From db0a5f61a436b337d11cfb2b4258336bcfab4fff Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Wed, 6 Jan 2021 16:08:18 +0100 Subject: [PATCH] Clear cache directory - delete old containers --- src/Command/CommandHelper.php | 2 ++ src/DependencyInjection/Configurator.php | 7 ++++- src/DependencyInjection/ContainerFactory.php | 33 ++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/Command/CommandHelper.php b/src/Command/CommandHelper.php index d2f577c066..cc4e384707 100644 --- a/src/Command/CommandHelper.php +++ b/src/Command/CommandHelper.php @@ -254,6 +254,8 @@ public static function begin( throw new \PHPStan\Command\InceptionNotSuccessfulException(); } + $containerFactory->clearOldContainers($tmpDir); + if (count($paths) === 0) { $errorOutput->writeLineFormatted('At least one path must be specified to analyse.'); throw new \PHPStan\Command\InceptionNotSuccessfulException(); diff --git a/src/DependencyInjection/Configurator.php b/src/DependencyInjection/Configurator.php index 67f40bce3b..1da912305f 100644 --- a/src/DependencyInjection/Configurator.php +++ b/src/DependencyInjection/Configurator.php @@ -30,10 +30,15 @@ protected function getDefaultParameters(): array return []; } + public function getContainerCacheDirectory(): string + { + return $this->getCacheDirectory() . '/nette.configurator'; + } + public function loadContainer(): string { $loader = new ContainerLoader( - $this->getCacheDirectory() . '/nette.configurator', + $this->getContainerCacheDirectory(), $this->parameters['debugMode'] ); diff --git a/src/DependencyInjection/ContainerFactory.php b/src/DependencyInjection/ContainerFactory.php index 940386eb26..1d340be041 100644 --- a/src/DependencyInjection/ContainerFactory.php +++ b/src/DependencyInjection/ContainerFactory.php @@ -10,6 +10,7 @@ use PHPStan\Php\PhpVersion; use Roave\BetterReflection\BetterReflection; use Roave\BetterReflection\SourceLocator\SourceStubber\PhpStormStubsSourceStubber; +use Symfony\Component\Finder\Finder; use function sys_get_temp_dir; class ContainerFactory @@ -119,6 +120,38 @@ public function create( return $container->getByType(Container::class); } + public function clearOldContainers(string $tempDirectory): void + { + $configurator = new Configurator(new LoaderFactory( + $this->fileHelper, + $this->rootDirectory, + $this->currentWorkingDirectory, + null + )); + $configurator->setDebugMode(true); + $configurator->setTempDirectory($tempDirectory); + + $finder = new Finder(); + $finder->name('Container_*')->in($configurator->getContainerCacheDirectory()); + $twoDaysAgo = time() - 24 * 60 * 60 * 2; + + foreach ($finder as $containerFile) { + if ($containerFile->getATime() > $twoDaysAgo) { + continue; + } + if ($containerFile->getCTime() > $twoDaysAgo) { + continue; + } + + $path = $containerFile->getRealPath(); + if ($path === false) { + continue; + } + + @unlink($path); + } + } + public function getCurrentWorkingDirectory(): string { return $this->currentWorkingDirectory;