Skip to content

Commit dec3ba0

Browse files
committed
revert to v3.2.1
1 parent afe7187 commit dec3ba0

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

src/RobotLoader/RobotLoader.php

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -411,51 +411,37 @@ public function setTempDirectory(string $dir): self
411411
private function loadCache(): void
412412
{
413413
$file = $this->getCacheFile();
414-
$handle = fopen($file, 'cb+');
415-
if (!$handle || !flock($handle, LOCK_SH)) {
416-
throw new \RuntimeException("Unable to create or acquire shared lock on file '$file'.");
417-
}
418-
419-
$data = include $file;
420-
if (is_array($data)) {
421-
[$this->classes, $this->missing] = $data;
414+
[$this->classes, $this->missing] = @include $file; // @ file may not exist
415+
if (is_array($this->classes)) {
422416
return;
423417
}
424418

425-
if (!flock($handle, LOCK_EX)) {
426-
throw new \RuntimeException("Unable to create or acquire exclusive lock on file '$file'.");
419+
$handle = fopen("$file.lock", 'cb+');
420+
if (!$handle || !flock($handle, LOCK_EX)) {
421+
throw new \RuntimeException("Unable to create or acquire exclusive lock on file '$file.lock'.");
427422
}
428423

429-
// while waiting for the lock, someone might have already created the cache
430-
if (fstat($handle)['size']) {
431-
flock($handle, LOCK_SH);
432-
$data = include $file;
433-
if (is_array($data)) {
434-
[$this->classes, $this->missing] = $data;
435-
return;
436-
}
424+
[$this->classes, $this->missing] = @include $file; // @ file may not exist
425+
if (!is_array($this->classes)) {
426+
$this->rebuild();
437427
}
438428

439-
$this->classes = $this->missing = [];
440-
$this->refreshClasses();
441-
$this->saveCache($handle);
429+
flock($handle, LOCK_UN);
430+
fclose($handle);
431+
@unlink("$file.lock"); // @ file may become locked on Windows
442432
}
443433

444434

445435
/**
446436
* Writes class list to cache.
447437
*/
448-
private function saveCache($handle = null): void
438+
private function saveCache(): void
449439
{
450440
$file = $this->getCacheFile();
451-
$handle = $handle ?: fopen($file, 'cb+');
452-
if (!$handle || !flock($handle, LOCK_EX)) {
453-
throw new \RuntimeException("Unable to create or acquire exclusive lock on file '$file'.");
454-
}
455-
441+
$tempFile = $file . uniqid('', true) . '.tmp';
456442
$code = "<?php\nreturn " . var_export([$this->classes, $this->missing], true) . ";\n";
457-
if (!ftruncate($handle, 0) || fwrite($handle, $code) !== strlen($code)) {
458-
@unlink($file); // @ - the locked file may not be deleted
443+
if (file_put_contents($tempFile, $code) !== strlen($code) || !rename($tempFile, $file)) {
444+
@unlink($tempFile); // @ - file may not exist
459445
throw new \RuntimeException("Unable to create '$file'.");
460446
}
461447
if (function_exists('opcache_invalidate')) {

0 commit comments

Comments
 (0)