Skip to content

Commit

Permalink
Allow dynamic (immmutable) extension of the mount manager
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Dec 3, 2023
1 parent 582d1ec commit a013d25
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/MountManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ public function __construct(array $filesystems = [], array $config = [])
$this->config = new Config($config);
}

/**
* @param array<string,FilesystemOperator> $filesystems
*/
public function extend(array $filesystems, array $config = []): MountManager
{
$clone = clone $this;
$clone->config = $this->config->extend($config);
$clone->mountFilesystems($filesystems);

return $clone;
}

public function fileExists(string $location): bool
{
/** @var FilesystemOperator $filesystem */
Expand Down Expand Up @@ -329,7 +341,7 @@ private function mountFilesystem(string $key, FilesystemOperator $filesystem): v
/**
* @param string $path
*
* @return array{0:FilesystemOperator, 1:string}
* @return array{0:FilesystemOperator, 1:string, 2:string}
*/
private function determineFilesystemAndPath(string $path): array
{
Expand Down
52 changes: 52 additions & 0 deletions src/MountManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,58 @@ public function copying_without_retaining_visibility(): void
self::assertEquals('private', $visibility);
}

/**
* @test
*/
public function extending_without_new_mounts_is_equal_but_not_the_same(): void
{
$mountManager = $this->mountManager->extend([]);

$this->assertNotSame($this->mountManager, $mountManager);
$this->assertEquals($this->mountManager, $mountManager);
}

/**
* @test
*/
public function extending_with_new_mounts_is_not_equal(): void
{
$mountManager = $this->mountManager->extend([
'third' => new Filesystem(new InMemoryFilesystemAdapter()),
]);

$this->assertNotEquals($this->mountManager, $mountManager);
}

/**
* @test
*/
public function extending_exposes_a_usable_mount_on_the_extension(): void
{
$mountManager = $this->mountManager->extend([
'third' => new Filesystem(new InMemoryFilesystemAdapter()),
]);

$mountManager->write('third://path.txt', 'this');
$contents = $mountManager->read('third://path.txt');

$this->assertEquals('this', $contents);
}

/**
* @test
*/
public function extending_does_not_mount_on_the_original_mount_manager(): void
{
$this->mountManager->extend([
'third' => new Filesystem(new InMemoryFilesystemAdapter()),
]);

$this->expectException(UnableToResolveFilesystemMount::class);

$this->mountManager->write('third://path.txt', 'this');
}

/**
* @test
*/
Expand Down

0 comments on commit a013d25

Please sign in to comment.