Create Event / Plugin for Composer

I developed a portable WebServer, and I am also creating a portable console to use Composer.

I have a problem. I need to create a plugin to add additional behavior to Composer.

I need that when downloading any package from Composer, it should edit the β€œscripts” composer.json of this package so that it works on the portable console.

When loading Laravel, for example:

Original composer.json:

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

composer.json edited by the plugin:

 { "name": "laravel/laravel", ... "scripts": { "post-root-package-install": [ "F:/portable_php_path/php.exe -r \"file_exists('.env') || copy('.env.example', '.env');\"" ], ... }, ... } 
  • Please note that a physical path was created for php.exe, since in the portable version it can be in any way.

(My question is to create a composer plugin. I have no problem editing composer.json with PHP.)

I read the tutorial for creating a plugin on the composer's website, but I was confused. ( https://getcomposer.org/doc/articles/plugins.md )

If there is another way to do this, it is also interesting. I accept other suggestions and ideas.

Thanks to everyone who can help.

[Sorry my bad english]

+8
php composer-php
source share
1 answer

I think you can use the PluginInterface plugin and EventSubscriberInterface

 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'; } } 
+3
source share

All Articles