How to create a Composer plugin

After tinkering around with Symfony4 and Flex I was curious about how Composer plugins work. So I started to check the Composer’s plugin API and the official documentation. The documentation is great and you even have a full example of a plugin but when I wanted to test it… nothing. The plugin wasn’t loaded and nothing happened. Why? Because I was testing it wrong. 🙂 So this is more of a tip on how to start with a Composer plugin than a real step by step tutorial on how to create it.

You can find the demo code here if you want to follow it while reading (ignore the instructions from README for now).

First steps:

  1. Create the composer.json file in a new folder;
  2. Make sure to set the “type” to “composer-plugin”, add “autoload” and “extra” sections;
  3. Create the PHP class defined in the “extra” section.

The wrong way to test the plugin (don’t do it like this):

What I did next was to actually run composer in the same project where the plugin was created as described above. But it doesn’t work like that because this is a plugin and should be added to a project to extend that project’s capabilities.

The right way to test the plugin:

So instead of trying to execute the plugin as a full featured project, add it to one of your projects. Create a repository for the plugin on Github or another VCS system, then update the composer.json file from your project and include it there:

Run composer update and make sure the plugin is installed.

In the demo the event is set to “init” which means that the plugin is executed each time composer runs. If you run composer now you should see the plugin’s output in terminal:

Published by Cristian Radulescu

PHP // MySql // Linux

Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.