hiqdev/composer-config-plugin
最新稳定版本:0.4.0
Composer 安装命令:
composer require hiqdev/composer-config-plugin
包简介
Composer plugin for config assembling
README 文档
README
Composer Config Plugin
Composer plugin for config assembling.
⚠️ The plugin is no longer supported in favor of yiisoft/config.
This Composer plugin provides assembling of configurations distributed with composer packages. It allows putting configuration needed to use a package right inside of the package thus implementing a plugin system. The package becomes a plugin holding both the code and its configuration.
Documentation
How it works?
- Scans installed packages for
config-pluginextra option in theircomposer.json. - Loads
.envfiles to set$_ENVvariables. - Requires
constantsfiles to set constants. - Requires
paramsfiles. - Requires config files.
- Options collected during earlier steps could and should be used in later
steps, e.g.
$_ENVshould be used for constants and parameters, which in turn should be used for configs. - File processing order is crucial to achieve expected behavior: options in root package have priority over options from included packages. It is described below in File processing order section.
- Collected configs are written as PHP files in
vendor/yiisoft/composer-config-plugin-outputdirectory along with information needed to rebuild configs on demand. - Then assembled configs are ready to be loaded into application using
require.
Read more about the general idea behind this plugin in English or Russian.
Installation
composer require "yiisoft/composer-config-plugin"
Out of the box this plugin supports configs in PHP and JSON formats.
To enable additional formats require:
- vlucas/phpdotenv - for
.envfiles. - symfony/yaml - for YAML files,
.ymland.yaml.
Usage
List your config files in composer.json like the following:
"extra": { "config-plugin-output-dir": "path/relative-to-composer-json", "config-plugin": { "envs": "db.env", "params": [ "config/params.php", "?config/params-local.php" ], "common": "config/common.php", "web": [ "$common", "config/web.php" "../src/Modules/*/config/web.php" ], "other": "config/other.php" } },
Markers
-
?- marks optional files. Absence of files not marked with it will cause exception."params": [ "params.php", "?params-local.php" ]It's okay if
params-local.phpwill not found, but it's not okay ifparams.phpwill be absent. -
*- marks wildcard path. It means zero or more matches by wildcard mask."web": [ "../src/Modules/*/config/web.php" ]It will collect all
web.phpin any subfolders ofsrc/Modules/inconfigfolder. -
$- reference to another config."params": [ "params.php", "?params-local.php" ], "params-console": [ "$params", "params-console.php" ], "params-web": [ "$params", "params-web.php" ]Output files
params-console.phpandparams-web.phpwill containparams.phpandparams-local.php.
Define your configs like the following:
<?php return [ 'components' => [ 'db' => [ 'class' => \my\Db::class, 'name' => $params['db.name'], 'password' => $params['db.password'], ], ], ];
A special variable $params is read from params config.
To load assembled configs in your application use require:
$config = require Yiisoft\Composer\Config\Builder::path('web');
Using sub-configs
In some cases it is convenient to extract part of your config into another file. For example, we want to extract database
configuration into db.php. To do it add the config to composer.json:
"extra": { "config-plugin-output-dir": "path/relative-to-composer-json", "config-plugin": { "envs": "db.env", "params": [ "config/params.php", "?config/params-local.php" ], "common": "config/common.php", "web": [ "$common", "config/web.php" ], "other": "config/other.php", "db": "config/db.php" } },
Create db.php:
<?php return [ 'class' => \my\Db::class, 'name' => $params['db.name'], 'password' => $params['db.password'], ];
Then in the config use Builder::require():
<?php use Yiisoft\Composer\Config\Builder; return [ 'components' => [ 'db' => Builder::require('db'), ], ];
Refreshing config
Plugin uses composer POST_AUTOLOAD_DUMP event i.e. composer runs this plugin on install, update and dump-autoload
commands. As the result configs are ready to be used right after package installation or update.
When you make changes to any of configs you may want to reassemble configs manually. In order to do it run:
composer dump-autoload
Above can be shortened to composer du.
If you need to force config rebuilding from your application, you can do it like the following:
// Don't do it in production, assembling takes it's time if (getenv('APP_ENV') === 'dev') { Yiisoft\Composer\Config\Builder::rebuild(); }
File processing order
Config files are processed in proper order to achieve naturally expected behavior:
- Options in outer packages override options from inner packages.
- Plugin respects the order your configs are listed in
composer.jsonwith. - Different types of options are processed in the following order:
- Environment variables from
envs. - Constants from
constants. - Parameters from
params. - Configs are processed last of all.
- Environment variables from
Debugging
There are several ways to debug config building internals.
- Plugin can show detected package dependencies hierarchy by running:
composer dump-autoload --verbose
Above can be shortened to composer du -v.
- You can see the assembled configs in the output directory which is
vendor/yiisoft/composer-config-plugin-outputby default and can be configured withconfig-plugin-output-dirextra option incomposer.json.
Known issues
This plugin treats configs as simple PHP arrays. No specific structure or semantics are expected and handled. It is simple and straightforward, but I'm in doubt... What about errors and typos? I think about adding config validation rules provided together with plugins. Will it solve all the problems?
License
This project is released under the terms of the BSD-3-Clause license. Read more here.
Copyright © 2016-2020, HiQDev (http://hiqdev.com/) Copyright © 2020, Yiisoft (https://www.yiiframework.com/)
hiqdev/composer-config-plugin 适用场景与选型建议
hiqdev/composer-config-plugin 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 69.05k 次下载、GitHub Stars 达 87, 最近一次更新时间为 2016 年 05 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「plugin」 「composer」 「config」 「assembling」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hiqdev/composer-config-plugin 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hiqdev/composer-config-plugin 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hiqdev/composer-config-plugin 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CakePHP 4.x AdminLTE Theme.
A Zend Framework module to quickly and easily set PHP settings.
This composer plugin enables installation of GravityForms WordPress plugin and its addons.
Server environment detection based on config array-file
Simple ASCII output of array data
统计信息
- 总下载量: 69.05k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 90
- 点击次数: 13
- 依赖项目数: 30
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2016-05-18
