2017-07-26 36 views
8

我開發了一個便攜式Web服務器,我也創建了一個便攜式控制檯來使用Composer。爲Composer創建活動/插件

我有一個問題。我需要創建一個插件來爲Composer添加其他行爲。

我需要用Composer下載任何軟件包時,它會編輯該軟件包的composer.json「腳本」,以便它可以在便攜式控制檯上使用。

當下載Laravel,例如:

原始composer.json:

{ 
    "name": "laravel/laravel", 
    ... 
    "scripts": { 
     "post-root-package-install": [ 
      "php -r \"file_exists('.env') || copy('.env.example', '.env');\"" 
     ], 
     ... 
    }, 
    ... 
} 

composer.json由插件編輯:

{ 
    "name": "laravel/laravel", 
    ... 
    "scripts": { 
     "post-root-package-install": [ 
      "F:/portable_php_path/php.exe -r \"file_exists('.env') || copy('.env.example', '.env');\"" 
     ], 
     ... 
    }, 
    ... 
} 
  • 注意,一個物理路徑具有已經爲php.exe生成,因爲在便攜版本中它可以在任何路徑中。

(我的問題是創造作曲家插件。我沒有問題,編輯用PHP composer.json。)

我閱讀教程創建的作曲家網站插件,但我感到困惑。 (https://getcomposer.org/doc/articles/plugins.md

如果還有其他方法可以做到這一點,那也很有趣。我接受其他建議和想法。

感謝任何人都可以提供幫助。

[對不起我的英文不好]

+1

你嘗試過什麼?你遇到了什麼具體問題並希望得到幫助? –

+1

您是否注意到只有根包的「腳本」部分被執行?所以應該在應用程序級別上定義哪個命令行/哪個腳本被執行(在你的情況下使用哪個php可執行文件)。例如如果Laravel是依賴項,則不應執行腳本部分。請參閱https://getcomposer.org/doc/articles/scripts.md#what-is-a-script- – P0rnflake

回答

3

我想你可以有一個插件實現PluginInterfaceEventSubscriberInterface

public static function getSubscribedEvents() 
{ 
    return [ 
     'post-package-install' => 'onPostPackageInstall' 
     // hook post-package-install using onPostPackageInstall method 
    ]; 
} 

public function onPostPackageInstall(\Composer\Installer\PackageEvent $event) 
{ 
    $vendorDir = $event->getComposer()->getConfig()->get('vendor-dir') . '/'; 

    /** @var InstallOperation $item */ 
    foreach ($event->getOperations() as $item) { 

     $packageInstalled = $item->getPackage()->getName(); 
     // do any thing with the package name like `laravel/laravel` 
     //You can now edit the composer.json file 

     echo $vendorDir . $packageInstalled . '/composer.json'; 

    } 

}