Skip to content

Commit 58a999b

Browse files
committed
Loads and builds cache on demand
1 parent d38c688 commit 58a999b

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

src/RobotLoader/RobotLoader.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class RobotLoader
5151
/** @var array of class => [file, time] */
5252
private $classes = [];
5353

54+
/** @var bool */
55+
private $cacheLoaded = false;
56+
5457
/** @var bool */
5558
private $refreshed = false;
5659

@@ -74,7 +77,6 @@ public function __construct()
7477
*/
7578
public function register(bool $prepend = false): self
7679
{
77-
$this->loadCache();
7880
spl_autoload_register([$this, 'tryLoad'], true, $prepend);
7981
return $this;
8082
}
@@ -85,6 +87,7 @@ public function register(bool $prepend = false): self
8587
*/
8688
public function tryLoad(string $type): void
8789
{
90+
$this->loadCache();
8891
$type = ltrim($type, '\\'); // PHP namespace bug #49143
8992
$info = $this->classes[$type] ?? null;
9093

@@ -158,6 +161,7 @@ public function excludeDirectory(...$paths): self
158161
*/
159162
public function getIndexedClasses(): array
160163
{
164+
$this->loadCache();
161165
$res = [];
162166
foreach ($this->classes as $class => $info) {
163167
$res[$class] = $info['file'];
@@ -171,6 +175,7 @@ public function getIndexedClasses(): array
171175
*/
172176
public function rebuild(): void
173177
{
178+
$this->cacheLoaded = true;
174179
$this->classes = $this->missing = [];
175180
$this->refreshClasses();
176181
if ($this->tempDirectory) {
@@ -410,6 +415,11 @@ public function setTempDirectory(string $dir): self
410415
*/
411416
private function loadCache(): void
412417
{
418+
if ($this->cacheLoaded) {
419+
return;
420+
}
421+
$this->cacheLoaded = true;
422+
413423
$file = $this->getCacheFile();
414424

415425
// Solving atomicity to work everywhere is really pain in the ass.

tests/Loaders/RobotLoader.phar.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,14 @@ Assert::true(class_exists('D'));
4545
$loader = new RobotLoader;
4646
$loader->setTempDirectory(getTempDir());
4747
$loader->addDirectory("phar://$pharFile/non-dir");
48-
Assert::exception(function () use ($loader, $pharFile) {
49-
$loader->register();
48+
Assert::exception(function () use ($loader) {
49+
$loader->rebuild();
5050
}, Nette\IOException::class, "File or directory 'phar://$pharFile/non-dir' not found.");
5151

5252

5353
$loader = new RobotLoader;
5454
$loader->setTempDirectory(getTempDir());
5555
$loader->addDirectory("phar://$pharFile/non-file.php");
56-
Assert::exception(function () use ($loader, $pharFile) {
57-
$loader->register();
56+
Assert::exception(function () use ($loader) {
57+
$loader->rebuild();
5858
}, Nette\IOException::class, "File or directory 'phar://$pharFile/non-file.php' not found.");

tests/Loaders/RobotLoader.rebuild.phpt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ file_put_contents($dir . '/file2.php', '<?php class B {}');
2121
$loader = new RobotLoader;
2222
$loader->setTempDirectory(getTempDir());
2323
$loader->addDirectory($dir);
24-
$loader->register(); // rebuilds cache
24+
$loader->register();
25+
class_exists('x'); // rebuilds cache
2526

2627
rename($dir . '/file1.php', $dir . '/file3.php');
2728

0 commit comments

Comments
 (0)