From 6cc2c077759eb49c246f540a32effbafcd9e6173 Mon Sep 17 00:00:00 2001 From: Joshua Estes Date: Sat, 24 Aug 2024 19:24:27 -0400 Subject: [PATCH] meh --- docs/components/filesystem/index.md | 3 +++ .../Filesystem/Adapter/ChainAdapter.php | 25 +++++++++++++++++++ .../Filesystem/Adapter/InMemoryAdapter.php | 10 ++++++++ .../Filesystem/Adapter/NativeAdapter.php | 22 ++++++++++++++++ .../Filesystem/Adapter/NullAdapter.php | 6 +++++ .../Filesystem/Adapter/ReadOnlyAdapter.php | 15 +++++++++++ .../Filesystem/Adapter/WormAdapter.php | 15 +++++++++++ .../Component/Filesystem/Filesystem.php | 5 ++++ .../Filesystem/Adapter/AdapterInterface.php | 5 ++++ .../Adapter/DirectoryAwareInterface.php | 4 +-- .../Filesystem/FilesystemInterface.php | 5 ++++ 11 files changed, 113 insertions(+), 2 deletions(-) diff --git a/docs/components/filesystem/index.md b/docs/components/filesystem/index.md index 0e3526d4..371a262b 100644 --- a/docs/components/filesystem/index.md +++ b/docs/components/filesystem/index.md @@ -37,6 +37,9 @@ $filesystem->delete('example.txt'); // Check if file exists $doesExist = $filesystem->exists('example.txt'); +// Get MIME Type of file +$mimeType = $filesystem->mimeType('example.txt'); + // Copy file $filesystem->copy('source.txt', 'destination.txt'); diff --git a/src/SonsOfPHP/Component/Filesystem/Adapter/ChainAdapter.php b/src/SonsOfPHP/Component/Filesystem/Adapter/ChainAdapter.php index bc586f0d..3ea2a3f6 100644 --- a/src/SonsOfPHP/Component/Filesystem/Adapter/ChainAdapter.php +++ b/src/SonsOfPHP/Component/Filesystem/Adapter/ChainAdapter.php @@ -10,6 +10,7 @@ use SonsOfPHP\Contract\Filesystem\Adapter\DirectoryAwareInterface; use SonsOfPHP\Contract\Filesystem\Adapter\MoveAwareInterface; use SonsOfPHP\Contract\Filesystem\ContextInterface; +use SonsOfPHP\Contract\Filesystem\Exception\FilesystemExceptionInterface; /** * Chain adapter allows you to use multiple adapters together. @@ -107,4 +108,28 @@ public function move(string $source, string $destination, ?ContextInterface $con $adapter->remove($source, $context); } } + + public function mimeType(string $path, ?ContextInterface $context = null): string + { + foreach ($this->adapters as $adapter) { + try { + return $adapter->mimeType($path, $context); + } catch (FilesystemExceptionInterface) { + } + } + } + + public function makeDirectory(string $path, ?ContextInterface $context = null): void + { + foreach ($this->adapters as $adapter) { + $adapter->makeDirectory($path, $context); + } + } + + public function removeDirectory(string $path, ?ContextInterface $context = null): void + { + foreach ($this->adapters as $adapter) { + $adapter->removeDirectory($path, $context); + } + } } diff --git a/src/SonsOfPHP/Component/Filesystem/Adapter/InMemoryAdapter.php b/src/SonsOfPHP/Component/Filesystem/Adapter/InMemoryAdapter.php index 849a40a9..c5d059c8 100644 --- a/src/SonsOfPHP/Component/Filesystem/Adapter/InMemoryAdapter.php +++ b/src/SonsOfPHP/Component/Filesystem/Adapter/InMemoryAdapter.php @@ -4,6 +4,7 @@ namespace SonsOfPHP\Component\Filesystem\Adapter; +use SonsOfPHP\Component\Filesystem\Exception\FilesystemException; use SonsOfPHP\Component\Filesystem\Exception\UnableToReadFileException; use SonsOfPHP\Contract\Filesystem\Adapter\AdapterInterface; use SonsOfPHP\Contract\Filesystem\Adapter\CopyAwareInterface; @@ -94,6 +95,15 @@ public function isDirectory(string $path, ?ContextInterface $context = null): bo return false; } + public function mimeType(string $path, ?ContextInterface $context = null): string + { + throw new FilesystemException('Not Supported'); + } + + public function makeDirectory(string $path, ?ContextInterface $context = null): void {} + + public function removeDirectory(string $path, ?ContextInterface $context = null): void {} + private function normalizePath(string $path): string { return ltrim($path, '/'); diff --git a/src/SonsOfPHP/Component/Filesystem/Adapter/NativeAdapter.php b/src/SonsOfPHP/Component/Filesystem/Adapter/NativeAdapter.php index 392a0fa7..180a071b 100644 --- a/src/SonsOfPHP/Component/Filesystem/Adapter/NativeAdapter.php +++ b/src/SonsOfPHP/Component/Filesystem/Adapter/NativeAdapter.php @@ -7,6 +7,7 @@ use SonsOfPHP\Component\Filesystem\Exception\FileNotFoundException; use SonsOfPHP\Component\Filesystem\Exception\FilesystemException; use SonsOfPHP\Component\Filesystem\Exception\UnableToCopyFileException; +use SonsOfPHP\Component\Filesystem\Exception\UnableToDeleteDirectoryException; use SonsOfPHP\Component\Filesystem\Exception\UnableToDeleteFileException; use SonsOfPHP\Component\Filesystem\Exception\UnableToMoveFileException; use SonsOfPHP\Component\Filesystem\Exception\UnableToWriteFileException; @@ -41,6 +42,10 @@ public function add(string $path, mixed $contents, ?ContextInterface $context = $this->makeDirectory(dirname($path), $context); + if (is_resource($contents)) { + $contents = stream_get_contents($contents, null, 0); + } + if (false === file_put_contents($this->prefix . '/' . ltrim($path, '/'), $contents)) { throw new UnableToWriteFileException('Unable to write file "' . $path . '"'); } @@ -107,10 +112,27 @@ public function makeDirectory(string $path, ?ContextInterface $context = null): } } + public function removeDirectory(string $path, ?ContextInterface $context = null): void + { + if ($this->isDirectory($path, $context) && false === rmdir($this->prefix . '/' . ltrim($path, '/'))) { + throw new UnableToDeleteDirectoryException('Unable to delete directory "' . $path . '"'); + } + } + public function move(string $source, string $destination, ?ContextInterface $context = null): void { if (false === rename($this->prefix . '/' . ltrim($source, '/'), $this->prefix . '/' . ltrim($destination, '/'))) { throw new UnableToMoveFileException('Unable to move file "' . $source . '" to "' . $destination . '"'); } } + + public function mimeType(string $path, ?ContextInterface $context = null): string + { + $mimeType = mime_content_type($this->prefix . '/' . ltrim($path, '/')); + if (false === $mimeType) { + throw new FilesystemException('Unable to guess MIME Type for "' . $path . '"'); + } + + return $mimeType; + } } diff --git a/src/SonsOfPHP/Component/Filesystem/Adapter/NullAdapter.php b/src/SonsOfPHP/Component/Filesystem/Adapter/NullAdapter.php index 303b7ea8..41b353e7 100644 --- a/src/SonsOfPHP/Component/Filesystem/Adapter/NullAdapter.php +++ b/src/SonsOfPHP/Component/Filesystem/Adapter/NullAdapter.php @@ -4,6 +4,7 @@ namespace SonsOfPHP\Component\Filesystem\Adapter; +use SonsOfPHP\Component\Filesystem\Exception\FilesystemException; use SonsOfPHP\Contract\Filesystem\Adapter\AdapterInterface; use SonsOfPHP\Contract\Filesystem\ContextInterface; @@ -36,4 +37,9 @@ public function isFile(string $path, ?ContextInterface $context = null): bool { return false; } + + public function mimeType(string $path, ?ContextInterface $context = null): string + { + throw new FilesystemException('Not Supported'); + } } diff --git a/src/SonsOfPHP/Component/Filesystem/Adapter/ReadOnlyAdapter.php b/src/SonsOfPHP/Component/Filesystem/Adapter/ReadOnlyAdapter.php index 8b639a35..3170fd03 100644 --- a/src/SonsOfPHP/Component/Filesystem/Adapter/ReadOnlyAdapter.php +++ b/src/SonsOfPHP/Component/Filesystem/Adapter/ReadOnlyAdapter.php @@ -69,4 +69,19 @@ public function move(string $source, string $destination, ?ContextInterface $con { throw new FilesystemException(); } + + public function mimeType(string $path, ?ContextInterface $context = null): string + { + return $this->adapter->mimeType($path, $context); + } + + public function makeDirectory(string $path, ?ContextInterface $context = null): void + { + throw new FilesystemException(); + } + + public function removeDirectory(string $path, ?ContextInterface $context = null): void + { + throw new FilesystemException(); + } } diff --git a/src/SonsOfPHP/Component/Filesystem/Adapter/WormAdapter.php b/src/SonsOfPHP/Component/Filesystem/Adapter/WormAdapter.php index 41cf9ce6..95307637 100644 --- a/src/SonsOfPHP/Component/Filesystem/Adapter/WormAdapter.php +++ b/src/SonsOfPHP/Component/Filesystem/Adapter/WormAdapter.php @@ -83,4 +83,19 @@ public function move(string $source, string $destination, ?ContextInterface $con { throw new FilesystemException(); } + + public function mimeType(string $path, ?ContextInterface $context = null): string + { + return $this->adapter->mimeType($path, $context); + } + + public function makeDirectory(string $path, ?ContextInterface $context = null): void + { + throw new FilesystemException(); + } + + public function removeDirectory(string $path, ?ContextInterface $context = null): void + { + throw new FilesystemException(); + } } diff --git a/src/SonsOfPHP/Component/Filesystem/Filesystem.php b/src/SonsOfPHP/Component/Filesystem/Filesystem.php index 5321df61..521eecc5 100644 --- a/src/SonsOfPHP/Component/Filesystem/Filesystem.php +++ b/src/SonsOfPHP/Component/Filesystem/Filesystem.php @@ -64,4 +64,9 @@ public function move(string $source, string $destination, ?ContextInterface $con $this->copy($source, $destination, $context); $this->delete($source, $context); } + + public function mimeType(string $path, ?ContextInterface $context = null): string + { + return $this->adapter->mimeType($path, $context); + } } diff --git a/src/SonsOfPHP/Contract/Filesystem/Adapter/AdapterInterface.php b/src/SonsOfPHP/Contract/Filesystem/Adapter/AdapterInterface.php index e062c339..c73bd0b0 100644 --- a/src/SonsOfPHP/Contract/Filesystem/Adapter/AdapterInterface.php +++ b/src/SonsOfPHP/Contract/Filesystem/Adapter/AdapterInterface.php @@ -57,4 +57,9 @@ public function has(string $path, ?ContextInterface $context = null): bool; * @throws FilesystemExceptionInterface Generic Failure Exception */ public function isFile(string $path, ?ContextInterface $context = null): bool; + + /** + * @throws FilesystemExceptionInterface Generic Failure Exception + */ + public function mimeType(string $path, ?ContextInterface $context = null): string; } diff --git a/src/SonsOfPHP/Contract/Filesystem/Adapter/DirectoryAwareInterface.php b/src/SonsOfPHP/Contract/Filesystem/Adapter/DirectoryAwareInterface.php index 95f001f2..b84e43ef 100644 --- a/src/SonsOfPHP/Contract/Filesystem/Adapter/DirectoryAwareInterface.php +++ b/src/SonsOfPHP/Contract/Filesystem/Adapter/DirectoryAwareInterface.php @@ -23,10 +23,10 @@ public function isDirectory(string $path, ?ContextInterface $context = null): bo /** * @throws FilesystemExceptionInterface Generic Failure Exception */ - //public function makeDirectory(string $path, ?ContextInterface $context = null): void; + public function makeDirectory(string $path, ?ContextInterface $context = null): void; /** * @throws FilesystemExceptionInterface Generic Failure Exception */ - //public function removeDirectory(string $path, ?ContextInterface $context = null): void; + public function removeDirectory(string $path, ?ContextInterface $context = null): void; } diff --git a/src/SonsOfPHP/Contract/Filesystem/FilesystemInterface.php b/src/SonsOfPHP/Contract/Filesystem/FilesystemInterface.php index 61069a0d..774639b4 100644 --- a/src/SonsOfPHP/Contract/Filesystem/FilesystemInterface.php +++ b/src/SonsOfPHP/Contract/Filesystem/FilesystemInterface.php @@ -63,4 +63,9 @@ public function move(string $source, string $destination, ?ContextInterface $con //public function isReadable(string $filename): bool; //public function isWritable(string $filename): bool; + + /** + * @throws FilesystemExceptionInterface Generic Failure Exception + */ + public function mimeType(string $path, ?ContextInterface $context = null): string; }