Usage

Implementation

Import the trait

In your Composer plugin, import the ConfigurablePlugin trait.

1use Composer\Plugin\PluginInterface;
2use cweagans\Composer\ConfigurablePlugin;
3
4class YourPlugin implements PluginInterface
5{
6    use ConfigurablePlugin;
7    
8    [...]
9}

Declare configuration values

Next, in the activate function of your plugin, declare your configuration schema.

 1use Composer\Composer;
 2use Composer\IO\IOInterface;
 3use Composer\Plugin\PluginInterface;
 4use cweagans\Composer\ConfigurablePlugin;
 5
 6class YourPlugin implements PluginInterface
 7{
 8    use ConfigurablePlugin;
 9
10    public function activate(Composer $composer, IOInterface $io)
11    {
12        $this->configuration = [
13            // You can have any number of blocks like this one. Each key must be unique.
14            'my-string-config' => [
15                // Required: this allows ConfigurablePlugin to determine values from environment variables.
16                // Allowed values: string, int, bool, list
17                'type' => 'string',
18                // Required: In the event that the users of your plugin don't bother to provide configuration, you need to provide reasonable defaults.
19                // The value must match the configuration value type. (further examples below)
20                'default' => 'somevalue',          
21            ],
22            'my-int-config' => [
23                'type' => 'int',
24                'default' => 123,          
25            ],
26            'my-bool-config' => [
27                'type' => 'bool',
28                'default' => false,          
29            ],
30            'my-list-config' => [
31                'type' => 'list',
32                'default' => ['somevalue'],          
33            ],
34        ];
35    }
36
37    [...]    
38}

Tell the library to load config values

 1use Composer\Composer;
 2use Composer\IO\IOInterface;
 3use Composer\Plugin\PluginInterface;
 4use cweagans\Composer\ConfigurablePlugin;
 5
 6class YourPlugin implements PluginInterface
 7{
 8    use ConfigurablePlugin;
 9
10    public function activate(Composer $composer, IOInterface $io)
11    {
12        $this->configuration = [...];
13        
14        // The second argument here can be an arbitrary string, but should be unique across
15        // all Composer plugins. This string is used for loading configuration from your
16        // composer.json and for constructing the name of the environment variables to
17        // check for configuration values.
18        $this->configure($composer->getPackage()->getExtra(), 'unique-key-for-your-plugin');
19    }
20
21    [...]    
22}

Provide configuration

Given the previous example configuration schema, you can provide configuration in composer.json like so:

 1{
 2    "extra": {
 3        "unique-key-for-your-plugin": {
 4            "my-string-config": "some user-provided value",
 5            "my-int-config": 12345,
 6            "my-bool-config": true,
 7            "my-list-config": ["a value", "another value"]
 8        }
 9    }
10}

You can also provide configuration through environment variables set before running Composer:

1export UNIQUE_KEY_FOR_YOUR_PLUGIN_MY_STRING_CONFIG="some user-provided value"
2export UNIQUE_KEY_FOR_YOUR_PLUGIN_MY_INT_CONFIG=1234567
3export UNIQUE_KEY_FOR_YOUR_PLUGIN_MY_BOOL_CONFIG=false
4export UNIQUE_KEY_FOR_YOUR_PLUGIN_MY_LIST_CONFIG=asdf,sdfg,dfgh
5composer install

Retrieve configuration values

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:

1$this->getConfig('my-string-config');
2$this->getConfig('my-int-config');
3$this->getConfig('my-bool-config');
4$this->getConfig('my-list-config');