May 25, 2021 Composer
You may need to change and extend Composer to add custom features. F or example, if your environment has special requirements for Composer's behavior, this does not apply to ordinary users. Or, you want to do something with Composer without affecting the average user.
In these cases, you may want to consider creating a plug-in to handle specific logic.
A plug-in is an ordinary Composer resource pack, where the main code of the plug-in is located and can rely on more other resource packs.
The files in the plug-in package are the same as the normal resource pack, but must meet the following requirements:
composer-plugin
class
the name of the plug-in class, including the namespace.
If you have more than one plug-in in a package, you can use arrays to define class names.
In addition, you must require a special resource pack
composer-plugin-api
defines the composer plugin API version that is compatible with your plug-in.
The current version of the composer plugin API is 1.0.0.
Instance:
{
"name": "my/plugin-package",
"type": "composer-plugin",
"require": {
"composer-plugin-api": "1.0.0"
}
}
Each plug-in must provide a class
Composer\Plugin\PluginInterface
interface. T
he
activate()
class is called after the plug-in is loaded and
Composer\Composer
of two classes: Composer/Composer and
Composer\IO\IOInterface
Both objects allow you to read all configurations and manipulate all internal objects and states.
Instance:
<?php
namespace phpDocumentor\Composer;
use Composer\Composer;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
class TemplateInstallerPlugin implements PluginInterface
{
public function activate(Composer $composer, IOInterface $io)
{
$installer = new TemplateInstaller($io, $composer);
$composer->getInstallationManager()->addInstaller($installer);
}
}
In addition, the plug-in implements the
Composer\EventDispatcher\EventSubscriberInterface
in order to automatically register the event handler to
EventDispatcher
The plug-in available events are as follows:
PRE_FILE_DOWNLOAD is
triggered before the file is downloaded, which allows you to manipulate
RemoteFilesystem
A plug-in can also subscribe to script events.
Instance:
<?php
namespace Naderman\Composer\AWS;
use Composer\Composer;
use Composer\EventDispatcher\EventSubscriberInterface;
use Composer\IO\IOInterface;
use Composer\Plugin\PluginInterface;
use Composer\Plugin\PluginEvents;
use Composer\Plugin\PreFileDownloadEvent;
class AwsPlugin implements PluginInterface, EventSubscriberInterface
{
protected $composer;
protected $io;
public function activate(Composer $composer, IOInterface $io)
{
$this->composer = $composer;
$this->io = $io;
}
public static function getSubscribedEvents()
{
return array(
PluginEvents::PRE_FILE_DOWNLOAD => array(
array('onPreFileDownload', 0)
),
);
}
public function onPreFileDownload(PreFileDownloadEvent $event)
{
$protocol = parse_url($event->getProcessedUrl(), PHP_URL_SCHEME);
if ($protocol === 's3') {
$awsClient = new AwsClient($this->io, $this->composer->getConfig());
$s3RemoteFilesystem = new S3RemoteFilesystem($this->io, $event->getRemoteFilesystem()->getOptions(), $awsClient);
$event->setRemoteFilesystem($s3RemoteFilesystem);
}
}
}
Plug-in packages are installed and loaded first, and when Composer starts, they will be loaded automatically if found in the current list of projects for which the resource pack is installed.
In addition, all plug-in
COMPOSER_HOME
directory so that they can be loaded before the Composer global command, and the plug-in is already loaded before the local project is operated.
You can
--no-plugins
with composer's commands via the --no-plugins option.
This may be especially useful if any plug-in causes an error while you want to upgrade or uninstall it.