Skip to content

Commit

Permalink
fix loading directory tree upon initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptouuuu committed Feb 27, 2021
1 parent aded977 commit 23e9c1f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Directory/Directory.php
Expand Up @@ -73,6 +73,26 @@ public static function named(string $name): self
return new self(new Name($name));
}

/**
* @internal
*
* @param Set<File> $files
*/
public static function defer(Name $name, Set $files): self
{
assertSet(File::class, $files, 2);

$self = new self($name);
// we hijack the constructors to prevent checking for duplicates when
// using a deferred set of files as it will trigger to load the whole
// directory tree, it's kinda safe to do this as this method should
// only be used within the filesystem adapter and there should be no
// duplicates on a concrete filesystem
$self->files = $files;

return $self;
}

public function name(): Name
{
return $this->name;
Expand Down
22 changes: 22 additions & 0 deletions tests/Directory/DirectoryTest.php
Expand Up @@ -418,6 +418,28 @@ public function testDirectoryLoadedWithDifferentFilesWithTheSameNameThrows()
});
}

public function testDeferringTheLoadingOfADirectoryDoesntLoadFiles()
{
$this
->forAll(FName::any())
->then(function($name) {
$this->assertInstanceOf(
Directory::class,
Directory::defer(
$name,
Set::defer(
File::class,
(function() {
throw new \Exception;

yield false;
})(),
),
),
);
});
}

public function properties(): iterable
{
foreach (PDirectory::list() as $property) {
Expand Down

0 comments on commit 23e9c1f

Please sign in to comment.