Search by

adachsoft / calendar-json-file

Arkadiusz Adach

JSON file repositories for adachsoft/calendar-contracts calendars and entries.

Package info

gitlab.com/a.adach/calendar-json-file

Issues

pkg:composer/adachsoft/calendar-json-file

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

v0.1.0 2026-11-24 18:03 UTC

This package is auto-updated.

Last update: 2026-11-24 17:37:49 UTC


README

adachsoft/calendar-json-file provides JSON-file implementations of AdachSoft\CalendarContracts\Repository\CalendarRepositoryInterface and AdachSoft\CalendarContracts\Repository\EntryRepositoryInterface for small installations that need durable calendar storage without a database.

Requirements

  • PHP 8.3 or newer.
  • adachsoft/calendar-contracts ^0.1.
  • adachsoft/recurrence-contracts ^0.1.
  • adachsoft/reference-contracts ^0.1.

Installation

composer require adachsoft/calendar-json-file

Usage

Create a calendar repository with a document path. The entry repository also requires a recurrence rule normalizer:

use AdachSoft\CalendarJsonFile\Repository\JsonCalendarRepository;
use AdachSoft\CalendarJsonFile\Repository\JsonEntryRepository;
use AdachSoft\Recurrence\RecurrenceFactory;

$calendarRepository = new JsonCalendarRepository(__DIR__ . '/var/calendars.json');
$entryRepository = new JsonEntryRepository(
    __DIR__ . '/var/entries.json',
    RecurrenceFactory::createNormalizer(),
);

// Pass these objects wherever the corresponding repository interfaces are required.

RecurrenceFactory is provided by the separate adachsoft/recurrence package; install it with composer require adachsoft/recurrence when using that factory. The adapter itself depends only on adachsoft/recurrence-contracts, so any RecurrenceRuleNormalizerInterface implementation can be supplied. Use adachsoft/recurrence ^0.1.1 or newer: earlier versions drop microseconds from rule date-times, so rules would not round-trip exactly.

Examples

See Basic usage for an example that saves a calendar and weekly entry, then reads entries with findInRange().

File format

Each repository uses a separate JSON document with records stored in an object keyed by identifier. Dates use the ISO-8601 format Y-m-d\TH:i:s.uP. Metadata is represented as a JSON object, including when it is empty.

A calendar document (calendars.json) has this shape:

{
    "calendars": {
        "work": {
            "id": "work",
            "name": "Work",
            "owner": {
                "type": "account",
                "id": "alice"
            },
            "sharedWith": [],
            "timezone": "Europe/Warsaw",
            "createdAt": "2026-05-01T10:00:00.000000+02:00",
            "updatedAt": "2026-05-01T10:00:00.000000+02:00",
            "version": 1,
            "metadata": {}
        }
    }
}

An entry document (entries.json) stores each recurrence rule in the normalizer's normalized form:

{
    "entries": {
        "weekly-planning": {
            "id": "weekly-planning",
            "calendarId": "work",
            "title": "Weekly planning",
            "timing": {
                "rule": {
                    "type": "repeating",
                    "start": "2026-06-01T09:00:00",
                    "timezone": "Europe/Warsaw",
                    "frequency": "weekly",
                    "interval": 1,
                    "times": [],
                    "weekdays": [1],
                    "month_days": [],
                    "weekday_occurrences": [],
                    "months": [],
                    "end": {
                        "type": "never"
                    },
                    "exceptions": [],
                    "additions": []
                },
                "durationMinutes": 60,
                "allDay": false
            },
            "createdBy": {
                "type": "account",
                "id": "alice"
            },
            "activeFrom": "2026-06-01T09:00:00.000000+02:00",
            "activeUntil": null,
            "createdAt": "2026-05-01T10:00:00.000000+02:00",
            "updatedAt": "2026-05-01T10:00:00.000000+02:00",
            "version": 1,
            "description": null,
            "subject": null,
            "location": null,
            "metadata": {}
        }
    }
}

The rule object is produced and consumed by the supplied RecurrenceRuleNormalizerInterface; its fields follow the selected normalizer's format.

Concurrency

Reads use a shared flock lock, while 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 aggregate must start at version 1, and an update is accepted only when the stored version (or 0 for a missing record) equals the new version minus one. A stale update throws ConcurrentModificationException without replacing the document.

Contract tests

Run the shared repository contract suites and their JSON-file-specific tests with:

vendor/bin/phpunit tests/Repository/JsonCalendarRepositoryContractTest.php
vendor/bin/phpunit tests/Repository/JsonEntryRepositoryContractTest.php

The calendar and entry tests extend AdachSoft\CalendarContracts\Testing\AbstractCalendarRepositoryContractTest and AdachSoft\CalendarContracts\Testing\AbstractEntryRepositoryContractTest, respectively.

Limits

Each operation reads or writes the complete JSON document. The adapter is intended for small installations and modest calendar collections, not large datasets or high-throughput storage. findInRange() scans all stored entries before filtering and sorting the matches.