60 string $protocol =
'tcp',
61 ?
string $domain =
null,
62 ?callable $resolver =
null
64 $this->service = $service;
65 $this->protocol = $protocol;
66 $this->domain = $domain;
68 $this->resolver = $resolver ??
static function ( $srv ) {
69 return dns_get_record( $srv, DNS_SRV );
83 $records = ( $this->resolver )( $this->
getSrvName() );
89 || ( count( $records ) === 1 && $records[0][
'target'] ===
'.' )
94 foreach ( $records as $record ) {
96 'target' => $record[
'target'],
97 'port' => (int)$record[
'port'],
98 'pri' => (
int)$record[
'pri'],
99 'weight' => (int)$record[
'weight'],
116 usort( $records,
static function ( $a, $b ) {
117 if ( $a[
'pri'] === $b[
'pri'] ) {
118 return mt_rand( 0, 1 ) ? 1 : -1;
121 return $a[
'pri'] - $b[
'pri'];
124 $serversAndPorts = [];
126 foreach ( $records as $record ) {
127 $serversAndPorts[] = [ $record[
'target'], $record[
'port'] ];
130 return $serversAndPorts;
139 $srv =
"_{$this->service}._{$this->protocol}";
141 if ( $this->domain ===
null || $this->domain ===
'' ) {
145 return "$srv.{$this->domain}";
__construct(string $service, string $protocol='tcp', ?string $domain=null, ?callable $resolver=null)
Construct a new discoverer for the given domain, service, and protocol.
getRecords()
Queries the resolver for an SRV resource record matching the service, protocol, and domain and return...
getServers()
Performs discovery for the domain, service, and protocol, and returns a list of resolved server name/...
getSrvName()
Returns the SRV resource record name.