northern-lights/hostsfile-parser

主机文件解析器

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

This package is auto-updated.

Last update: 2024-09-09 09:36:09 UTC


README

Maintainability

轻量级且简单的库,用于解析 hosts文件。支持所有使用此格式的操作系统。

安装

通过Composer

$ composer require northern-lights/hostsfile-parser

真的非常简单!

用法

<?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;
}

请注意,NorthernLights\HostsFileParser\Parser::parse() 是一个生成器。

要一次性解析所有内容并返回数组,可以使用 Parser::parseAll(),尽管不推荐,因为它可能在某些情况下导致内存溢出。

PSR-2标准

库努力遵守PSR-2编码标准,因此我们包括了以下命令

$ composer check-style
$ composer fix-style

注意:第二个命令实际上会修改文件

PSR-4标准

库遵守PSR-4自动加载标准

测试

$ composer php-lint
$ composer test

许可证

MIT许可证(MIT)。请参阅许可证文件以获取更多信息。