Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filesystem updates and enhancements #218

Merged
merged 1 commit into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/components/filesystem/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
25 changes: 25 additions & 0 deletions src/SonsOfPHP/Component/Filesystem/Adapter/ChainAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}
}
10 changes: 10 additions & 0 deletions src/SonsOfPHP/Component/Filesystem/Adapter/InMemoryAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, '/');
Expand Down
22 changes: 22 additions & 0 deletions src/SonsOfPHP/Component/Filesystem/Adapter/NativeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 . '"');
}
Expand Down Expand Up @@ -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;
}
}
6 changes: 6 additions & 0 deletions src/SonsOfPHP/Component/Filesystem/Adapter/NullAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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');
}
}
15 changes: 15 additions & 0 deletions src/SonsOfPHP/Component/Filesystem/Adapter/ReadOnlyAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
15 changes: 15 additions & 0 deletions src/SonsOfPHP/Component/Filesystem/Adapter/WormAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
5 changes: 5 additions & 0 deletions src/SonsOfPHP/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
5 changes: 5 additions & 0 deletions src/SonsOfPHP/Contract/Filesystem/FilesystemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading