Skip to content

Commit

Permalink
Clear cache directory - delete old containers
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 6, 2021
1 parent 1f441e1 commit db0a5f6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Command/CommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 6 additions & 1 deletion src/DependencyInjection/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']
);

Expand Down
33 changes: 33 additions & 0 deletions src/DependencyInjection/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

1 comment on commit db0a5f6

@dktapps
Copy link
Contributor

@dktapps dktapps commented on db0a5f6 Jan 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this, I have 500+ MB of these things floating around in my tmpdir on my Windows machine 😅

Please sign in to comment.