Provides a class to add/remove lines from the hosts file. It has been tested to work on *nix based systems, otherwise you'll need to supply the path to the hosts file.
First, you'll need to download the hostsfile manager library using composer:
composer require droath/hostsfile-manager:^0.0.1
Add lines to the hosts file contents:
The below code appends two entries to the hosts file contents. If any of those lines already exists then nothing is appended.
<?php
$hosts_file = (new \Droath\HostsFileManager\HostsFile())
->setLine('127.0.0.1', 'local.sickslap.com')
->setLine('127.0.0.2', 'local.hiphopsmack.com');
(new \Droath\HostsFileManager\HostsFileWriter($hosts_file))
->add();
Remove a single line from hosts file:
The below code removes the one entry from the hosts file. All other lines within the hosts file remain untouched.
<?php
$hosts_file = (new \Droath\HostsFileManager\HostsFile())
->setLine('127.0.0.2', 'local.hiphopsmack.com');
(new \Droath\HostsFileManager\HostsFileWriter($hosts_file))
->remove();