Skip to content

Commit a013d25

Browse files
committed
Allow dynamic (immmutable) extension of the mount manager
1 parent 582d1ec commit a013d25

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/MountManager.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ public function __construct(array $filesystems = [], array $config = [])
3333
$this->config = new Config($config);
3434
}
3535

36+
/**
37+
* @param array<string,FilesystemOperator> $filesystems
38+
*/
39+
public function extend(array $filesystems, array $config = []): MountManager
40+
{
41+
$clone = clone $this;
42+
$clone->config = $this->config->extend($config);
43+
$clone->mountFilesystems($filesystems);
44+
45+
return $clone;
46+
}
47+
3648
public function fileExists(string $location): bool
3749
{
3850
/** @var FilesystemOperator $filesystem */
@@ -329,7 +341,7 @@ private function mountFilesystem(string $key, FilesystemOperator $filesystem): v
329341
/**
330342
* @param string $path
331343
*
332-
* @return array{0:FilesystemOperator, 1:string}
344+
* @return array{0:FilesystemOperator, 1:string, 2:string}
333345
*/
334346
private function determineFilesystemAndPath(string $path): array
335347
{

src/MountManagerTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,58 @@ public function copying_without_retaining_visibility(): void
7676
self::assertEquals('private', $visibility);
7777
}
7878

79+
/**
80+
* @test
81+
*/
82+
public function extending_without_new_mounts_is_equal_but_not_the_same(): void
83+
{
84+
$mountManager = $this->mountManager->extend([]);
85+
86+
$this->assertNotSame($this->mountManager, $mountManager);
87+
$this->assertEquals($this->mountManager, $mountManager);
88+
}
89+
90+
/**
91+
* @test
92+
*/
93+
public function extending_with_new_mounts_is_not_equal(): void
94+
{
95+
$mountManager = $this->mountManager->extend([
96+
'third' => new Filesystem(new InMemoryFilesystemAdapter()),
97+
]);
98+
99+
$this->assertNotEquals($this->mountManager, $mountManager);
100+
}
101+
102+
/**
103+
* @test
104+
*/
105+
public function extending_exposes_a_usable_mount_on_the_extension(): void
106+
{
107+
$mountManager = $this->mountManager->extend([
108+
'third' => new Filesystem(new InMemoryFilesystemAdapter()),
109+
]);
110+
111+
$mountManager->write('third://path.txt', 'this');
112+
$contents = $mountManager->read('third://path.txt');
113+
114+
$this->assertEquals('this', $contents);
115+
}
116+
117+
/**
118+
* @test
119+
*/
120+
public function extending_does_not_mount_on_the_original_mount_manager(): void
121+
{
122+
$this->mountManager->extend([
123+
'third' => new Filesystem(new InMemoryFilesystemAdapter()),
124+
]);
125+
126+
$this->expectException(UnableToResolveFilesystemMount::class);
127+
128+
$this->mountManager->write('third://path.txt', 'this');
129+
}
130+
79131
/**
80132
* @test
81133
*/

0 commit comments

Comments
 (0)