Skip to content

Commit

Permalink
Filesystem Updates (#219)
Browse files Browse the repository at this point in the history
## Description



## Checklist
- [ ] Updated CHANGELOG files
- [ ] Updated Documentation
- [ ] Unit Tests Created
- [ ] php-cs-fixer
  • Loading branch information
JoshuaEstes authored Aug 24, 2024
1 parent a19ff66 commit 6799c7b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
42 changes: 35 additions & 7 deletions src/SonsOfPHP/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,52 @@ public function __construct(
private AdapterInterface $adapter,
) {}

public function write(string $path, mixed $contents, ?ContextInterface $context = null): void
public function write(string $path, mixed $contents, ContextInterface|array $context = null): void
{
if (!is_string($contents) && !is_resource($contents)) {
throw new FilesystemException(sprintf('Argument "$contents" must be of type "string" or "resource". Type "%s" given.', gettype($contents)));
}

if (is_array($context)) {
$context = new Context($context);
}

$this->adapter->add($path, $contents, $context);
}

public function read(string $path, ?ContextInterface $context = null): string
public function read(string $path, ContextInterface|array $context = null): string
{
if (is_array($context)) {
$context = new Context($context);
}

return $this->adapter->get($path, $context);
}

public function delete(string $path, ?ContextInterface $context = null): void
public function delete(string $path, ContextInterface|array $context = null): void
{
if (is_array($context)) {
$context = new Context($context);
}

$this->adapter->remove($path, $context);
}

public function exists(string $path, ?ContextInterface $context = null): bool
public function exists(string $path, ContextInterface|array $context = null): bool
{
if (is_array($context)) {
$context = new Context($context);
}

return $this->adapter->has($path, $context);
}

public function copy(string $source, string $destination, ?ContextInterface $context = null): void
public function copy(string $source, string $destination, ContextInterface|array $context = null): void
{
if (is_array($context)) {
$context = new Context($context);
}

if ($this->adapter instanceof CopyAwareInterface) {
$this->adapter->copy($source, $destination);
return;
Expand All @@ -54,8 +74,12 @@ public function copy(string $source, string $destination, ?ContextInterface $con
$this->write($destination, $this->get($source, $context), $context);
}

public function move(string $source, string $destination, ?ContextInterface $context = null): void
public function move(string $source, string $destination, ContextInterface|array $context = null): void
{
if (is_array($context)) {
$context = new Context($context);
}

if ($this->adapter instanceof MoveAwareInterface) {
$this->adapter->move($source, $destination, $context);
return;
Expand All @@ -65,8 +89,12 @@ public function move(string $source, string $destination, ?ContextInterface $con
$this->delete($source, $context);
}

public function mimeType(string $path, ?ContextInterface $context = null): string
public function mimeType(string $path, ContextInterface|array $context = null): string
{
if (is_array($context)) {
$context = new Context($context);
}

return $this->adapter->mimeType($path, $context);
}
}
14 changes: 7 additions & 7 deletions src/SonsOfPHP/Contract/Filesystem/FilesystemInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface FilesystemInterface
*
* @throws FilesystemExceptionInterface
*/
public function write(string $path, mixed $content, ?ContextInterface $context = null): void;
public function write(string $path, mixed $content, ContextInterface|array $context = null): void;
// or put, putContents

// could write not do this by default? Maybe option to overwrite if exists?
Expand All @@ -25,34 +25,34 @@ public function write(string $path, mixed $content, ?ContextInterface $context =
/**
* @throws FilesystemExceptionInterface
*/
public function read(string $path, ?ContextInterface $context = null): string;
public function read(string $path, ContextInterface|array $context = null): string;
// or get, getContents

/**
* @throws FilesystemExceptionInterface
*/
public function delete(string $path, ?ContextInterface $context = null): void;
public function delete(string $path, ContextInterface|array $context = null): void;
// or remove, rm, rmdir

/**
* Checks to see if a file or directory exists
*
* @throws FilesystemExceptionInterface Generic Failure Exception
*/
public function exists(string $path, ?ContextInterface $context = null): bool;
public function exists(string $path, ContextInterface|array $context = null): bool;

//public function isFile(string $path, ?ContextInterface $context = null): bool;

/**
* @throws FilesystemExceptionInterface
*/
public function copy(string $source, string $destination, ?ContextInterface $context = null): void;
public function copy(string $source, string $destination, ContextInterface|array $context = null): void;
// or cp

/**
* @throws FilesystemExceptionInterface
*/
public function move(string $source, string $destination, ?ContextInterface $context = null): void;
public function move(string $source, string $destination, ContextInterface|array $context = null): void;
// or mv

//public function createDirectory(string|iterable $dirs): bool;
Expand All @@ -67,5 +67,5 @@ public function move(string $source, string $destination, ?ContextInterface $con
/**
* @throws FilesystemExceptionInterface Generic Failure Exception
*/
public function mimeType(string $path, ?ContextInterface $context = null): string;
public function mimeType(string $path, ContextInterface|array $context = null): string;
}

0 comments on commit 6799c7b

Please sign in to comment.