cweagans/composer-configurable-plugin
Composer plugin that makes package configuration flexible by reading and merging settings from composer.json (extra) and other sources. Useful for teams needing configurable behavior across environments without hardcoding values in the plugin itself.
In your Composer plugin, import the ConfigurablePlugin trait.
{{< highlight php "hl_lines=6" >}} use Composer\Plugin\PluginInterface; use cweagans\Composer\ConfigurablePlugin;
class YourPlugin implements PluginInterface { use ConfigurablePlugin;
[...]
} {{< /highlight >}}
Next, in the activate function of your plugin, declare your configuration schema.
{{< highlight php "hl_lines=12-34" >}} use Composer\Composer; use Composer\IO\IOInterface; use Composer\Plugin\PluginInterface; use cweagans\Composer\ConfigurablePlugin;
class YourPlugin implements PluginInterface { use ConfigurablePlugin;
public function activate(Composer $composer, IOInterface $io)
{
$this->configuration = [
// You can have any number of blocks like this one. Each key must be unique.
'my-string-config' => [
// Required: this allows ConfigurablePlugin to determine values from environment variables.
// Allowed values: string, int, bool, list
'type' => 'string',
// Required: In the event that the users of your plugin don't bother to provide configuration, you need to provide reasonable defaults.
// The value must match the configuration value type. (further examples below)
'default' => 'somevalue',
],
'my-int-config' => [
'type' => 'int',
'default' => 123,
],
'my-bool-config' => [
'type' => 'bool',
'default' => false,
],
'my-list-config' => [
'type' => 'list',
'default' => ['somevalue'],
],
];
}
[...]
} {{< /highlight >}}
{{< highlight php "hl_lines=12-34" >}} use Composer\Composer; use Composer\IO\IOInterface; use Composer\Plugin\PluginInterface; use cweagans\Composer\ConfigurablePlugin;
class YourPlugin implements PluginInterface { use ConfigurablePlugin;
public function activate(Composer $composer, IOInterface $io)
{
$this->configuration = [...];
// The second argument here can be an arbitrary string, but should be unique across
// all Composer plugins. This string is used for loading configuration from your
// composer.json and for constructing the name of the environment variables to
// check for configuration values.
$this->configure($composer->getPackage()->getExtra(), 'unique-key-for-your-plugin');
}
[...]
} {{< /highlight >}}
Given the previous example configuration schema, you can provide configuration in composer.json like so:
{
"extra": {
"unique-key-for-your-plugin": {
"my-string-config": "some user-provided value",
"my-int-config": 12345,
"my-bool-config": true,
"my-list-config": ["a value", "another value"]
}
}
}
You can also provide configuration through environment variables set before running Composer:
export UNIQUE_KEY_FOR_YOUR_PLUGIN_MY_STRING_CONFIG="some user-provided value"
export UNIQUE_KEY_FOR_YOUR_PLUGIN_MY_INT_CONFIG=1234567
export UNIQUE_KEY_FOR_YOUR_PLUGIN_MY_BOOL_CONFIG=false
export UNIQUE_KEY_FOR_YOUR_PLUGIN_MY_LIST_CONFIG=asdf,sdfg,dfgh
composer install
Once all of your configuration has been wired up, the last step is to use your configuration value somewhere. You can do so anywhere in your Composer plugin by calling the getConfig method:
$this->getConfig('my-string-config');
$this->getConfig('my-int-config');
$this->getConfig('my-bool-config');
$this->getConfig('my-list-config');
How can I help you explore Laravel packages today?