Skip to content

Commit db0a5f6

Browse files
committed
Clear cache directory - delete old containers
1 parent 1f441e1 commit db0a5f6

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/Command/CommandHelper.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ public static function begin(
254254
throw new \PHPStan\Command\InceptionNotSuccessfulException();
255255
}
256256

257+
$containerFactory->clearOldContainers($tmpDir);
258+
257259
if (count($paths) === 0) {
258260
$errorOutput->writeLineFormatted('At least one path must be specified to analyse.');
259261
throw new \PHPStan\Command\InceptionNotSuccessfulException();

src/DependencyInjection/Configurator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,15 @@ protected function getDefaultParameters(): array
3030
return [];
3131
}
3232

33+
public function getContainerCacheDirectory(): string
34+
{
35+
return $this->getCacheDirectory() . '/nette.configurator';
36+
}
37+
3338
public function loadContainer(): string
3439
{
3540
$loader = new ContainerLoader(
36-
$this->getCacheDirectory() . '/nette.configurator',
41+
$this->getContainerCacheDirectory(),
3742
$this->parameters['debugMode']
3843
);
3944

src/DependencyInjection/ContainerFactory.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\Php\PhpVersion;
1111
use Roave\BetterReflection\BetterReflection;
1212
use Roave\BetterReflection\SourceLocator\SourceStubber\PhpStormStubsSourceStubber;
13+
use Symfony\Component\Finder\Finder;
1314
use function sys_get_temp_dir;
1415

1516
class ContainerFactory
@@ -119,6 +120,38 @@ public function create(
119120
return $container->getByType(Container::class);
120121
}
121122

123+
public function clearOldContainers(string $tempDirectory): void
124+
{
125+
$configurator = new Configurator(new LoaderFactory(
126+
$this->fileHelper,
127+
$this->rootDirectory,
128+
$this->currentWorkingDirectory,
129+
null
130+
));
131+
$configurator->setDebugMode(true);
132+
$configurator->setTempDirectory($tempDirectory);
133+
134+
$finder = new Finder();
135+
$finder->name('Container_*')->in($configurator->getContainerCacheDirectory());
136+
$twoDaysAgo = time() - 24 * 60 * 60 * 2;
137+
138+
foreach ($finder as $containerFile) {
139+
if ($containerFile->getATime() > $twoDaysAgo) {
140+
continue;
141+
}
142+
if ($containerFile->getCTime() > $twoDaysAgo) {
143+
continue;
144+
}
145+
146+
$path = $containerFile->getRealPath();
147+
if ($path === false) {
148+
continue;
149+
}
150+
151+
@unlink($path);
152+
}
153+
}
154+
122155
public function getCurrentWorkingDirectory(): string
123156
{
124157
return $this->currentWorkingDirectory;

0 commit comments

Comments
 (0)