yiisoft/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.
config-plugin extra option in their
composer.json..env files to set $_ENV variables.constants files to set constants.params files.$_ENV should be used for constants and parameters, which
in turn should be used for configs.vendor/yiisoft/composer-config-plugin-output
directory along with information needed to rebuild configs on demand.require.Read more about the general idea behind this plugin in English or Russian.
composer require "yiisoft/composer-config-plugin"
Out of the box this plugin supports configs in PHP and JSON formats.
To enable additional formats require:
.env files..yml and .yaml.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"
}
},
? - 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.php will not found, but it's not okay if params.php will 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.php in any subfolders of src/Modules/ in config folder.
$ - 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.php and params-web.php will contain params.php and params-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');
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'),
],
];
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();
}
Config files are processed in proper order to achieve naturally expected behavior:
composer.json with.envs.constants.params.There are several ways to debug config building internals.
composer dump-autoload --verbose
Above can be shortened to composer du -v.
vendor/yiisoft/composer-config-plugin-output by default and can be configured
with config-plugin-output-dir extra option in composer.json.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?
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/)
How can I help you explore Laravel packages today?