API

Events

In the process of applying patches, Composer Patches emits several different events. The event dispatcher system is the one provided by Composer, so any documentation related to the Composer event dispatcher also applies to plugins extending Composer Patches.

Subscribing to an event.

This functionality comes directly from Composer. In your plugin class, implement \Composer\EventDispatcher\EventSubscriberInterface. Your getSubscribedEvents() method should return a list of events that you want to subscribe to and their respective handlers.

 1use Composer\EventDispatcher\EventSubscriberInterface;
 2use Composer\Plugin\PluginInterface;
 3use cweagans\Composer\Event\PatchEvents;
 4
 5class YourPlugin implements PluginInterface, EventSubscriberInterface
 6{
 7    /**
 8     * @inheritDoc
 9     */
10    public function getSubscribedEvents(): array
11    {
12        return array(
13            PatchEvents::PRE_PATCH_GUESS_DEPTH => ['yourHandlerHere'],
14            PatchEvents::PRE_PATCH_APPLY => ['aDifferentHandler', 10],
15        );
16    }
17    
18    [...]
19}

A full list of events can be found in \cweagans\Composer\Event\PatchEvents and \cweagans\Composer\Event\PluginEvents.

Writing the event handler

Once you’ve subscribed to an event, you need to write the handler. The handler function should be located in your main plugin class and be a public method. If the event is listed in \cweagans\Composer\Event\PatchEvents, your handler function will receive a \cweagans\Composer\Event\PatchEvent as the first (and only) argument. If the event is listed in \cweagans\Composer\Event\PluginEvents, your handler function will receive a \cweagans\Composer\Event\PluginEvent object as the first (and only) argument.