los/losdomain

基于域名或子域名的PHP配置中间件

2.2.0 2022-02-11 10:14 UTC

This package is auto-updated.

Last update: 2024-09-11 15:59:27 UTC


README

Build Status Latest Stable Version Total Downloads Coverage Status Scrutinizer Code Quality SensioLabs Insight Dependency Status

简介

此模块提供基于访问的域名(或子域名)的PHP配置文件。

对于Zend Framework 2/3 框架.zend.com,请使用版本1.0。

待办事项

待办事项

  • 手动禁用域名的选项,将其重定向到页面。例如,已过期的订阅。
  • 与LosLicense(开发中)集成以检查域名的可用性并自动禁用它。
  • 存储以检索域名的选项(接口、文件、pdo等)

安装

composer require los/losdomain:^2.0

使用方法

例如,在 Zend Expressive 中,将以下代码添加到您的 config/config.php 文件中

foreach (\LosDomain\DomainService::configFiles('config/autoload/domains') as $file) {
    $config = ArrayUtils::merge($config, include $file);
}

由于包含的文件无法缓存,最好在读取/写入缓存之后包含代码。因此,完整的 config.php 文件将类似于

<?php

use Laminas\Stdlib\ArrayUtils;
use Laminas\Stdlib\Glob;

/**
 * Configuration files are loaded in a specific order. First ``global.php``, then ``*.global.php``.
 * then ``local.php`` and finally ``*.local.php``. This way local settings overwrite global settings.
 *
 * The configuration can be cached. This can be done by setting ``config_cache_enabled`` to ``true``.
 *
 * Obviously, if you use closures in your config you can't cache it.
 */

require __DIR__.'/env.php';

$cachedConfigFile = 'data/cache/app_config.php';

$config = [];
if (is_file($cachedConfigFile)) {
    // Try to load the cached config
    $config = include $cachedConfigFile;
} else {
    // Load configuration from autoload path
    foreach (Glob::glob('config/autoload/{{,*.}global,{,*.}local}.php', Glob::GLOB_BRACE) as $file) {
        $config = ArrayUtils::merge($config, include $file);
    }

    // Cache config if enabled
    if (isset($config['config_cache_enabled']) && $config['config_cache_enabled'] === true) {
        file_put_contents($cachedConfigFile, '<?php return ' . var_export($config, true) . ';');
    }
}

foreach (\LosDomain\DomainService::configFiles('config/autoload/domains') as $file) {
    $config = ArrayUtils::merge($config, include $file);
}

// Return an ArrayObject so we can inject the config as a service in Aura.Di
// and still use array checks like ``is_array``.
return new ArrayObject($config, ArrayObject::ARRAY_AS_PROPS);

服务将在 'config/autoload/domains' 文件夹中查找您的域名。如果存在与域名相同名称的文件夹,则将其导入。

例如,假设以下域名

每个域名(或子域名)都可以有不同的配置(工厂、数据库配置等)

由于client2.test.local没有配置,它将使用项目的默认配置。

域名别名

可以为域名分配别名。假设您有两个共享相同配置的域名

第一个按常规创建(在 config/autoload/domain/test.local/global.php 内创建配置文件)并且别名在 config/autoload/domains/config.php 文件中配置

<?php
return [
    'los_domain' => [
        'aliases' => [
            'www.test.local' => 'test.local',
        ],
    ],
];

使用此配置,当项目通过 'www.test.local' 访问时,服务将加载 'test.local' 文件。