From 305526b671941cf35037b25d04d4a6f77a98662e Mon Sep 17 00:00:00 2001 From: Joshua Estes Date: Wed, 25 Oct 2023 14:58:12 -0400 Subject: [PATCH] various updates and changes --- .../Filesystem/Adapter/AdapterInterface.php | 2 ++ .../Filesystem/Adapter/ChainAdapter.php | 33 +++++++++++++++++++ .../Filesystem/Adapter/NativeAdapter.php | 15 +++++++++ .../Filesystem/Adapter/NullAdapter.php | 19 +++++++---- .../Filesystem/Adapter/ReadOnlyAdapter.php | 15 +++++++++ .../Filesystem/Adapter/WormAdapter.php | 23 +++++++++++-- .../Exception/FileNotFoundException.php | 4 +-- .../Exception/FilesystemException.php | 4 +-- .../Exception/UnableToCopyFileException.php | 4 +-- .../UnableToDeleteDirectoryException.php | 4 +-- .../Exception/UnableToDeleteFileException.php | 4 +-- .../Exception/UnableToMoveFileException.php | 4 +-- .../Exception/UnableToReadFileException.php | 4 +-- .../Exception/UnableToWriteFileException.php | 4 +-- 14 files changed, 107 insertions(+), 32 deletions(-) diff --git a/src/SonsOfPHP/Component/Filesystem/Adapter/AdapterInterface.php b/src/SonsOfPHP/Component/Filesystem/Adapter/AdapterInterface.php index e79f17aa..878721fc 100644 --- a/src/SonsOfPHP/Component/Filesystem/Adapter/AdapterInterface.php +++ b/src/SonsOfPHP/Component/Filesystem/Adapter/AdapterInterface.php @@ -46,6 +46,8 @@ public function copy(string $source, string $destination): void; public function move(string $source, string $destination): void; public function exists(string $path): bool; + public function isFile(string $filename): bool; + public function isDirectory(string $path): bool; } diff --git a/src/SonsOfPHP/Component/Filesystem/Adapter/ChainAdapter.php b/src/SonsOfPHP/Component/Filesystem/Adapter/ChainAdapter.php index c62eeeae..6c757948 100644 --- a/src/SonsOfPHP/Component/Filesystem/Adapter/ChainAdapter.php +++ b/src/SonsOfPHP/Component/Filesystem/Adapter/ChainAdapter.php @@ -50,4 +50,37 @@ public function move(string $source, string $destination): void $this->adapter->move($source, $destination); } } + + public function exists(string $path): bool + { + foreach ($this->adapters as $adapter) { + if ($this->adapter->exists($path)) { + return true; + } + } + + return false; + } + + public function isFile(string $filename): bool + { + foreach ($this->adapters as $adapter) { + if ($this->adapter->isFile($path)) { + return true; + } + } + + return false; + } + + public function isDirectory(string $path): bool + { + foreach ($this->adapters as $adapter) { + if ($this->adapter->isDirectory($path)) { + return true; + } + } + + return false; + } } diff --git a/src/SonsOfPHP/Component/Filesystem/Adapter/NativeAdapter.php b/src/SonsOfPHP/Component/Filesystem/Adapter/NativeAdapter.php index bb80161c..43e58df7 100644 --- a/src/SonsOfPHP/Component/Filesystem/Adapter/NativeAdapter.php +++ b/src/SonsOfPHP/Component/Filesystem/Adapter/NativeAdapter.php @@ -39,4 +39,19 @@ public function move(string $source, string $destination): void { rename($this->prefix . $source, $this->prefix . $destination); } + + public function exists(string $path): bool + { + return $this->isFile($path) || $this->isDirectory($path); + } + + public function isFile(string $filename): bool + { + return is_file($filename); + } + + public function isDirectory(string $path): bool + { + return is_dir($filename); + } } diff --git a/src/SonsOfPHP/Component/Filesystem/Adapter/NullAdapter.php b/src/SonsOfPHP/Component/Filesystem/Adapter/NullAdapter.php index 0142fa7b..317fa6fd 100644 --- a/src/SonsOfPHP/Component/Filesystem/Adapter/NullAdapter.php +++ b/src/SonsOfPHP/Component/Filesystem/Adapter/NullAdapter.php @@ -12,24 +12,31 @@ */ final class NullAdapter implements AdapterInterface { - public function write(string $path, mixed $contents): void - { - } + public function write(string $path, mixed $contents): void {} public function read(string $path): string { return ''; } - public function delete(string $path): void + public function delete(string $path): void {} + + public function copy(string $source, string $destination): void {} + + public function move(string $source, string $destination): void {} + + public function exists(string $path): bool { + return false; } - public function copy(string $source, string $destination): void + public function isFile(string $filename): bool { + return false; } - public function move(string $source, string $destination): void + public function isDirectory(string $path): bool { + return false; } } diff --git a/src/SonsOfPHP/Component/Filesystem/Adapter/ReadOnlyAdapter.php b/src/SonsOfPHP/Component/Filesystem/Adapter/ReadOnlyAdapter.php index 2dd627c9..e158ada9 100644 --- a/src/SonsOfPHP/Component/Filesystem/Adapter/ReadOnlyAdapter.php +++ b/src/SonsOfPHP/Component/Filesystem/Adapter/ReadOnlyAdapter.php @@ -39,4 +39,19 @@ public function move(string $source, string $destination): void { throw new FilesystemException(); } + + public function exists(string $path): bool + { + return $this->adapter->exists($path); + } + + public function isFile(string $filename): bool + { + return $this->adapter->isFile($filename); + } + + public function isDirectory(string $path): bool + { + return $this->adapter->isDirectory($path); + } } diff --git a/src/SonsOfPHP/Component/Filesystem/Adapter/WormAdapter.php b/src/SonsOfPHP/Component/Filesystem/Adapter/WormAdapter.php index 23a77bd5..17fad4ac 100644 --- a/src/SonsOfPHP/Component/Filesystem/Adapter/WormAdapter.php +++ b/src/SonsOfPHP/Component/Filesystem/Adapter/WormAdapter.php @@ -21,7 +21,9 @@ public function __construct( public function write(string $path, mixed $contents): void { - // @todo throw exception if file already exists + if ($this->isFile($path)) { + throw new FilesystemException(); + } $this->adapter->write($path, $contents); } @@ -38,7 +40,9 @@ public function delete(string $path): void public function copy(string $source, string $destination): void { - // @todo throw exception if destination exists + if ($this->isFile($destination)) { + throw new FilesystemException(); + } $this->adapter->copy($source, $destination); } @@ -47,4 +51,19 @@ public function move(string $source, string $destination): void { throw new FilesystemException(); } + + public function exists(string $path): bool + { + return $this->adapter->exists($path); + } + + public function isFile(string $filename): bool + { + return $this->adapter->isFile($filename); + } + + public function isDirectory(string $path): bool + { + return $this->adapter->isDirectory($path); + } } diff --git a/src/SonsOfPHP/Component/Filesystem/Exception/FileNotFoundException.php b/src/SonsOfPHP/Component/Filesystem/Exception/FileNotFoundException.php index e922e826..e7261c6c 100644 --- a/src/SonsOfPHP/Component/Filesystem/Exception/FileNotFoundException.php +++ b/src/SonsOfPHP/Component/Filesystem/Exception/FileNotFoundException.php @@ -7,6 +7,4 @@ /** * @author Joshua Estes */ -final class FileNotFoundException extends FilesystemException -{ -} +final class FileNotFoundException extends FilesystemException {} diff --git a/src/SonsOfPHP/Component/Filesystem/Exception/FilesystemException.php b/src/SonsOfPHP/Component/Filesystem/Exception/FilesystemException.php index 77e12f3e..75811121 100644 --- a/src/SonsOfPHP/Component/Filesystem/Exception/FilesystemException.php +++ b/src/SonsOfPHP/Component/Filesystem/Exception/FilesystemException.php @@ -7,6 +7,4 @@ /** * @author Joshua Estes */ -class FilesystemException extends \Exception -{ -} +class FilesystemException extends \Exception {} diff --git a/src/SonsOfPHP/Component/Filesystem/Exception/UnableToCopyFileException.php b/src/SonsOfPHP/Component/Filesystem/Exception/UnableToCopyFileException.php index 2da2cb92..3c6bb5d0 100644 --- a/src/SonsOfPHP/Component/Filesystem/Exception/UnableToCopyFileException.php +++ b/src/SonsOfPHP/Component/Filesystem/Exception/UnableToCopyFileException.php @@ -7,6 +7,4 @@ /** * @author Joshua Estes */ -final class UnableToCopyFileException extends FilesystemException -{ -} +final class UnableToCopyFileException extends FilesystemException {} diff --git a/src/SonsOfPHP/Component/Filesystem/Exception/UnableToDeleteDirectoryException.php b/src/SonsOfPHP/Component/Filesystem/Exception/UnableToDeleteDirectoryException.php index c003af36..de0f9eec 100644 --- a/src/SonsOfPHP/Component/Filesystem/Exception/UnableToDeleteDirectoryException.php +++ b/src/SonsOfPHP/Component/Filesystem/Exception/UnableToDeleteDirectoryException.php @@ -7,6 +7,4 @@ /** * @author Joshua Estes */ -final class UnableToDeleteDirectoryException extends FilesystemException -{ -} +final class UnableToDeleteDirectoryException extends FilesystemException {} diff --git a/src/SonsOfPHP/Component/Filesystem/Exception/UnableToDeleteFileException.php b/src/SonsOfPHP/Component/Filesystem/Exception/UnableToDeleteFileException.php index 1c855135..7299316e 100644 --- a/src/SonsOfPHP/Component/Filesystem/Exception/UnableToDeleteFileException.php +++ b/src/SonsOfPHP/Component/Filesystem/Exception/UnableToDeleteFileException.php @@ -7,6 +7,4 @@ /** * @author Joshua Estes */ -final class UnableToDeleteFileException extends FilesystemException -{ -} +final class UnableToDeleteFileException extends FilesystemException {} diff --git a/src/SonsOfPHP/Component/Filesystem/Exception/UnableToMoveFileException.php b/src/SonsOfPHP/Component/Filesystem/Exception/UnableToMoveFileException.php index dd7bb226..abf0e2fd 100644 --- a/src/SonsOfPHP/Component/Filesystem/Exception/UnableToMoveFileException.php +++ b/src/SonsOfPHP/Component/Filesystem/Exception/UnableToMoveFileException.php @@ -7,6 +7,4 @@ /** * @author Joshua Estes */ -final class UnableToMoveFileException extends FilesystemException -{ -} +final class UnableToMoveFileException extends FilesystemException {} diff --git a/src/SonsOfPHP/Component/Filesystem/Exception/UnableToReadFileException.php b/src/SonsOfPHP/Component/Filesystem/Exception/UnableToReadFileException.php index ad1a248c..176a7bf2 100644 --- a/src/SonsOfPHP/Component/Filesystem/Exception/UnableToReadFileException.php +++ b/src/SonsOfPHP/Component/Filesystem/Exception/UnableToReadFileException.php @@ -7,6 +7,4 @@ /** * @author Joshua Estes */ -final class UnableToReadFileException extends FilesystemException -{ -} +final class UnableToReadFileException extends FilesystemException {} diff --git a/src/SonsOfPHP/Component/Filesystem/Exception/UnableToWriteFileException.php b/src/SonsOfPHP/Component/Filesystem/Exception/UnableToWriteFileException.php index e5254a96..5cf4d127 100644 --- a/src/SonsOfPHP/Component/Filesystem/Exception/UnableToWriteFileException.php +++ b/src/SonsOfPHP/Component/Filesystem/Exception/UnableToWriteFileException.php @@ -7,6 +7,4 @@ /** * @author Joshua Estes */ -final class UnableToWriteFileException extends FilesystemException -{ -} +final class UnableToWriteFileException extends FilesystemException {}