Skip to content
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

Improved performance of ContainerFactory::clearOldContainers() #733

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Command/CommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public static function begin(
bool $manageMemoryLimitFile = true,
bool $debugEnabled = false,
?string $singleReflectionFile = null,
?string $singleReflectionInsteadOfFile = null
?string $singleReflectionInsteadOfFile = null,
bool $cleanupContainerCache = true
): InceptionResult
{
if (!$allowXdebug) {
Expand Down Expand Up @@ -235,7 +236,9 @@ public static function begin(
throw new \PHPStan\Command\InceptionNotSuccessfulException();
}

$containerFactory->clearOldContainers($tmpDir);
if ($cleanupContainerCache) {
$containerFactory->clearOldContainers($tmpDir);
}

if (count($paths) === 0) {
$errorOutput->writeLineFormatted('At least one path must be specified to analyse.');
Expand Down
4 changes: 3 additions & 1 deletion src/Command/WorkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$allowXdebug,
false,
false,
$singleReflectionFile
$singleReflectionFile,
null,
false
);
} catch (\PHPStan\Command\InceptionNotSuccessfulException $e) {
return 1;
Expand Down
17 changes: 11 additions & 6 deletions src/DependencyInjection/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Reflection\ReflectionProviderStaticAccessor;
use Symfony\Component\Finder\Finder;
use function sys_get_temp_dir;

/** @api */
Expand Down Expand Up @@ -141,13 +140,15 @@ public function clearOldContainers(string $tempDirectory): void
$configurator->setDebugMode(true);
$configurator->setTempDirectory($tempDirectory);

$finder = new Finder();
$finder->name('Container_*')->in($configurator->getContainerCacheDirectory());
$iterator = new \FilesystemIterator($configurator->getContainerCacheDirectory(), \FilesystemIterator::KEY_AS_FILENAME | \FilesystemIterator::SKIP_DOTS);
$twoDaysAgo = time() - 24 * 60 * 60 * 2;

foreach ($finder as $containerFile) {
$path = $containerFile->getRealPath();
if ($path === false) {
/**
* @var string $fileName
* @var \SplFileInfo $containerFile
*/
foreach ($iterator as $fileName => $containerFile) {
if (preg_match('/^Container_.+\.php(\.meta|\.lock)?$/', $fileName) !== 1) {
continue;
}
if ($containerFile->getATime() > $twoDaysAgo) {
Expand All @@ -157,6 +158,10 @@ public function clearOldContainers(string $tempDirectory): void
continue;
}

$path = $containerFile->getRealPath();
if ($path === false) {
continue;
}
@unlink($path);
}
}
Expand Down