Skip to content

Commit

Permalink
Added copyTo and moveTo to directory
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss committed Apr 14, 2022
1 parent 36cc915 commit aa9b228
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
24 changes: 24 additions & 0 deletions modules/Files/src/Contract/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
use Elephox\Files\DirectoryNotCreatedException;
use Elephox\Files\DirectoryNotEmptyException;
use Elephox\Files\DirectoryNotFoundException;
use Elephox\Files\FileAlreadyExistsException;
use Elephox\Files\FileCopyException;
use Elephox\Files\FileMoveException;
use Elephox\Files\FilesystemNodeNotFoundException;
use Elephox\Files\FilesystemNodeNotImplementedException;

interface Directory extends FilesystemNode
{
Expand Down Expand Up @@ -58,4 +62,24 @@ public function delete(bool $recursive = true): void;
* @param int $permissions
*/
public function ensureExists(bool $recursive = true, int $permissions = 0o0777): void;

/**
* @param Directory $directory
* @param bool $overwrite
*
* @throws FileMoveException
* @throws FileAlreadyExistsException
* @throws FilesystemNodeNotImplementedException
*/
public function moveTo(Directory $directory, bool $overwrite = true): void;

/**
* @param Directory $directory
* @param bool $overwrite
*
* @throws FileCopyException
* @throws FileAlreadyExistsException
* @throws FilesystemNodeNotImplementedException
*/
public function copyTo(Directory $directory, bool $overwrite = true): void;
}
34 changes: 34 additions & 0 deletions modules/Files/src/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,38 @@ public function ensureExists(bool $recursive = true, int $permissions = 0o0777):
throw new DirectoryNotCreatedException($this->path);
}
}

public function moveTo(Contract\Directory $directory, bool $overwrite = true): void
{
if ($directory->getPath() === $this->path) {
return;
}

$directory->ensureExists();

foreach ($this->getChildren() as $node) {
if ($node instanceof Contract\File || $node instanceof Contract\Directory) {
$node->moveTo($directory, $overwrite);
} else {
throw new FilesystemNodeNotImplementedException($node, 'Cannot move filesystem node for unimplemented type ' . get_debug_type($node));
}
}
}

public function copyTo(Contract\Directory $directory, bool $overwrite = true): void
{
if ($directory->getPath() === $this->path) {
return;
}

$directory->ensureExists();

foreach ($this->getChildren() as $node) {
if ($node instanceof Contract\File || $node instanceof Contract\Directory) {
$node->copyTo($directory, $overwrite);
} else {
throw new FilesystemNodeNotImplementedException($node, 'Cannot move filesystem node for unimplemented type ' . get_debug_type($node));
}
}
}
}
2 changes: 1 addition & 1 deletion modules/Files/src/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private function getDestination(FilesystemNode $node, bool $overwrite): Contract
} elseif ($node instanceof Contract\File) {
$destination = $node;
} else {
throw new InvalidArgumentException('Given filesystem node is not a file or directory');
throw new FilesystemNodeNotImplementedException($node, 'Given filesystem node is not a file or directory');
}

if (!$overwrite && $destination->exists()) {
Expand Down
15 changes: 15 additions & 0 deletions modules/Files/src/FilesystemNodeNotImplementedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);

namespace Elephox\Files;

use Elephox\Files\Contract\FilesystemNode;
use Throwable;

class FilesystemNodeNotImplementedException extends FileException
{
public function __construct(public readonly FilesystemNode $node, string $message, int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}

0 comments on commit aa9b228

Please sign in to comment.