Composer plugin for config assembling
This Composer plugin provides assembling of configurations distributed with composer packages. This allows to put configuration needed to use package right inside of the package thus implementing plugin system: package becomes a plugin holding both the code and it's configuration.
How it works?
config-plugin extra option in their
composer.jsondotenv files to set $_ENV variablesdefines files to set constantsparams files$_ENV should be used for constants and parameters, which
in turn should be used for configsvendor/hiqdev/composer-config-plugin-output
directory along with information needed to rebuild configs on demandrequireRead more about the general idea behind this plugin in english or russian.
Add to require section of your composer.json:
"hiqdev/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 .yamlList your config files in composer.json like the following:
"extra": {
    "config-plugin": {
        "params": [
            "src/config/params.php",
            "?src/config/params-local.php"
        ],
        "common": "src/config/common.php",
        "web": [
            "$common",
            "src/config/web.php"
        ],
        "other": "src/config/other.php"
    }
},
? marks optional files, absence of other files will cause exception.
$common is inclusion - common config will be merged into web.
Define your configs like this:
return [
    'components' => [
        'db' => [
            'class' => \my\Db::class,
            'name' => $params['db.name'],
            'password' => $params['db.password'],
        ],
    ],
];
To load assembled configs in your application use require:
$config = require hiqdev\composer\config\Builder::path('web');
Plugin hangs on composer POST_AUTOLOAD_DUMP event.
I.e. composer runs this plugin on install, update and dump-autoload
commands.
As the result configs are just ready to be used after packages installation
or updating.
After you make changes to any of configs you may want to reassemble configs manually - run:
composer dump-autoload
Can be shortened to composer du.
Also, you can force config rebuild from your application like this:
// Don't do it in production, assembling takes it's time
if (ENVIRONMENT === 'dev') {
    hiqdev\composer\config\Builder::rebuild();
}
Config files are processed in proper order to achieve naturally expected behavior:
composer.jsondotenvdefinesparamsThere are several ways to debug config building internals.
composer dump-autoload --verbose
Can be shortened to composer du -v.
see the list of configs and files that plugin has detected and uses
to build configs: vendor/hiqdev/composer-config-plugin/output/__files.php.
see the assembled configs in
vendor/hiqdev/composer-config-plugin-output directory.
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?
Anonymous functions must be used in multiline form only:
return [
    'works' => function () {
        return 'value';
    },
    // this will not work
    'noway' => function () { return 'value'; },
];
This project is released under the terms of the BSD-3-Clause license. Read more here.
Copyright © 2016-2017, HiQDev (http://hiqdev.com/)