Documentation

HostsFile
in package

Represents a static hosts file which maps hostnames to IPs

Hosts files are used on most systems to avoid actually hitting the DNS for certain common hostnames.

Most notably, this file usually contains an entry to map "localhost" to the local IP. Windows is a notable exception here, as Windows does not actually include "localhost" in this file by default. To compensate for this, this class may explicitly be wrapped in another HostsFile instance which hard-codes these entries for Windows (see also Factory).

This class mostly exists to abstract the parsing/extraction process so this can be replaced with a faster alternative in the future.

Table of Contents

Properties

$contents  : mixed

Methods

__construct()  : mixed
Instantiate new hosts file with the given hosts file contents
getDefaultPath()  : string
Returns the default path for the hosts file on this system
getHostsForIp()  : array<string|int, string>
Returns all hostnames for the given IPv4 or IPv6 address
getIpsForHost()  : array<string|int, string>
Returns all IPs for the given hostname
loadFromPathBlocking()  : self
Loads a hosts file (from the given path or default location)

Properties

Methods

__construct()

Instantiate new hosts file with the given hosts file contents

public __construct(string $contents) : mixed
Parameters
$contents : string

getDefaultPath()

Returns the default path for the hosts file on this system

public static getDefaultPath() : string
Tags
codeCoverageIgnore
Return values
string

getHostsForIp()

Returns all hostnames for the given IPv4 or IPv6 address

public getHostsForIp(string $ip) : array<string|int, string>
Parameters
$ip : string
Return values
array<string|int, string>

getIpsForHost()

Returns all IPs for the given hostname

public getIpsForHost(string $name) : array<string|int, string>
Parameters
$name : string
Return values
array<string|int, string>

loadFromPathBlocking()

Loads a hosts file (from the given path or default location)

public static loadFromPathBlocking([string|null $path = null ]) : self

Note that this method blocks while loading the given path and should thus be used with care! While this should be relatively fast for normal hosts file, this may be an issue if this file is located on a slow device or contains an excessive number of entries. In particular, this method should only be executed before the loop starts, not while it is running.

Parameters
$path : string|null = null

(optional) path to hosts file or null=load default location

Tags
throws
RuntimeException

if the path can not be loaded (does not exist)

Return values
self

        
On this page

Search results