utopia-php/config
Composer 安装命令:
composer require utopia-php/config
包简介
A simple Config library to managing application config variables
关键字:
README 文档
README
Important
This repository is a read-only mirror of the utopia-php monorepo. Development happens in packages/config — please open issues and pull requests there.
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
utopia-php/config 适用场景与选型建议
utopia-php/config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 298.33k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2020 年 03 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「framework」 「php」 「config」 「upf」 「utopia」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 utopia-php/config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 utopia-php/config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 utopia-php/config 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP Framework HLEB2 is the foundation of the web application. Provides ease of development and application performance.
A Zend Framework module to quickly and easily set PHP settings.
Server environment detection based on config array-file
Single and multi-dimensional parameter bag with dot-path access for PHP.
Alfabank REST API integration
Laravel provider to be able to rewrite configuration
统计信息
- 总下载量: 298.33k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 12
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-25