2013-09-05 18:08:17 +00:00
|
|
|
<!--
|
|
|
|
tagline: Modify and extend Composer's functionality
|
|
|
|
-->
|
|
|
|
|
|
|
|
# Setting up and using plugins
|
|
|
|
|
|
|
|
## Synopsis
|
|
|
|
|
|
|
|
You may wish to alter or expand Composer's functionality with your own. For
|
|
|
|
example if your environment poses special requirements on the behaviour of
|
|
|
|
Composer which do not apply to the majority of its users or if you wish to
|
2020-10-24 19:20:11 +00:00
|
|
|
accomplish something with Composer in a way that is not desired by most users.
|
2013-09-05 18:08:17 +00:00
|
|
|
|
|
|
|
In these cases you could consider creating a plugin to handle your
|
|
|
|
specific logic.
|
|
|
|
|
|
|
|
## Creating a Plugin
|
|
|
|
|
2015-06-02 18:09:57 +00:00
|
|
|
A plugin is a regular Composer package which ships its code as part of the
|
2013-09-05 18:08:17 +00:00
|
|
|
package and may also depend on further packages.
|
|
|
|
|
|
|
|
### Plugin Package
|
|
|
|
|
|
|
|
The package file is the same as any other package file but with the following
|
|
|
|
requirements:
|
|
|
|
|
2015-06-02 18:09:57 +00:00
|
|
|
1. The [type][1] attribute must be `composer-plugin`.
|
|
|
|
2. The [extra][2] attribute must contain an element `class` defining the
|
2013-09-05 18:08:17 +00:00
|
|
|
class name of the plugin (including namespace). If a package contains
|
2021-12-19 14:15:21 +00:00
|
|
|
multiple plugins, this can be an array of class names.
|
2015-06-02 18:09:57 +00:00
|
|
|
3. You must require the special package called `composer-plugin-api`
|
|
|
|
to define which Plugin API versions your plugin is compatible with.
|
2020-10-24 19:20:11 +00:00
|
|
|
Requiring this package doesn't actually include any extra dependencies,
|
|
|
|
it only specifies which version of the plugin API to use.
|
|
|
|
|
|
|
|
> **Note:** When developing a plugin, although not required, it's useful to add
|
2021-12-19 14:15:21 +00:00
|
|
|
> a require-dev dependency on `composer/composer` to have IDE autocompletion on Composer classes.
|
2013-09-05 18:08:17 +00:00
|
|
|
|
2015-06-02 18:09:57 +00:00
|
|
|
The required version of the `composer-plugin-api` follows the same [rules][7]
|
2021-12-19 14:15:21 +00:00
|
|
|
as a normal package's rules.
|
2013-09-05 18:08:17 +00:00
|
|
|
|
2022-01-05 13:50:30 +00:00
|
|
|
The current Composer plugin API version is `2.3.0`.
|
2015-06-02 18:09:57 +00:00
|
|
|
|
2016-01-26 12:17:10 +00:00
|
|
|
An example of a valid plugin `composer.json` file (with the autoloading
|
2020-10-25 11:50:13 +00:00
|
|
|
part omitted and an optional require-dev dependency on `composer/composer` for IDE auto completion):
|
2013-09-05 18:08:17 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```json
|
|
|
|
{
|
|
|
|
"name": "my/plugin-package",
|
|
|
|
"type": "composer-plugin",
|
|
|
|
"require": {
|
2020-10-26 11:16:11 +00:00
|
|
|
"composer-plugin-api": "^2.0"
|
2015-06-02 18:09:57 +00:00
|
|
|
},
|
2020-10-24 19:20:11 +00:00
|
|
|
"require-dev": {
|
|
|
|
"composer/composer": "^2.0"
|
|
|
|
},
|
2015-06-02 18:09:57 +00:00
|
|
|
"extra": {
|
|
|
|
"class": "My\\Plugin"
|
2013-09-05 18:08:17 +00:00
|
|
|
}
|
2014-05-19 10:17:07 +00:00
|
|
|
}
|
|
|
|
```
|
2013-09-05 18:08:17 +00:00
|
|
|
|
|
|
|
### Plugin Class
|
|
|
|
|
|
|
|
Every plugin has to supply a class which implements the
|
|
|
|
[`Composer\Plugin\PluginInterface`][3]. The `activate()` method of the plugin
|
|
|
|
is called after the plugin is loaded and receives an instance of
|
|
|
|
[`Composer\Composer`][4] as well as an instance of
|
|
|
|
[`Composer\IO\IOInterface`][5]. Using these two objects all configuration can
|
|
|
|
be read and all internal objects and state can be manipulated as desired.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```php
|
|
|
|
<?php
|
2013-09-05 18:08:17 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
namespace phpDocumentor\Composer;
|
2013-09-05 18:08:17 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
use Composer\Composer;
|
|
|
|
use Composer\IO\IOInterface;
|
|
|
|
use Composer\Plugin\PluginInterface;
|
|
|
|
|
|
|
|
class TemplateInstallerPlugin implements PluginInterface
|
|
|
|
{
|
|
|
|
public function activate(Composer $composer, IOInterface $io)
|
2013-09-05 18:08:17 +00:00
|
|
|
{
|
2014-05-19 10:17:07 +00:00
|
|
|
$installer = new TemplateInstaller($io, $composer);
|
|
|
|
$composer->getInstallationManager()->addInstaller($installer);
|
2013-09-05 18:08:17 +00:00
|
|
|
}
|
2014-05-19 10:17:07 +00:00
|
|
|
}
|
|
|
|
```
|
2013-09-05 18:08:17 +00:00
|
|
|
|
|
|
|
## Event Handler
|
|
|
|
|
|
|
|
Furthermore plugins may implement the
|
|
|
|
[`Composer\EventDispatcher\EventSubscriberInterface`][6] in order to have its
|
|
|
|
event handlers automatically registered with the `EventDispatcher` when the
|
|
|
|
plugin is loaded.
|
|
|
|
|
2016-01-26 12:17:10 +00:00
|
|
|
To register a method to an event, implement the method `getSubscribedEvents()`
|
|
|
|
and have it return an array. The array key must be the
|
|
|
|
[event name](https://getcomposer.org/doc/articles/scripts.md#event-names)
|
|
|
|
and the value is the name of the method in this class to be called.
|
2013-11-27 11:55:12 +00:00
|
|
|
|
2020-10-13 10:05:24 +00:00
|
|
|
> **Note:** If you don't know which event to listen to, you can run a Composer
|
|
|
|
> command with the COMPOSER_DEBUG_EVENTS=1 environment variable set, which might
|
|
|
|
> help you identify what event you are looking for.
|
|
|
|
|
2016-01-26 11:38:12 +00:00
|
|
|
```php
|
|
|
|
public static function getSubscribedEvents()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'post-autoload-dump' => 'methodToBeCalled',
|
2016-01-26 12:17:10 +00:00
|
|
|
// ^ event name ^ ^ method name ^
|
2016-01-26 11:38:12 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-05-10 12:27:20 +00:00
|
|
|
By default, the priority of an event handler is set to 0. The priority can be
|
2016-01-26 12:17:10 +00:00
|
|
|
changed by attaching a tuple where the first value is the method name, as
|
|
|
|
before, and the second value is an integer representing the priority.
|
2016-05-10 12:27:20 +00:00
|
|
|
Higher integers represent higher priorities. Priority 2 is called before
|
2016-01-26 12:17:10 +00:00
|
|
|
priority 1, etc.
|
2016-01-26 11:38:12 +00:00
|
|
|
|
|
|
|
```php
|
|
|
|
public static function getSubscribedEvents()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
// Will be called before events with priority 0
|
2016-01-26 12:17:10 +00:00
|
|
|
'post-autoload-dump' => array('methodToBeCalled', 1)
|
2016-01-26 11:38:12 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-01-26 12:17:10 +00:00
|
|
|
If multiple methods should be called, then an array of tuples can be attached
|
|
|
|
to each event. The tuples do not need to include the priority. If it is
|
|
|
|
omitted, it will default to 0.
|
2016-01-26 11:38:12 +00:00
|
|
|
|
|
|
|
```php
|
|
|
|
public static function getSubscribedEvents()
|
|
|
|
{
|
2016-01-26 11:47:36 +00:00
|
|
|
return array(
|
2016-01-26 11:38:12 +00:00
|
|
|
'post-autoload-dump' => array(
|
|
|
|
array('methodToBeCalled' ), // Priority defaults to 0
|
|
|
|
array('someOtherMethodName', 1), // This fires first
|
|
|
|
)
|
2016-01-26 11:47:36 +00:00
|
|
|
);
|
2016-01-26 11:38:12 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Here's a complete example:
|
2013-09-05 18:08:17 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
```php
|
|
|
|
<?php
|
2013-09-05 18:08:17 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
namespace Naderman\Composer\AWS;
|
2013-09-05 18:08:17 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
use Composer\Composer;
|
|
|
|
use Composer\EventDispatcher\EventSubscriberInterface;
|
|
|
|
use Composer\IO\IOInterface;
|
|
|
|
use Composer\Plugin\PluginInterface;
|
|
|
|
use Composer\Plugin\PluginEvents;
|
|
|
|
use Composer\Plugin\PreFileDownloadEvent;
|
2013-09-05 18:08:17 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
class AwsPlugin implements PluginInterface, EventSubscriberInterface
|
|
|
|
{
|
|
|
|
protected $composer;
|
|
|
|
protected $io;
|
2013-09-05 18:08:17 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
public function activate(Composer $composer, IOInterface $io)
|
|
|
|
{
|
|
|
|
$this->composer = $composer;
|
|
|
|
$this->io = $io;
|
|
|
|
}
|
|
|
|
|
2020-10-13 09:05:37 +00:00
|
|
|
public function deactivate(Composer $composer, IOInterface $io)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function uninstall(Composer $composer, IOInterface $io)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
public static function getSubscribedEvents()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
PluginEvents::PRE_FILE_DOWNLOAD => array(
|
|
|
|
array('onPreFileDownload', 0)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2013-09-05 18:08:17 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
public function onPreFileDownload(PreFileDownloadEvent $event)
|
|
|
|
{
|
|
|
|
$protocol = parse_url($event->getProcessedUrl(), PHP_URL_SCHEME);
|
2013-09-05 18:08:17 +00:00
|
|
|
|
2014-05-19 10:17:07 +00:00
|
|
|
if ($protocol === 's3') {
|
2020-10-13 09:05:37 +00:00
|
|
|
// ...
|
2013-09-05 18:08:17 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-19 10:17:07 +00:00
|
|
|
}
|
|
|
|
```
|
2013-09-05 18:08:17 +00:00
|
|
|
|
2016-05-02 07:19:40 +00:00
|
|
|
## Plugin capabilities
|
|
|
|
|
2016-05-03 06:22:54 +00:00
|
|
|
Composer defines a standard set of capabilities which may be implemented by plugins.
|
|
|
|
Their goal is to make the plugin ecosystem more stable as it reduces the need to mess
|
|
|
|
with [`Composer\Composer`][4]'s internal state, by providing explicit extension points
|
|
|
|
for common plugin requirements.
|
|
|
|
|
|
|
|
Capable Plugins classes must implement the [`Composer\Plugin\Capable`][8] interface
|
2017-12-30 20:06:14 +00:00
|
|
|
and declare their capabilities in the `getCapabilities()` method.
|
|
|
|
This method must return an array, with the _key_ as a Composer Capability class name,
|
2016-05-03 06:22:54 +00:00
|
|
|
and the _value_ as the Plugin's own implementation class name of said Capability:
|
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace My\Composer;
|
|
|
|
|
|
|
|
use Composer\Composer;
|
|
|
|
use Composer\IO\IOInterface;
|
|
|
|
use Composer\Plugin\PluginInterface;
|
|
|
|
use Composer\Plugin\Capable;
|
|
|
|
|
|
|
|
class Plugin implements PluginInterface, Capable
|
|
|
|
{
|
|
|
|
public function activate(Composer $composer, IOInterface $io)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCapabilities()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
'Composer\Plugin\Capability\CommandProvider' => 'My\Composer\CommandProvider',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
2016-05-02 07:19:40 +00:00
|
|
|
|
|
|
|
### Command provider
|
|
|
|
|
|
|
|
The [`Composer\Plugin\Capability\CommandProvider`][9] capability allows to register
|
2020-10-10 10:01:58 +00:00
|
|
|
additional commands for Composer:
|
2016-05-02 07:19:40 +00:00
|
|
|
|
|
|
|
```php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace My\Composer;
|
|
|
|
|
|
|
|
use Composer\Plugin\Capability\CommandProvider as CommandProviderCapability;
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use Composer\Command\BaseCommand;
|
|
|
|
|
|
|
|
class CommandProvider implements CommandProviderCapability
|
|
|
|
{
|
|
|
|
public function getCommands()
|
|
|
|
{
|
|
|
|
return array(new Command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Command extends BaseCommand
|
|
|
|
{
|
2022-02-16 12:24:57 +00:00
|
|
|
protected function configure(): void
|
2016-05-02 07:19:40 +00:00
|
|
|
{
|
|
|
|
$this->setName('custom-plugin-command');
|
|
|
|
}
|
|
|
|
|
2022-02-16 12:24:57 +00:00
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
2016-05-02 07:19:40 +00:00
|
|
|
{
|
|
|
|
$output->writeln('Executing');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2016-05-03 06:22:54 +00:00
|
|
|
Now the `custom-plugin-command` is available alongside Composer commands.
|
2016-05-02 07:19:40 +00:00
|
|
|
|
2016-05-03 06:22:54 +00:00
|
|
|
> _Composer commands are based on the [Symfony Console Component][10]._
|
2016-05-02 07:19:40 +00:00
|
|
|
|
2019-04-30 11:46:17 +00:00
|
|
|
## Running plugins manually
|
|
|
|
|
2020-10-13 10:05:24 +00:00
|
|
|
Plugins for an event can be run manually by the `run-script` command. This works the same way as
|
2019-04-30 11:46:17 +00:00
|
|
|
[running scripts manually](scripts.md#running-scripts-manually).
|
|
|
|
|
2013-09-05 18:08:17 +00:00
|
|
|
## Using Plugins
|
|
|
|
|
|
|
|
Plugin packages are automatically loaded as soon as they are installed and will
|
2020-10-24 19:20:11 +00:00
|
|
|
be loaded when Composer starts up if they are found in the current project's
|
2013-09-05 18:08:17 +00:00
|
|
|
list of installed packages. Additionally all plugin packages installed in the
|
2020-10-24 19:20:11 +00:00
|
|
|
`COMPOSER_HOME` directory using the Composer global command are loaded before
|
2013-09-05 18:08:17 +00:00
|
|
|
local project plugins are loaded.
|
|
|
|
|
2020-10-24 19:20:11 +00:00
|
|
|
> You may pass the `--no-plugins` option to Composer commands to disable all
|
2015-03-04 23:50:10 +00:00
|
|
|
> installed plugins. This may be particularly helpful if any of the plugins
|
2013-09-05 18:08:17 +00:00
|
|
|
> causes errors and you wish to update or uninstall it.
|
|
|
|
|
2020-10-13 09:05:37 +00:00
|
|
|
## Plugin Helpers
|
|
|
|
|
|
|
|
As of Composer 2, due to the fact that DownloaderInterface can sometimes return Promises
|
|
|
|
and have been split up in more steps than they used to, we provide a [SyncHelper][11]
|
|
|
|
to make downloading and installing packages easier.
|
|
|
|
|
2020-10-25 21:06:49 +00:00
|
|
|
## Plugin Extra Attributes
|
|
|
|
|
|
|
|
A few special plugin capabilities can be unlocked using extra attributes in the plugin's composer.json.
|
|
|
|
|
|
|
|
### class
|
|
|
|
|
|
|
|
[See above](#plugin-package) for an explanation of the class attribute and how it works.
|
|
|
|
|
|
|
|
### plugin-modifies-downloads
|
|
|
|
|
|
|
|
Some special plugins need to update package download URLs before they get downloaded.
|
|
|
|
|
|
|
|
As of Composer 2.0, all packages are downloaded before they get installed. This means
|
|
|
|
on the first installation, your plugin is not yet installed when the download occurs,
|
|
|
|
and it does not get a chance to update the URLs on time.
|
|
|
|
|
|
|
|
Specifying `{"extra": {"plugin-modifies-downloads": true}}` in your composer.json will
|
|
|
|
hint to Composer that the plugin should be installed on its own before proceeding with
|
2020-10-25 21:32:44 +00:00
|
|
|
the rest of the package downloads. This slightly slows down the overall installation
|
|
|
|
process however, so do not use it in plugins which do not absolutely require it.
|
2020-10-25 21:06:49 +00:00
|
|
|
|
2013-09-05 18:08:17 +00:00
|
|
|
[1]: ../04-schema.md#type
|
|
|
|
[2]: ../04-schema.md#extra
|
2021-10-14 14:21:47 +00:00
|
|
|
[3]: https://github.com/composer/composer/blob/main/src/Composer/Plugin/PluginInterface.php
|
|
|
|
[4]: https://github.com/composer/composer/blob/main/src/Composer/Composer.php
|
|
|
|
[5]: https://github.com/composer/composer/blob/main/src/Composer/IO/IOInterface.php
|
|
|
|
[6]: https://github.com/composer/composer/blob/main/src/Composer/EventDispatcher/EventSubscriberInterface.php
|
2015-06-02 18:09:57 +00:00
|
|
|
[7]: ../01-basic-usage.md#package-versions
|
2021-10-14 14:21:47 +00:00
|
|
|
[8]: https://github.com/composer/composer/blob/main/src/Composer/Plugin/Capable.php
|
|
|
|
[9]: https://github.com/composer/composer/blob/main/src/Composer/Plugin/Capability/CommandProvider.php
|
2017-12-29 10:33:19 +00:00
|
|
|
[10]: https://symfony.com/doc/current/components/console.html
|
2021-10-14 14:21:47 +00:00
|
|
|
[11]: https://github.com/composer/composer/blob/main/src/Composer/Util/SyncHelper.php
|