Skip to content

Commit 120c1bc

Browse files
committed
Saves cache only once
1 parent ed45c04 commit 120c1bc

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/RobotLoader/RobotLoader.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ class RobotLoader
6666
/** @var string|null */
6767
private $tempDirectory;
6868

69+
/** @var bool */
70+
private $needSave = false;
71+
6972

7073
public function __construct()
7174
{
@@ -75,6 +78,14 @@ public function __construct()
7578
}
7679

7780

81+
public function __destruct()
82+
{
83+
if ($this->needSave) {
84+
$this->saveCache();
85+
}
86+
}
87+
88+
7889
/**
7990
* Register autoloader.
8091
*/
@@ -100,31 +111,25 @@ public function tryLoad(string $type): void
100111
[$file, $mtime] = $this->classes[$type] ?? null;
101112

102113
if ($this->autoRebuild) {
103-
$save = false;
104-
105114
if (!$this->refreshed) {
106115
if (!$file || !is_file($file)) {
107116
$this->refreshClasses();
108117
[$file] = $this->classes[$type] ?? null;
109-
$save = true;
118+
$this->needSave = true;
110119

111120
} elseif (filemtime($file) !== $mtime) {
112121
$this->updateFile($file);
113122
[$file] = $this->classes[$type] ?? null;
114-
$save = true;
123+
$this->needSave = true;
115124
}
116125
}
117126

118127
if (!$file || !is_file($file)) {
119128
$this->missingClasses[$type] = ++$missing;
120-
$save = $save || $file || ($missing <= self::RETRY_LIMIT);
129+
$this->needSave = $this->needSave || $file || ($missing <= self::RETRY_LIMIT);
121130
unset($this->classes[$type]);
122131
$file = null;
123132
}
124-
125-
if ($save) {
126-
$this->saveCache();
127-
}
128133
}
129134

130135
if ($file) {

0 commit comments

Comments
 (0)