composer php插件开发,如何创建 Composer 插件

下面由composer教程栏目为大家讲解如何创建 Composer 插件,希望对需要的朋友有所帮助!

0abba62afc7f4aeee0ff2c4a2d158d31.png

设置和使用插件

概要

您可能希望使用自己的功能更改或扩展 Composer 的功能。例如,如果您的环境对 Composer 的行为提出了特殊要求,这些要求不适用于大多数用户,或者您希望以大多数用户不希望的方式使用 Composer 完成某些任务。

在这些情况下,您可以考虑创建一个插件来处理您的特定逻辑。

创建一个插件

插件是一个常规的 Composer 包,它将代码作为包的一部分提供,也可能依赖于其他包。

插件包

包文件与任何其他包文件相同,但具有以下要求:

● type 属性必须是 composer-plugin.

● extra 属性必须包含一个元素 class,用于定义插件的类名(包括命名空间)。如果包中包含多个插件,则可以是类名称数组。

● 您需要依赖名为 composer-plugin-api 的特殊包来定义插件兼容的插件 API 版本。

所需的 composer-plugin-api 版本遵循与普通包相同的 规则 。

当前的 composer 插件 API 版本是 1.1.0。

常规的插件 composer.json 文件的示例(省略了自动加载部分):{

"name": "my/plugin-package",

"type": "composer-plugin",

"require": {

"composer-plugin-api": "^1.1"

},

"extra": {

"class": "My\\Plugin"

}

}

插件类

每个插件都必须提供一个实现 Composer\Plugin\PluginInterface 的类。 加载插件后调用插件的 activate() 方法并接收 Composer\Composer 以及 Composer\IO\IOInterface 的实例。使用这两个对象可以读取所有配置,并且可以根据需要操纵所有内部对象和状态。

例如:<?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);

}

}

事件处理器

此外,插件可以实现 Composer\EventDispatcher\EventSubscriberInterface 以便在加载插件时让其事件处理程序自动注册到 EventDispatcher 。

要将方法注册到事件,请实现方法 getSubscribedEvents() 并让它返回一个数组。 数组键必须是 事件名称 并且对应的键值是要调用的此类中方法的名称。public static function getSubscribedEvents()

{

return array(

'post-autoload-dump' => 'methodToBeCalled',

// ^ event name ^ ^ method name ^

);

}

默认情况下,事件处理程序的优先级设置为 0。可以通过附加元组来更改优先级,其中第一个值是方法名称,如前所述,第二个值是表示优先级的整数。更高的整数代表更高的优先级。优先级 2 在优先级 1 之前调用,等等。public static function getSubscribedEvents()

{

return array(

// Will be called before events with priority 0

'post-autoload-dump' => array('methodToBeCalled', 1)

);

}

如果应该调用多个方法,则可以将每个事件附加一个元组数组。元组不需要包含优先级。如果省略,则默认为 0。public static function getSubscribedEvents()

{

return array(

'post-autoload-dump' => array(

array('methodToBeCalled' ), // Priority defaults to 0

array('someOtherMethodName', 1), // This fires first

)

);

}

完整示例:<?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);

}

}

}

插件功能

Composer 定义了一组可由插件实现的标准功能。通过为常见的插件需求提供明确的扩展点,减少了弄乱 Composer\Composer 内部状态的需求,使插件生态系统更加稳定。

Capable Plugins 类必须实现 Composer\Plugin\Capable 接口并在 getCapabilities() 方法中声明它们的功能。这个方法必须要返回一个数组,并且数组的 key 为 Composer Capability 类名称,value 为作为 Plugin 自己的实现类名称:<?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',

);

}

}

命令提供者

Composer\Plugin\Capability\CommandProvider 功能允许为 Composer 注册其它命令:<?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

{

protected function configure()

{

$this->setName('custom-plugin-command');

}

protected function execute(InputInterface $input, OutputInterface $output)

{

$output->writeln('Executing');

}

}

现在, custom-plugin-command 与 Composer 命令一起提供。

Composer 命令是基于 Symfony Console Component 控制台组件的。

使用插件

插件包在安装后将会自动加载,如果在当前项目的已安装软件包列表中找到,则会在编译器启动时加载。此外,在加载本地项目插件之前,将使用 composer global 命令在 COMPOSER_HOME 目录中安装所有插件包。

您可以将 --no-plugins 选项传递给 Composer 命令以禁用所有已安装的插件。如果有插件造成错误,您希望更新或卸载它,这可能特别有用。

更多composer使用技术文章,请访问composer命令使用图文教程栏目!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值