Skip to content

Repository files navigation

Quillstack Local Storage

Tests Latest Version Downloads PHP Version StyleCI CodeFactor Quality Gate Coverage Maintainability Reliability Security Maintainability License

The package to manage files on the local storage.

Why this exists

It is the implementation behind quillstack/storage-interface, and its whole job is to make an interface out of four PHP functions so that everything above it can be tested without a disk.

The cache, the logger and dotenv all read and write through that interface. In a test they are handed something that keeps files in an array; in production they are handed this. Neither knows the difference, and neither calls file_put_contents itself.

Nothing here does anything clever. Reading it should take a minute and finding a bug in it should be hard, which is the point of a package this small.

Requirements

  • PHP 8.1 or newer

Installation

To install this package, run the standard command using Composer:

composer require quillstack/local-storage

Usage

Create a class or inject it as a dependency:

use Quillstack\LocalStorage\LocalStorage;

$storage = new LocalStorage();
$storage->save('var/cache/token.txt', 'muHaloosPps23sKkdsaaBBcei');

If you want to use it as a dependency:

use Quillstack\LocalStorage\LocalStorage;

public function __construct(private LocalStorage $storage)
{
    //
}

public function getTokenFromCache()
{
    $this->storage->get('var/cache/token.txt');
}

Storage interface

This package implements quillstack/storage-interface:
https://github.com/quillstack/storage-interface
with methods:

  • get() Retrieves the contents of a file.
    This method opens a file and return its contents, it throws an exception if file doesn't exist.
  • exists() Checks if the file exists on the storage.
  • missing() Checks if the file is missing from the storage.
  • save() Saves the contents to the file.
    This method throws an exception if there are any troubles with saving a file (e.g. no space left on device).
  • delete() Deletes one or more files.
    This method deletes one or many files and throws an exception if error occurs during deleting a file.

Benchmark

Measured with quillstack/benchmark on a thousand write-and-read pairs of a small file. Runs are interleaved and unconcurrent, each figure is the median of five, and PHP is 8.5.7.

Version
quillstack/local-storage 0.6.0
league/flysystem 3.35.3
Per write and read Relative
quillstack/local-storage 83.6 µs
league/flysystem 85.7 µs 1.02×

These are the same number. Both call the same kernel, and two per cent between them is the filesystem rather than either library. A benchmark of two thin wrappers over file_put_contents was never going to say anything else, and it is here because the alternative is leaving a reader to wonder.

The difference is what they reach. league/flysystem writes to S3, FTP, SFTP, Azure, Google Cloud, in-memory and a dozen more, with stream support, visibility, MIME detection and directory listings. This writes to the disk it is running on. Where you need one of those, use Flysystem — and where you need a StorageInterface implementation that does not bring an ecosystem with it, that is what this is.

Tests

Run tests using a command:

phpdbg -qrr ./vendor/bin/unit-tests

The rest of Quillstack

This is one component of Quillstack, a PHP framework which is as simple to use as it is strict about what it does.

License

MIT — see LICENSE.

About

The library to manage files in local storage.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Used by

Contributors

Languages