adachsoft / principal-contact-json-file
JSON-file storage adapter for adachsoft/principal-contact-contracts: principal contacts repository with file locking, atomic writes and optimistic versions.
Package info
gitlab.com/a.adach/principal-contact-json-file
pkg:composer/adachsoft/principal-contact-json-file
Requires
- php: ^8.3
- adachsoft/principal-contact-contracts: ^0.1
- adachsoft/reference-contracts: ^0.1
Requires (Dev)
- adachsoft/php-code-style: ^0.7.4
- friendsofphp/php-cs-fixer: ^3.95
- phpstan/phpstan: ^2.3
- phpunit/phpunit: ^13.4
- rector/rector: ^2.8
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A JSON-file storage adapter for AdachSoft\PrincipalContactContracts\Repository\PrincipalContactsRepositoryInterface. It persists versioned contact snapshots for principals such as accounts or groups, independently of an application's authentication system.
Requirements
- PHP 8.3 or newer.
- Composer.
Installation
composer require adachsoft/principal-contact-json-file
Composer installs this adapter and its declared runtime dependencies.
Usage
Create a repository with the path of the JSON document:
use AdachSoft\PrincipalContactContracts\Repository\PrincipalContactsRepositoryInterface;
use AdachSoft\PrincipalContactJsonFile\Repository\JsonPrincipalContactsRepository;
$filePath = __DIR__ . '/var/principal-contacts.json';
$repository = new JsonPrincipalContactsRepository($filePath);
Pass the repository to ContactServiceFactory::create() when the separate adachsoft/principal-contact service package is installed. That package is not a dependency of this adapter. Its factory accepts the following arguments:
public static function create(
PrincipalContactsRepositoryInterface $repository,
?ContactAccessPolicyInterface $policy = null,
?ContactValueValidatorRegistry $validators = null,
?ContactChangeListenerInterface $listener = null,
?ClockInterface $clock = null,
): ContactServiceInterface;
An application using that separate package can create its service with the repository, for example $contactService = ContactServiceFactory::create($repository);. The basic usage example runs with this package and its declared dependencies only.
File format
The document has a principals object. Each key is a JSON-encoded two-element array [principal type, principal identifier]. Encoding the pair as a JSON array preserves its field boundaries, so different pairs cannot collide.
{
"principals": {
"[\"account\",\"user-123\"]": {
"principal": {
"type": "account",
"id": "user-123"
},
"contactPoints": [
{
"channel": "email",
"value": "person@example.com",
"primary": true,
"label": "Work"
},
{
"channel": "phone",
"value": "+15551234567",
"primary": false,
"label": null
}
],
"createdAt": "2025-03-04T05:06:07.123456+00:00",
"updatedAt": "2025-03-05T06:07:08.654321+00:00",
"version": 2
}
}
}
Contact-point order is preserved. Timestamps use Y-m-d\TH:i:s.uP, retaining microseconds and the UTC offset.
Concurrency and atomic writes
Reads use a shared flock lock on a sidecar file named <document>.lock. Updates use an exclusive lock for the complete read-modify-write operation. The adapter checks the optimistic version while holding that lock: the stored version, or 0 when absent, must equal the incoming snapshot version minus one. A mismatch raises ConcurrentModificationException without replacing the document.
Successful writes encode a temporary file in the document's directory and atomically replace the JSON document with rename(). Applications should keep the JSON file and sidecar lock on a filesystem that supports flock and same-directory atomic rename.
Contract tests
Run the repository contract and adapter tests from the package root:
vendor/bin/phpunit --testsuite default
JsonPrincipalContactsRepositoryContractTest extends the reusable contract test from adachsoft/principal-contact-contracts.
Limits
Each operation reads and validates the whole JSON document, and each successful update writes the whole document. This adapter is intended for small installations and modest contact datasets, not high-volume or large-scale storage.