adachsoft / json-file-storage
A concurrency-safe JSON file storage library with locking and atomic writes.
Requires
- php: ^8.3
Requires (Dev)
- adachsoft/php-code-style: ^0.7.4
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13
- rector/rector: ^2.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
adachsoft/json-file-storage provides one shared JSON document store for *-json-file repositories. It centralizes whole-document reads and writes, sidecar locking, and atomic replacement so repositories do not need to maintain separate copies of the storage implementation.
Requirements
PHP 8.3 or newer.
Installation
composer require adachsoft/json-file-storage
Usage
API
Create a store for a JSON document. Its parent directory is created when needed. A missing or empty document reads as an empty array; the document root must otherwise be a JSON object. JSON objects, including nested objects, are returned as associative arrays.
use AdachSoft\JsonFileStorage\JsonFileStorage;
$storage = new JsonFileStorage(__DIR__ . '/var/repository.json');
$document = $storage->read();
$storage->update(static function (array $document): array {
$document['count'] = (int) ($document['count'] ?? 0) + 1;
return $document;
});
read(): array reads the whole document. update(callable(array): array): void passes the current document to a read-modify-write callback. transaction(callable(array): JsonFileTransactionResult): mixed runs a callback under the same exclusive lock and returns its result. JsonFileTransactionResult contains the replacement document and the value to return; a null document means that the file is not written.
Atomic repository operations
Use transaction() when a repository must read, check, and modify its document as one operation. For example, a job can be removed and returned exactly once:
use AdachSoft\JsonFileStorage\JsonFileStorage;
use AdachSoft\JsonFileStorage\JsonFileTransactionResult;
final readonly class JobRepository
{
public function __construct(private JsonFileStorage $storage)
{
}
public function consume(string $jobId): mixed
{
return $this->storage->transaction(
static function (array $document) use ($jobId): JsonFileTransactionResult {
$jobs = $document['jobs'] ?? [];
if (!is_array($jobs) || !array_key_exists($jobId, $jobs)) {
return new JsonFileTransactionResult(null, null);
}
$job = $jobs[$jobId];
unset($jobs[$jobId]);
$document['jobs'] = $jobs;
return new JsonFileTransactionResult($document, $job);
},
);
}
}
The lookup, removal, and returned item all happen while holding one exclusive lock, preventing two cooperating processes from consuming the same item.
Guarantees and limits
read()uses a shared lock;update()andtransaction()use an exclusive lock for the full read-modify-write operation. The lock is stored in a sidecar file named<document>.lock.- Writes are pretty-printed UTF-8 JSON with a trailing newline. A temporary file is created in the document's directory, flushed and synchronized with
fsync(), then atomically installed withrename(). - The store rewrites the entire document on each write. It is intended for small- and medium-sized documents, not large databases.
flock()locks are advisory and coordinate only processes that follow the same locking convention. Use a local filesystem; network filesystems such as NFS are not supported.