From a013d2591e5bac733475b445664860c61ca769d0 Mon Sep 17 00:00:00 2001 From: Frank de Jonge Date: Sun, 3 Dec 2023 19:19:21 +0100 Subject: [PATCH] Allow dynamic (immmutable) extension of the mount manager --- src/MountManager.php | 14 ++++++++++- src/MountManagerTest.php | 52 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/src/MountManager.php b/src/MountManager.php index 0fa7d8903..66d5f052a 100644 --- a/src/MountManager.php +++ b/src/MountManager.php @@ -33,6 +33,18 @@ public function __construct(array $filesystems = [], array $config = []) $this->config = new Config($config); } + /** + * @param array $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 */ @@ -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 { diff --git a/src/MountManagerTest.php b/src/MountManagerTest.php index a28994cde..301b192fd 100644 --- a/src/MountManagerTest.php +++ b/src/MountManagerTest.php @@ -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 */