xy2z/lite-config
Composer 安装命令:
composer require xy2z/lite-config
包简介
A lightweight PHP static class with zero dependencies.
README 文档
README
A lightweight PHP static class with zero dependencies.
Supports multiple configs and multidimensional arrays.
Built-in support for PHP, INI and JSON files. Also supports YAML and anything else you can parse to an array, see custom_handler() below.
Requirements
- PHP 7.0 +
Install
composer require xy2z/lite-config
Examples
require 'path/to/vendor/autoload.php'; use xy2z\LiteConfig\LiteConfig as Config;
Array
Config::loadArray([ 'version' => '1.0', 'app' => [ 'name' => 'Example' ] ]); echo Config::get('version'); # 1.0 echo Config::get('app'); # Array('name' => 'Example') echo Config::get('app.name', 'default name'); # Example echo Config::get('app.type', 'Desktop'); # Desktop
Directory
# config/settings.php return [ 'app_name' => 'Example', ]; # index.php Config::loadDir('config/', true); echo Config::get('settings.app_name'); # key is 'filename.key'
Single file
# No key prefix Config::loadFile('config/settings.php'); echo Config::get('key'); # Prefix filename to key Config::loadFile('config/db.ini', true); echo Config::get('db.key');
YAML
To load a single file as YAML see below. If you need to use loadDir() with yaml (or other) files, then read about "Custom Handler" below.
composer require symfony/yaml
use xy2z\LiteConfig\LiteConfig as Config; use Symfony\Component\Yaml\Yaml; Config::loadArray(Yaml::parseFile(__DIR__ . '/config/file.yml')); echo Config::get('key');
Custom Handler
A custom handler can be used for file extensions other than the built in (.php, .json and .ini). This will automatically work when using the loadFile() and loadDir() functions.
Here's how you use the static custom_handler() function if you want YAML support.
use xy2z\LiteConfig\LiteConfig; use Symfony\Component\Yaml\Yaml; class CustomLiteConfig extends LiteConfig { protected static function custom_handler(string $extension, string $path) { if (($extension === 'yml') || ($extension === 'yaml')) { return Yaml::parseFile($path); } // Handle other extensions here... } } CustomLiteConfig::loadDir(__DIR__ . '/config', true); var_dump(CustomLiteConfig::all());
If you want to modify the existing handling of all files, you can overwrite the complete getFileContent() function in your child class, which is used by loadDir() and loadFile().
Public Methods
get(string $key, $default = null)Get value of key.all()Returns a complete array.exists(string $key)Does key exist?loadDir(string $path, bool $prefix_filename = false, string $prefix = null)Loads all files in dir.loadFile(string $path, bool $prefix_filename = false, string $custom_prefix = null)Load a single file.loadArray(array $array, string $prefix = null)Loads a php array.
Public Properties
ini_process_sections(bool, defaulttrue) - Argument used in file_parse_ini()ini_scanner_mode(int, defaultINI_SCANNER_TYPED) - Argument used in file_parse_ini()ignore_unsupported_filestypes(bool, defaulttrue) - If true, files with unsupported extensions will be ignored. If false, an exception will be thrown.
xy2z/lite-config 适用场景与选型建议
xy2z/lite-config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 401 次下载、GitHub Stars 达 6, 最近一次更新时间为 2018 年 09 月 21 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 xy2z/lite-config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xy2z/lite-config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 401
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 4
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-09-21