Skip to content

Commit

Permalink
Refactor filesystem (#215)
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 21, 2024
1 parent 7456a96 commit fb8f737
Show file tree
Hide file tree
Showing 30 changed files with 185 additions and 129 deletions.
9 changes: 9 additions & 0 deletions docs/contracts/filesystem/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Filesystem Contract
---

## Installation

```shell
composer require sonsofphp/filesystem-contract
```
6 changes: 5 additions & 1 deletion src/SonsOfPHP/Component/Filesystem/Adapter/ChainAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

namespace SonsOfPHP\Component\Filesystem\Adapter;

use SonsOfPHP\Component\Filesystem\ContextInterface;
use SonsOfPHP\Component\Filesystem\Exception\FileNotFoundException;
use SonsOfPHP\Contract\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\CopyAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\DirectoryAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\MoveAwareInterface;
use SonsOfPHP\Contract\Filesystem\ContextInterface;

/**
* Chain adapter allows you to use multiple adapters together.
Expand Down
27 changes: 0 additions & 27 deletions src/SonsOfPHP/Component/Filesystem/Adapter/CopyAwareInterface.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

namespace SonsOfPHP\Component\Filesystem\Adapter;

use SonsOfPHP\Component\Filesystem\ContextInterface;
use SonsOfPHP\Component\Filesystem\Exception\UnableToReadFileException;
use SonsOfPHP\Contract\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\CopyAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\DirectoryAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\MoveAwareInterface;
use SonsOfPHP\Contract\Filesystem\ContextInterface;

/**
* Just keeps files in memory, does not write anything to disk
Expand Down
27 changes: 0 additions & 27 deletions src/SonsOfPHP/Component/Filesystem/Adapter/MoveAwareInterface.php

This file was deleted.

6 changes: 5 additions & 1 deletion src/SonsOfPHP/Component/Filesystem/Adapter/NativeAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

namespace SonsOfPHP\Component\Filesystem\Adapter;

use SonsOfPHP\Component\Filesystem\ContextInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\CopyAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\DirectoryAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\MoveAwareInterface;
use SonsOfPHP\Contract\Filesystem\ContextInterface;

/**
* The native adapter will use the underlying filesystem to store files.
Expand Down
3 changes: 2 additions & 1 deletion src/SonsOfPHP/Component/Filesystem/Adapter/NullAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace SonsOfPHP\Component\Filesystem\Adapter;

use SonsOfPHP\Component\Filesystem\ContextInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Contract\Filesystem\ContextInterface;

/**
* Null Adapter does absolutly nothing, it's good for testing and that's pretty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

namespace SonsOfPHP\Component\Filesystem\Adapter;

use SonsOfPHP\Component\Filesystem\ContextInterface;
use SonsOfPHP\Component\Filesystem\Exception\FilesystemException;
use SonsOfPHP\Contract\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\CopyAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\DirectoryAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\MoveAwareInterface;
use SonsOfPHP\Contract\Filesystem\ContextInterface;

/**
* The Read Only adapter will only allow you to read existing files, if you try
Expand Down
6 changes: 5 additions & 1 deletion src/SonsOfPHP/Component/Filesystem/Adapter/WormAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

namespace SonsOfPHP\Component\Filesystem\Adapter;

use SonsOfPHP\Component\Filesystem\ContextInterface;
use SonsOfPHP\Component\Filesystem\Exception\FilesystemException;
use SonsOfPHP\Contract\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\CopyAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\DirectoryAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\MoveAwareInterface;
use SonsOfPHP\Contract\Filesystem\ContextInterface;

/**
* WORM = Write Once, Read Many. Using this adapter allows you to create new
Expand Down
1 change: 1 addition & 0 deletions src/SonsOfPHP/Component/Filesystem/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use ArrayIterator;
use SonsOfPHP\Component\Filesystem\Exception\FilesystemException;
use SonsOfPHP\Contract\Filesystem\ContextInterface;
use Traversable;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace SonsOfPHP\Component\Filesystem\Exception;

use SonsOfPHP\Contract\Filesystem\Exception\FileNotFoundExceptionInterface;

/**
* @author Joshua Estes <[email protected]>
*/
final class FileNotFoundException extends FilesystemException {}
final class FileNotFoundException extends FilesystemException implements FileNotFoundExceptionInterface {}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
namespace SonsOfPHP\Component\Filesystem\Exception;

use Exception;
use SonsOfPHP\Contract\Filesystem\Exception\FilesystemExceptionInterface;

/**
* @author Joshua Estes <[email protected]>
*/
class FilesystemException extends Exception {}
class FilesystemException extends Exception implements FilesystemExceptionInterface {}
8 changes: 5 additions & 3 deletions src/SonsOfPHP/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace SonsOfPHP\Component\Filesystem;

use SonsOfPHP\Component\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Component\Filesystem\Adapter\CopyAwareInterface;
use SonsOfPHP\Component\Filesystem\Adapter\MoveAwareInterface;
use SonsOfPHP\Component\Filesystem\Exception\FilesystemException;
use SonsOfPHP\Contract\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\CopyAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\MoveAwareInterface;
use SonsOfPHP\Contract\Filesystem\ContextInterface;
use SonsOfPHP\Contract\Filesystem\FilesystemInterface;

/**
* @author Joshua Estes <[email protected]>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
use SonsOfPHP\Component\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Component\Filesystem\Adapter\ChainAdapter;
use SonsOfPHP\Component\Filesystem\Adapter\CopyAwareInterface;
use SonsOfPHP\Component\Filesystem\Adapter\DirectoryAwareInterface;
use SonsOfPHP\Component\Filesystem\Adapter\InMemoryAdapter;
use SonsOfPHP\Component\Filesystem\Adapter\MoveAwareInterface;
use SonsOfPHP\Component\Filesystem\Exception\FileNotFoundException;
use SonsOfPHP\Contract\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\CopyAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\DirectoryAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\MoveAwareInterface;
use SonsOfPHP\Contract\Filesystem\Exception\FileNotFoundExceptionInterface;

#[CoversClass(ChainAdapter::class)]
#[UsesClass(InMemoryAdapter::class)]
Expand Down Expand Up @@ -64,7 +64,7 @@ public function testItWillThrowExceptionWhenFileNotFound(): void
$this->adapters[] = $adp;

$adapter = new ChainAdapter($this->adapters);
$this->expectException(FileNotFoundException::class);
$this->expectException(FileNotFoundExceptionInterface::class);
$adapter->get('/path/to/file.txt');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use SonsOfPHP\Component\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Component\Filesystem\Adapter\InMemoryAdapter;
use SonsOfPHP\Component\Filesystem\Exception\UnableToReadFileException;
use SonsOfPHP\Contract\Filesystem\Adapter\AdapterInterface;

/**
* @uses \SonsOfPHP\Component\Filesystem\Adapter\InMemoryAdapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use SonsOfPHP\Component\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Component\Filesystem\Adapter\CopyAwareInterface;
use SonsOfPHP\Component\Filesystem\Adapter\DirectoryAwareInterface;
use SonsOfPHP\Component\Filesystem\Adapter\MoveAwareInterface;
use SonsOfPHP\Component\Filesystem\Adapter\NativeAdapter;
use SonsOfPHP\Contract\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\CopyAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\DirectoryAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\MoveAwareInterface;

/**
* @internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use SonsOfPHP\Component\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Component\Filesystem\Adapter\NullAdapter;
use SonsOfPHP\Contract\Filesystem\Adapter\AdapterInterface;

/**
* @internal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\MockObject;
use PHPUnit\Framework\TestCase;
use SonsOfPHP\Component\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Component\Filesystem\Adapter\CopyAwareInterface;
use SonsOfPHP\Component\Filesystem\Adapter\DirectoryAwareInterface;
use SonsOfPHP\Component\Filesystem\Adapter\InMemoryAdapter;
use SonsOfPHP\Component\Filesystem\Adapter\MoveAwareInterface;
use SonsOfPHP\Component\Filesystem\Adapter\ReadOnlyAdapter;
use SonsOfPHP\Component\Filesystem\Exception\FilesystemException;
use SonsOfPHP\Contract\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\CopyAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\DirectoryAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\MoveAwareInterface;
use SonsOfPHP\Contract\Filesystem\Exception\FilesystemExceptionInterface;

#[CoversClass(ReadOnlyAdapter::class)]
#[UsesClass(InMemoryAdapter::class)]
Expand All @@ -40,28 +40,28 @@ public function testItHasTheCorrectInterface(): void
public function testItWillThrowExceptionWhenAddingFile(): void
{
$adapter = new ReadOnlyAdapter($this->adapter);
$this->expectException(FilesystemException::class);
$this->expectException(FilesystemExceptionInterface::class);
$adapter->add('/path/to/file.ext', 'contents');
}

public function testItWillThrowExceptionWhenRemovingFile(): void
{
$adapter = new ReadOnlyAdapter($this->adapter);
$this->expectException(FilesystemException::class);
$this->expectException(FilesystemExceptionInterface::class);
$adapter->remove('/path/to/file.ext');
}

public function testItWillThrowExceptionWhenCopyingFile(): void
{
$adapter = new ReadOnlyAdapter($this->adapter);
$this->expectException(FilesystemException::class);
$this->expectException(FilesystemExceptionInterface::class);
$adapter->copy('/path/to/file.ext', '/path/to/dest.ext');
}

public function testItWillThrowExceptionWhenMovingFile(): void
{
$adapter = new ReadOnlyAdapter($this->adapter);
$this->expectException(FilesystemException::class);
$this->expectException(FilesystemExceptionInterface::class);
$adapter->move('/path/to/file.ext', '/path/to/dest.ext');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
use SonsOfPHP\Component\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Component\Filesystem\Adapter\CopyAwareInterface;
use SonsOfPHP\Component\Filesystem\Adapter\DirectoryAwareInterface;
use SonsOfPHP\Component\Filesystem\Adapter\InMemoryAdapter;
use SonsOfPHP\Component\Filesystem\Adapter\MoveAwareInterface;
use SonsOfPHP\Component\Filesystem\Adapter\WormAdapter;
use SonsOfPHP\Component\Filesystem\Exception\FilesystemException;
use SonsOfPHP\Contract\Filesystem\Adapter\AdapterInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\CopyAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\DirectoryAwareInterface;
use SonsOfPHP\Contract\Filesystem\Adapter\MoveAwareInterface;
use SonsOfPHP\Contract\Filesystem\Exception\FilesystemExceptionInterface;

#[CoversClass(WormAdapter::class)]
#[UsesClass(InMemoryAdapter::class)]
Expand Down Expand Up @@ -41,23 +41,23 @@ public function testItCanAddFileOnlyOnce(): void
$adapter = new WormAdapter($this->adapter);

$adapter->add('/path/to/file.ext', 'contents');
$this->expectException(FilesystemException::class);
$this->expectException(FilesystemExceptionInterface::class);
$adapter->add('/path/to/file.ext', 'contents');
}

public function testItCannotRemoveFile(): void
{
$adapter = new WormAdapter($this->adapter);

$this->expectException(FilesystemException::class);
$this->expectException(FilesystemExceptionInterface::class);
$adapter->remove('/path/to/file.ext');
}

public function testItCannotMoveFile(): void
{
$adapter = new WormAdapter($this->adapter);

$this->expectException(FilesystemException::class);
$this->expectException(FilesystemExceptionInterface::class);
$adapter->move('/path/to/file.ext', '/path/to/dest.ext');
}

Expand Down Expand Up @@ -104,7 +104,7 @@ public function testItCanCopyFileOnlyIfDestinationExists(): void
$adapter->copy('/path/to/source.ext', '/path/to/destination.txt');

$adapter->add('/path/to/another.ext', 'contents');
$this->expectException(FilesystemException::class);
$this->expectException(FilesystemExceptionInterface::class);
$adapter->copy('/path/to/source.ext', '/path/to/another.ext');
}
}
Loading

0 comments on commit fb8f737

Please sign in to comment.