jasny/config
最新稳定版本:v2.0.1
Composer 安装命令:
composer require jasny/config
包简介
Configure your application
README 文档
README
Configure your application. Implements PSR-11 to be used as container.
Installation
Jasny Config is registred at packagist as jasny/config and can be easily installed using composer.
composer require jasny/config
Usage
use Jasny\Config; $config = (new Config()) ->load('settings.yml') ->load('settings.dev.yml', ['optional' => true]);
Loaders
- Dir
- Ini
- Json
- Yaml
- Symfony
- Yaml extension
- Env
- DynamoDB
The DelegateLoader can pick a loader based on class name or file extension.
For every loader; a Jasny\Config\LoadException is thrown if the loader can't load the settings.
Dir
DirLoader loader will traverse through every file in a directory. It uses the filename (without extension) as key name.
| option | type | default | |
|---|---|---|---|
| recursive | bool | false | Traverse recursively through subdirectories |
| optional | bool | false | Return empty config is directory doesn't exist |
| base_path | string | getcwd() | Basepath for relative paths |
Ini
IniLoader parses an INI file using parse_ini_file.
| option | type | default | |
|---|---|---|---|
| process_sections | bool | false | Group settings by section name |
| mode | enum | INI_SCANNER_NORMAL | Mode of parsing options (off = false, etc) |
| optional | bool | false | Return empty config is file doesn't exist |
| base_path | string | getcwd() | Basepath for relative paths |
Json
JsonLoader parses a JSON file using json_decode.
| option | type | default | |
|---|---|---|---|
| optional | bool | false | Return empty config is file doesn't exist |
| base_path | string | getcwd() | Basepath for relative paths |
Yaml
YamlLoader parses a YAML file using yaml_parse.
| option | type | default | |
|---|---|---|---|
| num | int | 0 | Document to extract from stream (0 is first doc) |
| callbacks | array | [] | Content handlers for YAML nodes |
| optional | bool | false | Return empty config is file doesn't exist |
| base_path | string | getcwd() | Basepath for relative paths |
The YamlLoader is used by default if the yaml extension is loaded.
Symfony\Yaml
YamlSymfonyLoader is an alternative loader for YAML, using the Symfony Yaml component.
| option | type | default | |
|---|---|---|---|
| optional | bool | false | Return empty config is file doesn't exist |
| base_path | string | getcwd() | Basepath for relative paths |
When constructing the loader a Symfony\Component\Yaml\Parser object may be passed.
use Symfony\Component\Yaml; $parser = new class() extends Yaml\Parser() { public function parse($value, $options = 0) { return parent::parseFile($value, $options | Yaml\Yaml::PARSE_CONSTANT); } } $yamlLoader = new YamlSymfonyLoader($options, $parser);
Env
EnvLoader maps environment variables to settings. In the mapping the settings key supports dot notation.
| option | type | default | |
|---|---|---|---|
| map | array | (required) | key/value pairs from env name to setting key |
Example
$map = [ "APP_SECRET" => "secret", "APP_DATABASE_PASSWORD" => "db.password" ]; $config->load('env', ['map' => $map]);
DynamoDB
DynamoDBLoader allows loading settings from an AWS DynamoDB database. It expects all settings to be in a single item.
It will not do a table scan. By default the whole item is taken as settings. Alternatively, use the settings_field
option to specify a Map field that holds the settings.
| option | type | default | |
|---|---|---|---|
| table | string | (required) | DynamoDB table name |
| key_field | string | 'key' | Indexed field (typically primary index) |
| key_value | string | (required) | Value to select item on |
| settings_field | string | null | within the item that holds the settings |
Example
$dynamodb = Aws\DynamoDb\DynamoDbClient::factory([ 'region' => 'eu-west-1', 'version' => '2012-08-10' ]); * $config = new Jasny\Config(); $config->load($dynamodb, [ 'table' => 'config', 'key_field' => 'key', 'key_value' => 'myapp' ]);
DelegateLoader
The DelegateLoader is a map with loaders, than can automatically pick a loader based on the file extension or class
name.
By default it holds all the loaders of this library. You may pass an array of loaders as dependency injection.
use Jasny\Config\Loader; $loader = new Loader\DelegateLoader([ 'yaml' => new Loader\YamlLoader(['base_path' => __DIR__ . '/config']), 'json' => new Loader\JsonLoader(['base_path' => __DIR__]) ]); $config = new Config([], $loader); $config->load('composer.json'); $config->load('settings.yml');
Config
The Config object is a dynamic object; settings are added as public properties. It extends the stdClass object.
Upon construction you can pass settings as associative array or stdClass object.
$config = new Jasny\Config([ 'foo' => 'bar', 'db' => [ 'host' => 'localhost', 'username' => 'root', 'password' => 'god' ] ]);
Optionally you can pass a loader. If your config is only in JSON files, consider passing the JsonLoader. This saves
the creation of the DelegateLoader and loaders for all other file types.
load()
Load new setting from file or other data source and add it to the config.
load(string|object $source, array $options = [])
This is a fluent interface, so the method returns $this.
Example
$config = (new Config()) ->load('composer.json') ->load('config/settings.yml') ->load($dynamoDB, ['table' => 'config', 'key_value' => 'myapp']);
merge()
Merge one or more Config (or stdClass) objects into the config.
merge(stdClass $settings, ...)
This is a fluent interface, so the method returns $this.
get()
The Config object implements the PSR-11 ContainerInterface.
The get method finds a setting in the config by key. The dot notation may be used to get child properties.
mixed get(string $key)
If the setting doesn't exist, a Jasny\Config\Exception\NotFoundException is thrown.
Example
$config->get('foo'); // bar $config->get('db.host'); // localhost // throws a NotFoundException $config->get('something_random');
has()
The has method is part of the PSR-11 ContainerInterface. It checks if a setting exists. As with get(), the key
supports the dot notation.
bool has(string $key)
saveAsScript() / loadFromScript()
The saveAsScript() method create parsable string representation of a variable using
var_export and store it as a PHP script.
As described in the 500x faster caching article, storing the parsed configuration as a PHP script can make loading it much faster.
The loadFromScript() method can be used to load the settings. It's recommended to use this function rather than just
file_exists and include, as checking the file exists would always hit the file system. This function checks the
opcode cache first.
$config = Config::loadFromScript('tmp/settings.php'); if (!isset($config)) { $config = (new Config()) ->load('composer.json') ->load('config/settings.yml'); $config->saveAsScript('tmp/settings.php'); }
jasny/config 适用场景与选型建议
jasny/config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22.33k 次下载、GitHub Stars 达 20, 最近一次更新时间为 2012 年 11 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「yaml」 「json」 「configuration」 「db」 「mysql」 「ini」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jasny/config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jasny/config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jasny/config 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Configuration component
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
Utils to load, parse and work with configuration on Mezzio projects
SUML for PHP
统计信息
- 总下载量: 22.33k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 21
- 点击次数: 16
- 依赖项目数: 3
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2012-11-06