简体   繁体   中英

Can I programmatically trigger “composer update” using the Composer API?

I have a composer script that I run that calls a static method on a class. My class does some altering to the composer.json file and then rewrites it using the Composer\\Json\\JsonFile class from the Composer API:

composer.json

{
  ...
  "scripts": {
    "my-script": "MyScriptObject::doSomething"
  }
} 

My question is, how can I trigger a composer update from inside MyScriptObject class using the Composer API? I don't want to use the pre-update-cmd because I cannot pass arguments to it.

In looking at the API docs I can see that there is a Composer\\Installer class that I can use to do something like: Composer\\Installer::create(IOInterface $io, Composer $composer)->run() , but that performs an install and loads dependencies from the lock file. I'd like to perform an update so I can regenerate the lock file.

Does anyone know where this is at in the API?

Source: https://getcomposer.org/apidoc/master/index.html

The answer was right in front of me. All I had to do was setUpdate() before calling run:

Composer\\Installer::create($this->io, $this->composer)->setUpdate(true)->run();

Source: https://github.com/composer/composer/blob/1.0.0-alpha3/src/Composer/Installer.php#L489

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM