48 string $protocol =
'tcp',
49 ?
string $domain =
null,
50 ?callable $resolver =
null
52 $this->service = $service;
53 $this->protocol = $protocol;
54 $this->domain = $domain;
56 $this->resolver = $resolver ??
static function ( $srv ) {
57 return dns_get_record( $srv, DNS_SRV );
71 $records = ( $this->resolver )( $this->getSrvName() );
77 || ( count( $records ) === 1 && $records[0][
'target'] ===
'.' )
82 foreach ( $records as $record ) {
84 'target' => $record[
'target'],
85 'port' => (int)$record[
'port'],
86 'pri' => (
int)$record[
'pri'],
87 'weight' => (int)$record[
'weight'],
102 $records = $this->getRecords();
104 usort( $records,
static fn ( $a, $b ) =>
105 $a[
'pri'] <=> $b[
'pri'] ?:
106 ( mt_rand( 0, 1 ) ? 1 : -1 )
109 $serversAndPorts = [];
111 foreach ( $records as $record ) {
112 $serversAndPorts[] = [ $record[
'target'], $record[
'port'] ];
115 return $serversAndPorts;
122 $srv =
"_{$this->service}._{$this->protocol}";
124 if ( $this->domain ===
null || $this->domain ===
'' ) {
128 return "$srv.{$this->domain}";
133class_alias( DnsSrvDiscoverer::class,
'DnsSrvDiscoverer' );