Skip to content

Storage interface

composer require quillstack/storage-interface

Common interface for Storage classes.

Six methods, so that something writing files does not have to know which files. A package depending on this one works the same against the disk, against a bucket, or against something held in memory for a test.

Requirements

  • PHP 8.1 or newer

Installation

shell
composer require quillstack/storage-interface

Usage

Ask for the interface, not for an implementation:

php
use Quillstack\StorageInterface\StorageInterface;

final class Invoices
{
    public function __construct(private readonly StorageInterface $storage)
    {
    }

    public function keep(string $number, string $pdf): void
    {
        $this->storage->save("/invoices/{$number}.pdf", $pdf);
    }
}

Point it at whichever storage the application uses:

php
$app = new App(__DIR__ . '/../.env', [
    StorageInterface::class => LocalStorage::class,
]);

Technical documentation

MethodDoes
get(string $path): mixedreads what is there
exists(string $path): boolwhether there is anything at that path
missing(string $path): boolthe other way round, because !exists() reads worse
save(string $path, mixed $contents): boolwrites, replacing whatever was there
add(string $path, mixed $contents): boolwrites on the end of what is there
delete(string $path, string ...$more): boolremoves one or several

What implements it

quillstack/queue writes messages through it, which is why a queue can be pointed somewhere other than the local disk without knowing it has been.

There is nothing to run here: a package which only names things has no behaviour to test.

License

MIT. See LICENSE.

Released under the MIT License.