utopia-php/config 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

utopia-php/config

最新稳定版本:1.0.0

Composer 安装命令:

composer require utopia-php/config

包简介

A simple Config library to managing application config variables

README 文档

README

Build Status Total Downloads Discord

Utopia Config library is simple and lite library for managing application configuration. This library is aiming to be as simple and easy to learn and use. This library is maintained by the Appwrite team.

Although this library is part of the Utopia Framework project it is dependency free and can be used as standalone with any other PHP project or framework.

Getting Started

Install using composer:

composer require utopia-php/config
<?php

require_once './vendor/autoload.php';

use Utopia\Config\Parser\JSON;
use Utopia\Config\Attribute\Key;
use Utopia\Config\Config;
use Utopia\Config\Exception\Load;
use Utopia\Config\Exception\Parse;
use Utopia\Config\Source\File;
use Utopia\Validator\ArrayList;
use Utopia\Validator\Integer;
use Utopia\Validator\JSON as JSONValidator;
use Utopia\Validator\Nullable;
use Utopia\Validator\Text;

class DatabaseConfig
{
    #[Key('db.host', new Text(length: 1024), required: true)]
    public string $host;

    #[Key('db.port', new Integer(loose: true), required: false)]
    public ?int $port;

    #[Key('db.username', new Text(length: 1024), required: true)]
    public string $username;

    #[Key('db.password', new Text(length: 1024), required: true)]
    public string $password;
    
    #[Key('db.name', new Nullable(new Text(length: 1024)), required: true)]
    public ?string $name;

    /**
     * @var array<string, mixed> $config
     */
    #[Key('db.config', new Nullable(new JSONValidator), required: true)]
    public ?array $config;

    /**
     * @var array<string> $whitelistIps
     */
    #[Key('db.whitelistIps', new ArrayList(new Text(length: 100), length: 100), required: true)]
    public array $whitelistIps;
}

$source = new File(__DIR__.'/config.json');
$parser = new JSON();

try {
    $config = Config::load($source, $parser, DatabaseConfig::class);
} catch (Load $err) {
    exit('Config could not be loaded from a file: ' . $err->getMessage());
} catch (Parse $err) {
    exit('Config could not be parsed as JSON: ' . $err->getMessage());
}

\var_dump($config);
// $config->host
// $config->port
// $config->username
// ...

For above example to work, make sure to setup config.json file too:

{
  "db": {
    "host": "127.0.0.1",
    "port": 3306,
    "username": "root",
    "password": "password",
    "name": "utopia",
    "config": {
      "timeout": 3000,
      "handshakeTimeout": 5000
    },
    "whitelistIps": [
        "127.0.0.1",
        "172.17.0.0/16"
    ]
  },
}

Notice dot notation is supported, and automatically finds nested objects.

Alternatively, you can load configs directly from a variable:

<?php

require_once './vendor/autoload.php';

use Utopia\Config\Attribute\Key;
use Utopia\Config\Config;
use Utopia\Config\Source\Variable;
use Utopia\Config\Parser\None;
use Utopia\Validator\Whitelist;

class FirewallConfig
{
    #[Key('security-level', new Whitelist('high', 'low'), required: true)]
    public string $securityLevel;
}

$config = Config::load(
    source: new Variable([
        'security-level' => 'high',
    ]),
    parser: new None(),
    FirewallConfig::class
);
\var_dump($config);
// $config->securityLevel

Below is example how to combine multiple configs into one:

<?php
class FirewallConfig
{
    /**
     * @var array<string> $allowIps
     */
    #[Key('ALLOW_IPS', new ArrayList(new Text(length: 100), length: 100), required: true)]
    public array $allowIps;
    
    #[Key('CAPTCHA', new Whitelist(['enabled', 'disabled']), required: true)]
    public string $captcha;
}

class CredentialsConfig
{
    #[Key('DATABASE_PASSWORD', new Text(length: 1024), required: true)]
    public string $dbPass;
    
    #[Key('CACHE_PASSWORD', new Text(length: 1024), required: true)]
    public string $cachePass;
}

class EnvironmentConfig
{
    #[Key('RATE_LIMIT_HITS', new Integer(loose: true), required: true)]
    public int $abuseHits;
    
    #[Key('RATE_LIMIT_SECONDS', new Integer(loose: true), required: true)]
    public int $abuseTime;   
}

class AppConfig
{
    #[ConfigKey]
    public FirewallConfig $firewall;
    
    #[ConfigKey]
    public CredentialsConfig $credentials;
    
    #[ConfigKey]
    public EnvironmentConfig $environment;
}

$config = Config::load(
  new Variable([
    'firewall' => Config::load(new File('firewall.json'), new JSON(), FirewallConfig::class),
    'credentials' => Config::load(new File('credentials.yml'), new YAML(), CredentialsConfig::class),
    'environment' => Config::load(new File('.env'), new Dotenv(), EnvironmentConfig::class),
  ]),
  new None(),
  AppConfig::class
);

\var_dump($config);
// $config->firewall->allowIps
// $config->credentials->dbPass
// $config->environment->abuseHits

You can also load environment variables instead of reading dotenv file:

<?php

use Utopia\Config\Config;
use Utopia\Config\Source\Environment;
use Utopia\Config\Parser\None;

class CredentialsConfig
{
    #[Key('DATABASE_PASSWORD', new Text(length: 1024), required: true)]
    public string $dbPass;
    
    #[Key('CACHE_PASSWORD', new Text(length: 1024), required: true)]
    public string $cachePass;
}

$config = Config::load(new Environment(), new None(), CredentialsConfig::class);

\var_dump($config);
// $config->dbPass
// $config->$cachePass

System Requirements

Utopia Framework requires PHP 8.0 or later. We recommend using the latest PHP version whenever possible.

When using YAML adapter, or running tests with it, you need to install the YAML extension for PHP.

Copyright and license

The MIT License (MIT) http://www.opensource.org/licenses/mit-license.php

统计信息

  • 总下载量: 160.46k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 14
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 14
  • Watchers: 7
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固