From 6145d8453290ba02d0c96689e87da473011af85a Mon Sep 17 00:00:00 2001 From: Frank de Jonge Date: Fri, 9 Sep 2022 10:41:48 +0200 Subject: [PATCH] Turn path prefixing into an adapter decorator --- .../PathPrefixedAdapter.php} | 101 +++++++++--------- .../PathPrefixedAdapterTest.php} | 55 +++++----- 2 files changed, 80 insertions(+), 76 deletions(-) rename src/{PathPrefixedFilesystem.php => PathPrefixing/PathPrefixedAdapter.php} (51%) rename src/{PathPrefixedFilesystemTest.php => PathPrefixing/PathPrefixedAdapterTest.php} (51%) diff --git a/src/PathPrefixedFilesystem.php b/src/PathPrefixing/PathPrefixedAdapter.php similarity index 51% rename from src/PathPrefixedFilesystem.php rename to src/PathPrefixing/PathPrefixedAdapter.php index fdb0609eb..d33bb825a 100644 --- a/src/PathPrefixedFilesystem.php +++ b/src/PathPrefixing/PathPrefixedAdapter.php @@ -1,36 +1,47 @@ filesystem = $filesystem; + $this->adapter = $adapter; $this->prefix = new PathPrefixer($prefix); } - public function has(string $location): bool - { - return $this->filesystem->has($this->preparePath($location)); - } - public function read(string $location): string { try { - return $this->filesystem->read($this->preparePath($location)); + return $this->adapter->read($this->prefix->prefixPath($location)); } catch (Throwable $previous) { throw UnableToReadFile::fromLocation($location, $previous->getMessage(), $previous); } @@ -39,23 +50,23 @@ public function read(string $location): string public function readStream(string $location) { try { - return $this->filesystem->readStream($this->preparePath($location)); + return $this->adapter->readStream($this->prefix->prefixPath($location)); } catch (Throwable $previous) { throw UnableToReadFile::fromLocation($location, $previous->getMessage(), $previous); } } - public function listContents(string $location, bool $deep = self::LIST_SHALLOW): DirectoryListing + public function listContents(string $location, bool $deep): Generator { - return $this->filesystem->listContents($this->preparePath($location), $deep)->map( - fn(StorageAttributes $attributes) => $attributes->withPath($this->stripPath($attributes->path())) - ); + foreach ($this->adapter->listContents($this->prefix->prefixPath($location), $deep) as $attributes) { + yield $attributes->withPath($this->prefix->stripPrefix($attributes->path())); + } } public function fileExists(string $location): bool { try { - return $this->filesystem->fileExists($this->preparePath($location)); + return $this->adapter->fileExists($this->prefix->prefixPath($location)); } catch (Throwable $previous) { throw UnableToCheckFileExistence::forLocation($location, $previous); } @@ -64,61 +75,61 @@ public function fileExists(string $location): bool public function directoryExists(string $location): bool { try { - return $this->filesystem->directoryExists($this->preparePath($location)); + return $this->adapter->directoryExists($this->prefix->prefixPath($location)); } catch (Throwable $previous) { throw UnableToCheckDirectoryExistence::forLocation($location, $previous); } } - public function lastModified(string $path): int + public function lastModified(string $path): FileAttributes { try { - return $this->filesystem->lastModified($this->preparePath($path)); + return $this->adapter->lastModified($this->prefix->prefixPath($path)); } catch (Throwable $previous) { throw UnableToRetrieveMetadata::lastModified($path, $previous->getMessage(), $previous); } } - public function fileSize(string $path): int + public function fileSize(string $path): FileAttributes { try { - return $this->filesystem->fileSize($this->preparePath($path)); + return $this->adapter->fileSize($this->prefix->prefixPath($path)); } catch (Throwable $previous) { throw UnableToRetrieveMetadata::fileSize($path, $previous->getMessage(), $previous); } } - public function mimeType(string $path): string + public function mimeType(string $path): FileAttributes { try { - return $this->filesystem->mimeType($this->preparePath($path)); + return $this->adapter->mimeType($this->prefix->prefixPath($path)); } catch (Throwable $previous) { throw UnableToRetrieveMetadata::mimeType($path, $previous->getMessage(), $previous); } } - public function visibility(string $path): string + public function visibility(string $path): FileAttributes { try { - return $this->filesystem->visibility($this->preparePath($path)); + return $this->adapter->visibility($this->prefix->prefixPath($path)); } catch (Throwable $previous) { throw UnableToRetrieveMetadata::visibility($path, $previous->getMessage(), $previous); } } - public function write(string $location, string $contents, array $config = []): void + public function write(string $location, string $contents, Config $config): void { try { - $this->filesystem->write($this->preparePath($location), $contents, $config); + $this->adapter->write($this->prefix->prefixPath($location), $contents, $config); } catch (Throwable $previous) { throw UnableToWriteFile::atLocation($location, $previous->getMessage(), $previous); } } - public function writeStream(string $location, $contents, array $config = []): void + public function writeStream(string $location, $contents, Config $config): void { try { - $this->filesystem->writeStream($this->preparePath($location), $contents, $config); + $this->adapter->writeStream($this->prefix->prefixPath($location), $contents, $config); } catch (Throwable $previous) { throw UnableToWriteFile::atLocation($location, $previous->getMessage(), $previous); } @@ -127,7 +138,7 @@ public function writeStream(string $location, $contents, array $config = []): vo public function setVisibility(string $path, string $visibility): void { try { - $this->filesystem->setVisibility($this->preparePath($path), $visibility); + $this->adapter->setVisibility($this->prefix->prefixPath($path), $visibility); } catch (Throwable $previous) { throw UnableToSetVisibility::atLocation($path, $previous->getMessage(), $previous); } @@ -136,7 +147,7 @@ public function setVisibility(string $path, string $visibility): void public function delete(string $location): void { try { - $this->filesystem->delete($this->preparePath($location)); + $this->adapter->delete($this->prefix->prefixPath($location)); } catch (Throwable $previous) { throw UnableToDeleteFile::atLocation($location, $previous->getMessage(), $previous); } @@ -145,46 +156,36 @@ public function delete(string $location): void public function deleteDirectory(string $location): void { try { - $this->filesystem->deleteDirectory($this->preparePath($location)); + $this->adapter->deleteDirectory($this->prefix->prefixPath($location)); } catch (Throwable $previous) { throw UnableToDeleteDirectory::atLocation($location, $previous->getMessage(), $previous); } } - public function createDirectory(string $location, array $config = []): void + public function createDirectory(string $location, Config $config): void { try { - $this->filesystem->createDirectory($this->preparePath($location), $config); + $this->adapter->createDirectory($this->prefix->prefixPath($location), $config); } catch (Throwable $previous) { throw UnableToCreateDirectory::atLocation($location, $previous->getMessage(), $previous); } } - public function move(string $source, string $destination, array $config = []): void + public function move(string $source, string $destination, Config $config): void { try { - $this->filesystem->move($this->preparePath($source), $this->preparePath($destination), $config); + $this->adapter->move($this->prefix->prefixPath($source), $this->prefix->prefixPath($destination), $config); } catch (Throwable $previous) { throw UnableToMoveFile::fromLocationTo($source, $destination, $previous); } } - public function copy(string $source, string $destination, array $config = []): void + public function copy(string $source, string $destination, Config $config): void { try { - $this->filesystem->copy($this->preparePath($source), $this->preparePath($destination), $config); + $this->adapter->copy($this->prefix->prefixPath($source), $this->prefix->prefixPath($destination), $config); } catch (Throwable $previous) { throw UnableToCopyFile::fromLocationTo($source, $destination, $previous); } } - - private function stripPath(string $path): string - { - return $this->prefix->stripPrefix($path); - } - - private function preparePath(string $path): string - { - return $this->prefix->prefixPath($path); - } } diff --git a/src/PathPrefixedFilesystemTest.php b/src/PathPrefixing/PathPrefixedAdapterTest.php similarity index 51% rename from src/PathPrefixedFilesystemTest.php rename to src/PathPrefixing/PathPrefixedAdapterTest.php index ff88ed1a0..35f7a5892 100644 --- a/src/PathPrefixedFilesystemTest.php +++ b/src/PathPrefixing/PathPrefixedAdapterTest.php @@ -1,63 +1,66 @@ write('foo.txt', 'bla'); + $prefix->write('foo.txt', 'bla', new Config); static::assertTrue($prefix->fileExists('foo.txt')); - static::assertTrue($prefix->has('foo.txt')); static::assertFalse($prefix->directoryExists('foo.txt')); - static::assertTrue($fs->has('foo/foo.txt')); - static::assertFalse($fs->directoryExists('foo/foo.txt')); + static::assertTrue($adapter->fileExists('foo/foo.txt')); + static::assertFalse($adapter->directoryExists('foo/foo.txt')); static::assertSame('bla', $prefix->read('foo.txt')); static::assertSame('bla', stream_get_contents($prefix->readStream('foo.txt'))); - static::assertSame('text/plain', $prefix->mimeType('foo.txt')); - static::assertSame(3, $prefix->fileSize('foo.txt')); - static::assertSame(Visibility::PUBLIC, $prefix->visibility('foo.txt')); + static::assertSame('text/plain', $prefix->mimeType('foo.txt')->mimeType()); + static::assertSame(3, $prefix->fileSize('foo.txt')->fileSize()); + static::assertSame(Visibility::PUBLIC, $prefix->visibility('foo.txt')->visibility()); $prefix->setVisibility('foo.txt', Visibility::PRIVATE); - static::assertSame(Visibility::PRIVATE, $prefix->visibility('foo.txt')); - static::assertEqualsWithDelta($prefix->lastModified('foo.txt'), time(), 2); + static::assertSame(Visibility::PRIVATE, $prefix->visibility('foo.txt')->visibility()); + static::assertEqualsWithDelta($prefix->lastModified('foo.txt')->lastModified(), time(), 2); - $prefix->copy('foo.txt', 'bla.txt'); - static::assertTrue($prefix->has('bla.txt')); + $prefix->copy('foo.txt', 'bla.txt', new Config); + static::assertTrue($prefix->fileExists('bla.txt')); - $prefix->createDirectory('dir'); + $prefix->createDirectory('dir', new Config()); static::assertTrue($prefix->directoryExists('dir')); static::assertFalse($prefix->directoryExists('dir2')); $prefix->deleteDirectory('dir'); static::assertFalse($prefix->directoryExists('dir')); - $prefix->move('bla.txt', 'bla2.txt'); - static::assertFalse($prefix->has('bla.txt')); - static::assertTrue($prefix->has('bla2.txt')); + $prefix->move('bla.txt', 'bla2.txt', new Config()); + static::assertFalse($prefix->fileExists('bla.txt')); + static::assertTrue($prefix->fileExists('bla2.txt')); $prefix->delete('bla2.txt'); - static::assertFalse($prefix->has('bla2.txt')); + static::assertFalse($prefix->fileExists('bla2.txt')); - $prefix->createDirectory('test'); + $prefix->createDirectory('test', new Config()); - $files = $prefix->listContents('', true)->toArray(); + $files = iterator_to_array($prefix->listContents('', true)); static::assertCount(2, $files); } public function testWriteStream(): void { - $fs = new Filesystem(new InMemoryFilesystemAdapter()); - $prefix = new PathPrefixedFilesystem($fs, 'foo'); + $adapter = new InMemoryFilesystemAdapter(); + $prefix = new PathPrefixedAdapter($adapter, 'foo'); $tmpFile = sys_get_temp_dir() . '/' . uniqid('test', true); file_put_contents($tmpFile, 'test'); - $prefix->writeStream('a.txt', fopen($tmpFile, 'rb')); + $prefix->writeStream('a.txt', fopen($tmpFile, 'rb'), new Config()); static::assertTrue($prefix->fileExists('a.txt')); static::assertSame('test', $prefix->read('a.txt')); @@ -69,6 +72,6 @@ public function testWriteStream(): void public function testEmptyPrefix(): void { static::expectException(\InvalidArgumentException::class); - new PathPrefixedFilesystem(new Filesystem(new InMemoryFilesystemAdapter()), ''); + new PathPrefixedAdapter(new InMemoryFilesystemAdapter(), ''); } }