如何創建 Composer 插件

下面由composer教程欄目為大家講解如何創建 composer 插件,希望對需要的朋友有所幫助!

如何創建 Composer 插件

設置和使用插件

概要

您可能希望使用自己的功能更改或擴展 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":?"MyPlugin" ????} }

插件類

每個插件都必須提供一個實現? ComposerPluginPluginInterface 的類。 加載插件后調用插件的 activate() 方法并接收 ComposerComposer 以及 ComposerIOIOInterface 的實例。使用這兩個對象可以讀取所有配置,并且可以根據需要操縱所有內部對象和狀態。

例如:

<?php namespace phpDocumentorComposer; use ComposerComposer; use ComposerIOIOInterface; use ComposerPluginPluginInterface; class TemplateInstallerPlugin implements PluginInterface {     public function activate(Composer $composer, IOInterface $io)     {         $installer = new TemplateInstaller($io, $composer);         $composer->getInstallationManager()-&gt;addInstaller($installer); ????} }

事件處理器

此外,插件可以實現 ComposerEventDispatcherEventSubscriberInterface 以便在加載插件時讓其事件處理程序自動注冊到 EventDispatcher 。

要將方法注冊到事件,請實現方法 getSubscribedEvents() 并讓它返回一個數組。 數組鍵必須是 事件名稱 并且對應的鍵值是要調用的此類中方法的名稱。

public?static?function?getSubscribedEvents() { ????return?array( ????????'post-autoload-dump'?=&gt;?'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'?=&gt;?array('methodToBeCalled',?1) ????); }

如果應該調用多個方法,則可以將每個事件附加一個元組數組。元組不需要包含優先級。如果省略,則默認為 0。

public?static?function?getSubscribedEvents() { ????return?array( ????????'post-autoload-dump'?=&gt;?array( ????????????array('methodToBeCalled'??????),?//?Priority?defaults?to?0 ????????????array('someOtherMethodName',?1),?//?This?fires?first ????????) ????); }

完整示例:

<?php namespace NadermanComposerAWS; use ComposerComposer; use ComposerEventDispatcherEventSubscriberInterface; use ComposerIOIOInterface; use ComposerPluginPluginInterface; use ComposerPluginPluginEvents; use ComposerPluginPreFileDownloadEvent; class AwsPlugin implements PluginInterface, EventSubscriberInterface {     protected $composer;     protected $io;     public function activate(Composer $composer, IOInterface $io)     {         $this->composer?=?$composer; ????????$this-&gt;io?=?$io; ????} ????public?static?function?getSubscribedEvents() ????{ ????????return?array( ????????????PluginEvents::PRE_FILE_DOWNLOAD?=&gt;?array( ????????????????array('onPreFileDownload',?0) ????????????), ????????); ????} ????public?function?onPreFileDownload(PreFileDownloadEvent?$event) ????{ ????????$protocol?=?parse_url($event-&gt;getProcessedUrl(),?PHP_URL_SCHEME); ????????if?($protocol?===?'s3')?{ ????????????$awsClient?=?new?AwsClient($this-&gt;io,?$this-&gt;composer-&gt;getConfig()); ????????????$s3RemoteFilesystem?=?new?S3RemoteFilesystem($this-&gt;io,?$event-&gt;getRemoteFilesystem()-&gt;getOptions(),?$awsClient); ????????????$event-&gt;setRemoteFilesystem($s3RemoteFilesystem); ????????} ????} }

插件功能

Composer 定義了一組可由插件實現的標準功能。通過為常見的插件需求提供明確的擴展點,減少了弄亂? ComposerComposer 內部狀態的需求,使插件生態系統更加穩定。

Capable Plugins 類必須實現 ComposerPluginCapable 接口并在 getCapabilities() 方法中聲明它們的功能。這個方法必須要返回一個數組,并且數組的 key 為 Composer Capability 類名稱,value 為作為 Plugin 自己的實現類名稱:

<?php namespace MyComposer; use ComposerComposer; use ComposerIOIOInterface; use ComposerPluginPluginInterface; use ComposerPluginCapable; class Plugin implements PluginInterface, Capable {     public function activate(Composer $composer, IOInterface $io)     {     }     public function getCapabilities()     {         return array(             &#39;ComposerPluginCapabilityCommandProvider&#39; =>?'MyComposerCommandProvider', ????????); ????} }

命令提供者

ComposerPluginCapabilityCommandProvider 功能允許為 Composer 注冊其它命令:

<?php namespace MyComposer; use ComposerPluginCapabilityCommandProvider as CommandProviderCapability; use symfonyComponentconsoleInputInputInterface; use SymfonyComponentConsoleOutputOutputInterface; use ComposerCommandBaseCommand; 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-&gt;writeln('Executing'); ????} }

現在, custom-plugin-command 與 Composer 命令一起提供。

Composer 命令是基于 Symfony Console Component 控制臺組件的。

使用插件

插件包在安裝后將會自動加載,如果在當前項目的已安裝軟件包列表中找到,則會在編譯器啟動時加載。此外,在加載本地項目插件之前,將使用 composer global 命令在 COMPOSER_HOME 目錄中安裝所有插件包。

您可以將 –no-plugins 選項傳遞給 Composer 命令以禁用所有已安裝的插件。如果有插件造成錯誤,您希望更新或卸載它,這可能特別有用。

更多composer使用技術文章,請訪問composer教程欄目!

? 版權聲明
THE END
喜歡就支持一下吧
點贊9 分享