rollerworks/pdb-symfony-bridge

PHP 域名解析器 Bridge 用于 Symfony 缓存、HTTP 客户端和 FrameworkBundle(可选)

v1.0.0 2023-11-02 12:30 UTC

This package is auto-updated.

Last update: 2024-08-30 01:45:59 UTC


README

本包提供了 Jeremy Kendall 和 Ignace Nyamagana Butera 的 PHP 域名解析器的 Symfony 特定桥接器。

提供缓存适配器、HTTP 客户端适配器,并在 Rollerworks\Component\PdbSfBridge\Bundle\RollerworksPdbBundle 中提供可选的 FrameworkBundle 集成。

安装

要安装此包,请将 rollerworks/pdb-symfony-bridge 添加到您的 composer.json 文件中

$ php composer.phar require rollerworks/pdb-symfony-bridge

现在,Composer 将自动下载所有必需的文件,并为您安装它们。

Symfony Flex(含 contrib)假设启用 Bundle 并添加所需配置。 https://symfony.ac.cn/doc/current/bundles.html

否则,请添加以下配置

rollerworks_pdb:
    cache_pool: 'rollerworks.cache.public_prefix_db'
    #manager: http # either: 'http' (default), 'static' (requires manual updates) or 'mock'

framework:
    cache:
        pools:
            # This name can be changed by setting `rollerworks_pdb.cache_pool` (**Don't reuse an existing cache pool!**)
            rollerworks.cache.public_prefix_db:
                adapter: cache.adapter.array # use a persistent adapter that can be easily invalidated like cache.adapter.memcached or cache.adapter.pdo
                default_lifetime: 604800 # one week, the cache should be automatically refreshed, unless manager=static is used

需求

您需要至少 PHP 8.1,推荐但不强制要求互联网访问。

公共后缀和顶级域名需要定期手动更新。当互联网访问可用时,PdbManager 将自动下载列表并将其存储在缓存中。

如果无法访问互联网,则需要手动刷新本地缓存。(目前尚不支持)。

基本用法

使用 HttpUpdatedPdpManager 获取一个 PdbManager,它将自动使用 HttpClient 更新本地缓存。

<?php

use Rollerworks\Component\PdbSfBridge\HttpUpdatedPdpManager;
use Symfony\Component\Cache\Psr16Cache(;

// Any PSR16 Compatible adapter can be used, but it's recommended to use
// A caching adapter that allows easy invalidation (like Pdo or Memcache).
$cacheAdapter = ...;
$cache = new Psr16Cache($cacheAdapter);

// Optional, if not provided created as shown
// $httpClient = new \Symfony\Component\HttpClient\HttpClient::create();

$manager = HttpUpdatedPdpManager::create($cache, /*$httpClient*/);

// Not required. But recommended to warm-up the caches.
$manager->updateCaches();

// \Pdp\PublicSuffixList
$publicSuffixList = $manager->getPublicSuffixList();

// \Pdp\PublicSuffixList
$topLevelDomainList = $manager->getTopLevelDomainList();

当无法访问互联网时,请使用 StaticPdpManager

<?php

use Rollerworks\Component\PdbSfBridge\StaticPdpManager;

// Any PSR16 Compatible adapter can be used, but it's recommended to use
// A caching adapter that allows easy invalidation (like Pdo or Memcache).
$cacheAdapter = ...;
$cache = new Psr16Cache($cacheAdapter);

// Provide the lists as described, realpath (not contents are string)
$publicSuffixList = ...; // File provided from https://publicsuffix.org/list/public_suffix_list.dat
$topLevelDomainList ...; // File provided from https://data.iana.org/TLD/tlds-alpha-by-domain.txt

$manager = new StaticPdpManager($publicSuffixList, $topLevelDomainList, $cache);

// Not required. But recommended to warm-up the caches.
$manager->updateCaches();

// \Pdp\PublicSuffixList
$publicSuffixList = $manager->getPublicSuffixList();

// \Pdp\PublicSuffixList
$topLevelDomainList = $manager->getTopLevelDomainList();

有关域名解析,请参阅 [PHP 域名解析器] 的官方文档。

测试

在测试中使用管理器时,请使用 \Rollerworks\Component\PdbSfBridge\PdpMockProvider::getPdpManager()

Bundle 使用

首先设置一个合适的缓存适配器(Flex 脚本使用 ArrayCache 允许启动 Kernel,但对于生产使用,需要使用持久缓存适配器,否则每次初始化 Manager 时都需要下载、解析和缓存规则)。

# config/packages/rollerworks_pdb.yaml

rollerworks_pdb:
    cache_pool: 'rollerworks.cache.public_prefix_db'

framework:
    cache:
        pools:
            # This name can be changed by setting `rollerworks_pdb.cache_pool` (**Don't reuse an existing cache pool!**)
            rollerworks.cache.public_prefix_db:
                adapter: cache.adapter.memcached # or cache.adapter.pdo
                default_lifetime: 604800 # one week, the cache should be automatically refreshed

离线使用

当 HttpClient 组件已安装并启用时,将自动使用 HttpUpdatedPdpManager。当 HttpClient 不可用时,将使用 StaticPdpManager

要强制使用静态适配器,请将配置 rollerworks_pdb.manager 设置为 static

当启用静态适配器时,您必须使用提供的文件名运行 rollerworks-pdb:update

并禁用缓存过期,否则将使用预打包版本。

首先从 https://publicsuffix.org/list/public_suffix_list.dathttps://data.iana.org/TLD/tlds-alpha-by-domain.txt 分别下载列表。

并将它们加载到缓存中。

$ bin/console rollerworks-pdb:update public_suffix_list.dat tlds-alpha-by-domain.txt

文件路径可以是绝对路径,也可以是相对于当前工作目录的相对路径。

禁用缓存过期: Symfony 缓存预热设计为一旦缓存预热,就不期望未来的文件写入(出于安全原因)。

这就是为什么缓存应该很容易更新,最好没有文件系统写入。

rollerworks-pdb:update更新缓存而不将提供的文件写入应用程序的var/cache目录。因此,当缓存过期时,没有文件可以读取,为了避免这种情况,将预打包的列表(用于MockManager)作为替代,但这些列表仅定期更新,因此可能已过时。

版本控制

为了提高透明度和对发布周期的洞察力,并努力保持向后兼容性,尽可能按照语义版本控制指南维护此包。

版本号将采用以下格式

<主版本>.<次版本>.<修补版本>

并遵循以下指南构建

  • 破坏向后兼容性将增加主版本(并重置次版本和修补版本)
  • 在不破坏向后兼容性情况下添加的新功能将增加次版本(并重置修补版本)
  • 错误修复和其他更改将增加修补版本

有关SemVer的更多信息,请访问http://semver.org/

许可

此库在MIT许可下发布。

贡献

这是一个开源项目。如果您想做出贡献,请阅读贡献指南。如果您正在提交拉取请求,请遵循提交补丁部分的指南。