zooxsmart/los-domain

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

1.0.0 2023-10-30 17:52 UTC

This package is auto-updated.

Last update: 2024-08-30 01:44:27 UTC


README

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

简介

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

对于Zend Framework 2/3 framework.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'文件。