adachsoft / calendar-json-file
JSON file repositories for adachsoft/calendar-contracts calendars and entries.
Requires
- php: ^8.3
- adachsoft/calendar-contracts: ^0.1
- adachsoft/recurrence-contracts: ^0.1
- adachsoft/reference-contracts: ^0.1
Requires (Dev)
- adachsoft/php-code-style: ^0.7.3
- adachsoft/recurrence: ^0.1.1
- 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
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.