Search by

adachsoft / data-backup-contracts

Arkadiusz Adach

Interfaces and value objects for interoperable PHP data backup and restore integrations.

Package info

gitlab.com/a.adach/data-backup-contracts

Issues

pkg:composer/adachsoft/data-backup-contracts

Statistics

Installs: 30

Dependents: 1

Suggesters: 0

Stars: 0

v0.2.0 2026-11-24 18:31 UTC

This package is auto-updated.

Last update: 2026-11-24 17:31:21 UTC


README

adachsoft/data-backup-contracts defines the PHP interfaces, immutable value objects, DTOs, enums, and typed collections used to describe system data sources and backup archives. It provides contracts only; concrete archive formats, storage adapters, and backup orchestration belong in other packages.

Installation and integration

Install the package with Composer:

composer require adachsoft/data-backup-contracts

Implement the relevant contracts in your application or adapter package, then register those implementations with your backup orchestration layer. The library does not provide concrete storage, archive format, or orchestration implementations.

Concepts

  • System: an application whose data is protected.
  • Data source: a logical part of a system's data with its own schema and version, such as auth.accounts.
  • Archive: a system-independent package containing a manifest and one part for each included data source.
  • Manifest: archive metadata describing the source system, creation time, scope, source schemas, dependencies, counts, checksums, and labels.
  • Target: access to a system, either in-process or remotely.
  • Storage: a location where archives are saved, listed, opened, and deleted.

Import modes and policies

ImportRequest and the shared ImportContext describe how records are applied:

  • Mode: REPLACE replaces selected target data with archive data; MERGE combines archive data with existing data.
  • Conflict policy: SKIP leaves a conflicting target record unchanged, OVERWRITE replaces it, and FAIL treats the conflict as an error.
  • Missing-reference policy: FAIL rejects an unresolved reference, SKIP_RECORD skips the record that contains it, and KEEP preserves the unresolved reference as-is.
  • Dry run: validates the requested import without applying data changes.

Identifier mapping

Record identifiers are stable within a source. During import, a parent source records each source-to-target identifier mapping through IdMapInterface. Dependent sources resolve those mappings before storing references, using the same ImportContext. This keeps references correct when target identifiers differ from the archive identifiers.

Interfaces

  • DataSourceInterface — exposes a data source's stable ID, schema version, part kind, and dependencies.
  • RecordDataSourceInterface — exports and imports a record-based source through streaming contracts.
  • BlobDataSourceInterface — exports and imports a blob-based source through streaming contracts.
  • DataSourceRegistryInterface — lists registered sources and resolves them by ID.
  • IdMapInterface — records and resolves source-to-target record identifiers.
  • SchemaMigratorInterface — checks and applies migrations from older record schemas.
  • ConsistencyGuardInterface — freezes and releases consistency-sensitive system writes.
  • RecordReaderInterface — streams typed records from an archive part.
  • RecordWriterInterface — writes typed records to an archive part.
  • BlobReaderInterface — streams typed blobs from an archive part.
  • BlobWriterInterface — writes typed blobs to an archive part.
  • ArchiveWriterInterface — opens record and blob parts and finalizes the archive manifest.
  • ArchiveReaderInterface — reads an archive manifest and opens its data-source parts.
  • BackupTargetInterface — describes a system and exports or imports archive data.
  • ArchiveStorageInterface — keeps serialized archive bytes with their manifest: saves, opens, lists, and deletes them; it does not know the archive format.
  • ArchiveFormatInterface — serializes a logical archive into bytes (createWriter) and reads it back (openReader); it does not know where bytes are stored.
  • ArchiveValidatorInterface — validates archive integrity and compatibility with a target import.
  • BackupServiceInterface — defines the high-level backup and restore operations.
  • RetentionPolicyInterface — selects archives eligible for deletion while protecting keep=true labels.
  • BackupEventListenerInterface — receives backup success, backup failure, and restore completion events.

Example: record data source

The example assumes an application-specific AccountRepository that streams records and imports one record, returning its target ID.

use AdachSoft\DataBackupContracts\Collection\DataSourceIdCollection;
use AdachSoft\DataBackupContracts\Collection\ValidationIssueCollection;
use AdachSoft\DataBackupContracts\Contract\RecordDataSourceInterface;
use AdachSoft\DataBackupContracts\Contract\RecordReaderInterface;
use AdachSoft\DataBackupContracts\Contract\RecordWriterInterface;
use AdachSoft\DataBackupContracts\Dto\ImportContext;
use AdachSoft\DataBackupContracts\Dto\SourceImportReport;
use AdachSoft\DataBackupContracts\Enum\PartKindEnum;
use AdachSoft\DataBackupContracts\ValueObject\DataSourceId;

final class AccountDataSource implements RecordDataSourceInterface
{
    public function __construct(
        private AccountRepository $accounts,
        private DataSourceId $id,
        private DataSourceIdCollection $dependencies,
    ) {
    }

    public function getId(): DataSourceId
    {
        return $this->id;
    }

    public function getSchemaVersion(): int
    {
        return 1;
    }

    public function getKind(): PartKindEnum
    {
        return PartKindEnum::RECORDS;
    }

    public function getDependencies(): DataSourceIdCollection
    {
        return $this->dependencies;
    }

    public function exportRecords(RecordWriterInterface $writer): void
    {
        foreach ($this->accounts->iterateRecords() as $record) {
            $writer->write($record);
        }
    }

    public function importRecords(RecordReaderInterface $reader, ImportContext $context): SourceImportReport
    {
        $created = 0;

        foreach ($reader as $record) {
            $targetId = $this->accounts->import($record, $context);
            $context->idMap->record($this->id, $record->id, $targetId);
            ++$created;
        }

        return new SourceImportReport(
            $this->id,
            $created,
            0,
            0,
            0,
            new ValidationIssueCollection(),
        );
    }
}

Out of scope

This package does not provide an archive file format, encryption or signing, storage implementations, or backup orchestration. The orchestration layer belongs in adachsoft/data-backup.

Namespaces

Public types are organized under AdachSoft\DataBackupContracts in the ValueObject, Enum, Dto, Collection, Contract, and Exception namespaces.

Requirements

  • PHP 8.3 or newer
  • adachsoft/collection 3.x
  • psr/http-message 2.x