northern-lights/hostsfile-parser

dev-master / 1.0.x-dev 2020-08-09 00:41 UTC

This package is auto-updated.

Last update: 2024-04-09 08:34:53 UTC


README

Maintainability

Lightweight and simple library for parsing hosts file. All operating systems that use this format are supported.

Install

Via Composer

$ composer require northern-lights/hostsfile-parser

It really is that easy!

Usage

<?php

declare(strict_types=1);

namespace NorthernLights\HostsFileParser\Example;

use NorthernLights\HostsFileParser\Host;
use NorthernLights\HostsFileParser\HostsFile;
use NorthernLights\HostsFileParser\Parser;

require __DIR__ . '/vendor/autoload.php';

$parser = new Parser(new HostsFile('/etc/hosts'));

/** @var Host $host */
foreach ($parser->parse() as $host) {
    $domains = $host->getDomains();
    echo 'Host: ' . $host->getIp() . PHP_EOL;
    echo sprintf('Domains: %d -> { %s }', count($domains), implode(', ', $domains)) . PHP_EOL;
    echo 'Line: ' . $host->getLine() . PHP_EOL;
    echo sprintf('Raw entry: [ %s ]', $host) . PHP_EOL;
    echo '--------------------------------------' . PHP_EOL;
}

Please note that NorthernLights\HostsFileParser\Parser::parse() is a generator.

To parse all at once and return array, you can use Parser::parseAll() although discouraged as it could cause OOM in certain cases.

PSR-2 Standard

Library strives to comply with PSR-2 coding standards, therefore we included following commands:

$ composer check-style
$ composer fix-style

Note: Second command will actually modify files

PSR-4 Standard

Library complies with PSR-4 autoloading standard

Testing

$ composer php-lint
$ composer test

License

The MIT License (MIT). Please see License File for more information.