Skip to content

Commit

Permalink
various updates and changes
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaEstes committed Oct 25, 2023
1 parent ea9f83b commit 305526b
Show file tree
Hide file tree
Showing 14 changed files with 107 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
33 changes: 33 additions & 0 deletions src/SonsOfPHP/Component/Filesystem/Adapter/ChainAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
15 changes: 15 additions & 0 deletions src/SonsOfPHP/Component/Filesystem/Adapter/NativeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
19 changes: 13 additions & 6 deletions src/SonsOfPHP/Component/Filesystem/Adapter/NullAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
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 @@ -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);
}
}
23 changes: 21 additions & 2 deletions src/SonsOfPHP/Component/Filesystem/Adapter/WormAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
/**
* @author Joshua Estes <[email protected]>
*/
final class FileNotFoundException extends FilesystemException
{
}
final class FileNotFoundException extends FilesystemException {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
/**
* @author Joshua Estes <[email protected]>
*/
class FilesystemException extends \Exception
{
}
class FilesystemException extends \Exception {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
/**
* @author Joshua Estes <[email protected]>
*/
final class UnableToCopyFileException extends FilesystemException
{
}
final class UnableToCopyFileException extends FilesystemException {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
/**
* @author Joshua Estes <[email protected]>
*/
final class UnableToDeleteDirectoryException extends FilesystemException
{
}
final class UnableToDeleteDirectoryException extends FilesystemException {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
/**
* @author Joshua Estes <[email protected]>
*/
final class UnableToDeleteFileException extends FilesystemException
{
}
final class UnableToDeleteFileException extends FilesystemException {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
/**
* @author Joshua Estes <[email protected]>
*/
final class UnableToMoveFileException extends FilesystemException
{
}
final class UnableToMoveFileException extends FilesystemException {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
/**
* @author Joshua Estes <[email protected]>
*/
final class UnableToReadFileException extends FilesystemException
{
}
final class UnableToReadFileException extends FilesystemException {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
/**
* @author Joshua Estes <[email protected]>
*/
final class UnableToWriteFileException extends FilesystemException
{
}
final class UnableToWriteFileException extends FilesystemException {}

0 comments on commit 305526b

Please sign in to comment.