Skip to content

Commit

Permalink
Loads and builds cache on demand
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jul 10, 2020
1 parent d38c688 commit 58a999b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
12 changes: 11 additions & 1 deletion src/RobotLoader/RobotLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class RobotLoader
/** @var array of class => [file, time] */
private $classes = [];

/** @var bool */
private $cacheLoaded = false;

/** @var bool */
private $refreshed = false;

Expand All @@ -74,7 +77,6 @@ public function __construct()
*/
public function register(bool $prepend = false): self
{
$this->loadCache();
spl_autoload_register([$this, 'tryLoad'], true, $prepend);
return $this;
}
Expand All @@ -85,6 +87,7 @@ public function register(bool $prepend = false): self
*/
public function tryLoad(string $type): void
{
$this->loadCache();
$type = ltrim($type, '\\'); // PHP namespace bug #49143
$info = $this->classes[$type] ?? null;

Expand Down Expand Up @@ -158,6 +161,7 @@ public function excludeDirectory(...$paths): self
*/
public function getIndexedClasses(): array
{
$this->loadCache();
$res = [];
foreach ($this->classes as $class => $info) {
$res[$class] = $info['file'];
Expand All @@ -171,6 +175,7 @@ public function getIndexedClasses(): array
*/
public function rebuild(): void
{
$this->cacheLoaded = true;
$this->classes = $this->missing = [];
$this->refreshClasses();
if ($this->tempDirectory) {
Expand Down Expand Up @@ -410,6 +415,11 @@ public function setTempDirectory(string $dir): self
*/
private function loadCache(): void
{
if ($this->cacheLoaded) {
return;
}
$this->cacheLoaded = true;

$file = $this->getCacheFile();

// Solving atomicity to work everywhere is really pain in the ass.
Expand Down
8 changes: 4 additions & 4 deletions tests/Loaders/RobotLoader.phar.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ Assert::true(class_exists('D'));
$loader = new RobotLoader;
$loader->setTempDirectory(getTempDir());
$loader->addDirectory("phar://$pharFile/non-dir");
Assert::exception(function () use ($loader, $pharFile) {
$loader->register();
Assert::exception(function () use ($loader) {
$loader->rebuild();
}, Nette\IOException::class, "File or directory 'phar://$pharFile/non-dir' not found.");


$loader = new RobotLoader;
$loader->setTempDirectory(getTempDir());
$loader->addDirectory("phar://$pharFile/non-file.php");
Assert::exception(function () use ($loader, $pharFile) {
$loader->register();
Assert::exception(function () use ($loader) {
$loader->rebuild();
}, Nette\IOException::class, "File or directory 'phar://$pharFile/non-file.php' not found.");
3 changes: 2 additions & 1 deletion tests/Loaders/RobotLoader.rebuild.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ file_put_contents($dir . '/file2.php', '<?php class B {}');
$loader = new RobotLoader;
$loader->setTempDirectory(getTempDir());
$loader->addDirectory($dir);
$loader->register(); // rebuilds cache
$loader->register();
class_exists('x'); // rebuilds cache

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

Expand Down

0 comments on commit 58a999b

Please sign in to comment.