somephp/appconfig
Composer 安装命令:
composer require somephp/appconfig
包简介
Load and use a single instance of a JSON config file.
README 文档
README
A basic application config class that loads from a JSON file and returns a single run-time instance. The instance is writable.
- An object can be created from a JSON file.
- An object can be created from a section of a JSON file, one level deep.
- An object can be built at runtime.
- Only one instance of an object is returned.
- The returned object is writable.
License
MIT - MIT License File: LICENSE
Installation
Composer
composer require somephp/appconfig
Usage Examples
The following two examples show how to load a JSON config file and a section of a JSON config file. They use config.json. The last example shows how to build a configuration at runtime.
Example #1 create an object from a json file
<?php
require_once 'AppConfig.php';
try {
// Specify the path of a JSON file to load.
$config = AppConfig::load('config.json');
} catch (Exception $e) {
var_dump($e->getMessage());
exit;
}
echo "API: {$config->dev->api->example->url} \n";
echo "User: {$config->dev->db->mysql->user} \n";
echo "Passowrd: {$config->dev->db->mysql->pass} \n";
Example #1 output
API: https://example.com/dev/api
User: admin
Passowrd: love
Example #2 create an object from a sub section of a JSON file
<?php
class ServerConfig {
public static $env = 'prod';
}
require_once 'AppConfig.php';
try {
// Create an object from the "prod" section of a JSON file.
$config = AppConfig::load('config.json', ServerConfig::$env);
} catch (Exception $e) {
var_dump($e->getMessage());
exit;
}
// Display a copy the entire runtime object.
echo print_r($config->get(), true);
Example #2 output
stdClass Object
(
[db] => stdClass Object
(
[mysql] => stdClass Object
(
[server] => https://example.com
[user] => admin
[pass] => secret
[db] => currency_prod
)
[mongo] => stdClass Object
(
[collection] => currency_prod
[doc] =>
[server] => mongodb://127.0.0.1:27017
)
)
[api] => stdClass Object
(
[example] => stdClass Object
(
[url] => https://example.com/api
[secret] => 74c42a009d7c0fcdb1aa4a853b37977f
[key] => 85bb2e1519dbb2c2421b5cad6039bfcd
[pass] => realsecret5realkey
)
)
)
Example #3 build an object at runtime
<?php
require_once 'AppConfig.php';
try {
// Create an empty config object.
$config = AppConfig::load();
} catch (Exception $e) {
var_dump($e->getMessage());
exit;
}
echo 'config->api: '. print_r($config->api, true). PHP_EOL;
echo 'config->db: '. print_r($config->db, true). PHP_EOL;
echo PHP_EOL;
// Add a property.
@$config->api->example->url = 'http://example.com/api';
// Add an object.
$config->db = (object) [
'mysql' => (object) [
'server' => 'localhost',
'user' => 'admin',
'pass' => 'love',
'db' => 'currency_dev'
]
];
// load() will return the same instance of the config object.
$configTwo = AppConfig::load();
echo 'configTwo->api: '. print_r($configTwo->api, true). PHP_EOL;
echo 'configTwo->db: '. print_r($configTwo->db, true). PHP_EOL;
Example #3 output
config->api:
config->db:
configTwo->api: stdClass Object
(
[example] => stdClass Object
(
[url] => http://example.com/api
)
)
configTwo->db: stdClass Object
(
[mysql] => stdClass Object
(
[server] => localhost
[user] => admin
[pass] => love
[db] => currency_dev
)
)
Contents
| Resource | Description |
|---|---|
Contributions
Suggestions and code modifications are welcome. Create a merge request, and tell me what you are thinking.
somephp/appconfig 适用场景与选型建议
somephp/appconfig 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 21 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 08 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 somephp/appconfig 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 somephp/appconfig 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 21
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-08-04