Skip to content

Commit

Permalink
stores list of empty PHP files
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 7, 2021
1 parent 266c0c0 commit ed45c04
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/RobotLoader/RobotLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class RobotLoader
/** @var array of class => counter */
private $missingClasses = [];

/** @var array of file => mtime */
private $emptyFiles = [];

/** @var string|null */
private $tempDirectory;

Expand Down Expand Up @@ -187,7 +190,7 @@ public function getIndexedClasses(): array
public function rebuild(): void
{
$this->cacheLoaded = true;
$this->classes = $this->missingClasses = [];
$this->classes = $this->missingClasses = $this->emptyFiles = [];
$this->refreshClasses();
if ($this->tempDirectory) {
$this->saveCache();
Expand All @@ -209,17 +212,18 @@ public function refresh(): void


/**
* Refreshes $classes.
* Refreshes $this->classes & $this->emptyFiles.
*/
private function refreshClasses(): void
{
$this->refreshed = true; // prevents calling refreshClasses() or updateFile() in tryLoad()
$files = $classes = [];
$files = $this->emptyFiles;
$classes = [];
foreach ($this->classes as $class => [$file, $mtime]) {
$files[$file] = $mtime;
$classes[$file][] = $class;
}
$this->classes = [];
$this->classes = $this->emptyFiles = [];

foreach ($this->scanPaths as $path) {
$iterator = is_file($path)
Expand All @@ -230,9 +234,13 @@ private function refreshClasses(): void
$mtime = $fileInfo->getMTime();
$file = $fileInfo->getPathname();
$foundClasses = isset($files[$file]) && $files[$file] === $mtime
? $classes[$file]
? ($classes[$file] ?? [])
: $this->scanPhp($file);

if (!$foundClasses) {
$this->emptyFiles[$file] = $mtime;
}

$files[$file] = $mtime;
$classes[$file] = []; // prevents the error when adding the same file twice

Expand Down Expand Up @@ -456,7 +464,7 @@ private function loadCache(): void

$data = @include $file; // @ file may not exist
if (is_array($data)) {
[$this->classes, $this->missingClasses] = $data;
[$this->classes, $this->missingClasses, $this->emptyFiles] = $data;
return;
}

Expand All @@ -468,11 +476,11 @@ private function loadCache(): void
// while waiting for exclusive lock, someone might have already created the cache
$data = @include $file; // @ file may not exist
if (is_array($data)) {
[$this->classes, $this->missingClasses] = $data;
[$this->classes, $this->missingClasses, $this->emptyFiles] = $data;
return;
}

$this->classes = $this->missingClasses = [];
$this->classes = $this->missingClasses = $this->emptyFiles = [];
$this->refreshClasses();
$this->saveCache($lock);
// On Windows concurrent creation and deletion of a file can cause a error 'permission denied',
Expand All @@ -490,7 +498,7 @@ private function saveCache($lock = null): void
// on Windows: that the file is not read by another thread
$file = $this->getCacheFile();
$lock = $lock ?: $this->acquireLock("$file.lock", LOCK_EX);
$code = "<?php\nreturn " . var_export([$this->classes, $this->missingClasses], true) . ";\n";
$code = "<?php\nreturn " . var_export([$this->classes, $this->missingClasses, $this->emptyFiles], true) . ";\n";

if (file_put_contents("$file.tmp", $code) !== strlen($code) || !rename("$file.tmp", $file)) {
@unlink("$file.tmp"); // @ file may not exist
Expand Down

0 comments on commit ed45c04

Please sign in to comment.