adachsoft / dns-zone-contracts
Provider-independent PHP contracts and value objects for managing DNS zone records.
Requires
- php: ^8.3
- adachsoft/collection: ^3.0
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.9
- symplify/phpstan-rules: ^14.19
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Provider-independent PHP contracts and value objects for reading, writing, and synchronizing DNS records in a zone. Provider adapters can implement the same API for OVH, Cloudflare, or another DNS service, while workflow consumers use one stable contract for mail setup and domain-verification records.
Requirements
- PHP 8.3 or later
- Composer
adachsoft/collection3.x
Installation
composer require adachsoft/dns-zone-contracts
Model
ZoneNamestores a lowercase ASCII domain name without a trailing dot. One trailing dot in input is normalized away; internationalized names must be supplied in ASCII-compatible form.RecordNamestores a lowercase name relative to the zone. The empty string and@denote the apex. Underscore labels such as_dmarcand_domainkeyare supported.fqdn(ZoneName $zone)returns the full name without a trailing dot.DnsRecordcontains a relative name,DnsRecordTypeEnum, record value, optional TTL, optional MX/SRV priority, and optional providerDnsRecordId. TXT values are stored without surrounding presentation quotes; provider adapters handle quoting.DnsRecordCollectionis an immutable, type-safe collection of records.RecordFilteroptionally restricts a read by relative name and/or type.DnsChangeSetreports created, updated, and deleted records and exposesisEmpty().
Record priorities are limited to MX and SRV records and must be between 0 and 65535. A null TTL means the zone's default; zero is allowed.
Services
DnsZoneReaderInterface lists records and checks whether a zone exists. DnsZoneWriterInterface creates, updates, and deletes records. Persisted records returned by a provider must have an identifier; updateRecord() requires that identifier. applyChanges() publishes pending changes for providers that require a zone refresh and can be a no-op for providers that apply changes immediately. DnsZoneInterface combines the reader and writer contracts.
Provider failures should be translated to the package exceptions: ZoneNotFoundException, RecordNotFoundException, InvalidDnsValueException, DnsAuthenticationException, DnsRateLimitedException, or DnsTransportException. All implement DnsZoneExceptionInterface.
Synchronizing records
RecordSetSynchronizer accepts any DnsZoneInterface implementation. ADD_MISSING adds records missing by relative name, type, and normalized value, and leaves existing records untouched. REPLACE_SAME_NAME_AND_TYPE reconciles every name/type pair present in the desired collection; it replaces all existing records for those pairs, including TXT records. Include every same-name/type record that must remain, or use ADD_MISSING when unrelated values must be preserved. Existing matching records retain their provider identifiers; unmatched records are reused for updates before additional records are created or excess records are deleted.
Value comparison is case-insensitive and ignores one trailing dot for CNAME, MX, and NS hostnames. TXT presentation quotes are removed (and quoted TXT character strings are concatenated) before exact comparison. MX/SRV priorities participate in replacement matching; replacement also updates a matched record when its TTL differs. Add-missing mode does not modify an existing record's TTL or priority.
Mail DNS example
Replace the example targets and DKIM key with values issued by the mail provider. This example synchronizes the complete desired TXT set for the apex and _dmarc; include any existing verification TXT values at those same names/types if they must be retained.
use AdachSoft\DnsZoneContracts\Model\DnsRecord;
use AdachSoft\DnsZoneContracts\Model\DnsRecordCollection;
use AdachSoft\DnsZoneContracts\Model\DnsRecordTypeEnum;
use AdachSoft\DnsZoneContracts\Model\RecordName;
use AdachSoft\DnsZoneContracts\Model\SyncModeEnum;
use AdachSoft\DnsZoneContracts\Model\ZoneName;
use AdachSoft\DnsZoneContracts\Service\RecordSetSynchronizer;
use AdachSoft\DnsZoneContracts\Service\DnsZoneInterface;
/** @var DnsZoneInterface $dnsZone Provider adapter configured by the application */
$zoneName = new ZoneName('example.com');
$desiredRecords = new DnsRecordCollection([
new DnsRecord(new RecordName(''), DnsRecordTypeEnum::MX, 'mx1.mail-provider.example', 3600, 10),
new DnsRecord(new RecordName(''), DnsRecordTypeEnum::MX, 'mx2.mail-provider.example', 3600, 20),
new DnsRecord(new RecordName(''), DnsRecordTypeEnum::TXT, 'v=spf1 include:spf.mail-provider.example ~all', 3600),
new DnsRecord(
new RecordName('selector._domainkey'),
DnsRecordTypeEnum::TXT,
'v=DKIM1; k=rsa; p=REPLACE_WITH_PROVIDER_KEY',
3600,
),
new DnsRecord(
new RecordName('_dmarc'),
DnsRecordTypeEnum::TXT,
'v=DMARC1; p=quarantine; rua=mailto:dmarc@example.com',
3600,
),
]);
$changes = (new RecordSetSynchronizer($dnsZone))->ensure(
$zoneName,
$desiredRecords,
SyncModeEnum::REPLACE_SAME_NAME_AND_TYPE,
);
if ($changes->isEmpty() === false) {
// The provider adapter has published the changes before ensure() returns.
printf("Created %d, updated %d, and deleted %d records.\n", count($changes->created), count($changes->updated), count($changes->deleted));
}
Use ADD_MISSING instead when records should only be added and no existing values should be changed or removed. To preview an operation without writing or publishing changes, pass dryRun: true to ensure(); the returned DnsChangeSet contains the planned records.
Implementing a provider
Implement DnsZoneInterface and map the provider API to the package model:
- Convert provider record names to relative
RecordNamevalues and return aDnsRecordIdfor every persisted record. - Translate provider record types, values, TTLs, and MX/SRV priorities to
DnsRecordinstances. Keep TXT values unquoted in the contract model and quote/unquote them inside the adapter as required by the provider. - Require a record identifier for updates and use identifiers for update/delete operations.
- Implement
applyChanges()as the provider's explicit zone refresh when needed; use a no-op when changes are immediate. - Translate provider authentication, rate-limit, missing-zone/record, invalid-value, and transport failures to the corresponding package exceptions.
InMemoryDnsZone is included as a working implementation for tests and small integrations. It registers ZoneName instances in its constructor and exposes appliedCount() to verify calls to applyChanges().