Search by

adachsoft / principal-group-json-file

Arkadiusz Adach

JSON file storage adapter for principal group repositories.

Package info

gitlab.com/a.adach/principal-group-json-file

Issues

pkg:composer/adachsoft/principal-group-json-file

Statistics

Installs: 1

Dependents: 0

Suggesters: 0

Stars: 0

v0.1.0 2026-11-24 15:21 UTC

This package is auto-updated.

Last update: 2026-11-24 14:22:03 UTC


README

adachsoft/principal-group-json-file provides a JSON-file implementation of AdachSoft\PrincipalGroup\Repository\GroupRepositoryInterface for small installations that need durable group storage without a database.

Requirements

  • PHP 8.3 or newer.
  • adachsoft/principal-group ^0.1.
  • adachsoft/reference-contracts ^0.1.

Installation

composer require adachsoft/principal-group-json-file

Usage

Create the repository with the path to its JSON document and pass it to the principal-group service factory:

use AdachSoft\PrincipalGroup\Service\GroupServiceFactory;
use AdachSoft\PrincipalGroupJsonFile\Repository\JsonGroupRepository;

$repository = new JsonGroupRepository(__DIR__ . '/var/groups.json');
$groupService = GroupServiceFactory::create($repository);

GroupServiceFactory::create() accepts the repository as its first argument. Its optional listener, clock, and identifier generator can be supplied when needed.

Examples

See Basic usage for a standalone example of creating the repository and reading a group.

File format

The document stores groups in an object keyed by group identifier:

{
    "groups": {
        "home": {
            "id": "home",
            "name": "Home",
            "principalType": "group",
            "version": 1,
            "createdAt": "2025-05-04T08:09:10.123456+00:00",
            "updatedAt": "2025-05-04T08:09:10.123456+00:00",
            "metadata": {},
            "members": [
                {
                    "principal": {
                        "type": "account",
                        "id": "alice"
                    },
                    "role": "admin",
                    "joinedAt": "2025-05-04T08:09:10.123456+00:00"
                }
            ]
        }
    }
}

Dates use the ISO-8601 format Y-m-d\TH:i:s.uP. Metadata is stored as a JSON object, including when it is empty.

Concurrency

Reads use a shared flock lock and read-modify-write operations use an exclusive lock on a sidecar .lock file. Updates write a temporary file beside the document and atomically replace it. Repository saves also use optimistic versions: a new group must start at version 1, and an update is accepted only when the stored version (or 0 for a missing group) equals the new version minus one. Stale updates throw ConcurrentModificationException without replacing the document.

Contract tests

Run the repository contract and its JSON-file-specific tests with:

vendor/bin/phpunit tests/Repository/JsonGroupRepositoryContractTest.php

The contract test extends the shared AdachSoft\PrincipalGroup\Testing\AbstractGroupRepositoryContractTest suite.

Limits

Each operation reads or writes the complete JSON document. This adapter is intended for small installations and modest group collections, not large datasets or high-throughput storage.