简体   繁体   中英

How to use composer/composer PHP classes to update individual packages

I want to use the composer/composer PHP classes to update individual plugin packages. I do not want to use command-line solutions like exec("php composer.phar update");

I am unable to get it to work. I have tried several different options, much alike the following code. It just returns a blank screen.

    use Composer\Console\Application;
    use Symfony\Component\Console\Input\ArrayInput;
    use Symfony\Component\Console\Output\BufferedOutput;

    $input = new ArrayInput(array('command' => 'require vendor/packkage dev-master'));
    $output = new BufferedOutput();
    $application = new Application();

    $application->run($input, $output);
    dd($output->fetch());

Things i would like to achieve:

  • Download/Update individual packages
  • Get result output to verify success
  • Dump autoload
  • Remove/require packages

A bit of context details:

I am creating a plugin updater for my PHP application (in admin panel). Every plugin is a composer package and resides on my own Satis repository. The plugins get installed into a custom dir using my composer plugin. I can read composer.lock locally and packages.json on the satis server to figure out what packages require updates.

update I've managed to at least get it to work. The no-output issue was due to $application->setAutoExit that needed to be false before running. Next issue that i had was that the required package would download itself into the same directory as the class where i called it from. Solved that by using putenv and chdir. Result:

root/comp.php

putenv('COMPOSER_HOME=' . __DIR__ . '/vendor/bin/composer');
chdir(__DIR__);

root/workbench/sumvend/sumpack/src/PackageManager.php

include(base_path() . '/comp.php');
$input = new ArrayInput(array('command' => 'require', 'packages' => ['vend/pak dev-master']));
$output = new BufferedOutput();
$application = new Application();

$application->setAutoExit(false);
$application->run($input, $output); //, $output);
dd($output->fetch());

This works, but it's far from ideal.

The full solution to this would be pretty long winded, but I will try to get you on the right track.

php composer.phar require composer/composer dev-master

You can load the source of composer into your project vendors. You might have already done this.

The code you are looking for is at: Composer\\Command\\RequireCommand .

$install = Installer::create($io, $composer);

$install
    ->setVerbose($input->getOption('verbose'))
    ->setPreferSource($input->getOption('prefer-source'))
    ->setPreferDist($input->getOption('prefer-dist'))
    ->setDevMode($updateDevMode)
    ->setUpdate(true)
    ->setUpdateWhitelist(array_keys($requirements))
    ->setWhitelistDependencies($input->getOption('update-with-dependencies'));
;

$status = $install->run();

Most of the command relates to the reading and writing to of the composer.json file. However the installer itself is independent of where the configuration actually came from. You could in theory store the configuration in a database.

This is the static create method for the installer:

public static function create(IOInterface $io, Composer $composer)
{
    return new static(
        $io,
        $composer->getConfig(),
        $composer->getPackage(),
        $composer->getDownloadManager(),
        $composer->getRepositoryManager(),
        $composer->getLocker(),
        $composer->getInstallationManager(),
        $composer->getEventDispatcher(),
        $composer->getAutoloadGenerator()
    );
}

You will need to pay special attention to the Package , And implement your own.

Although your current attempt to run it on the command line will work, I do not recommend it because Composer is primarily a development and deployment utility, not an application utility.

In order to smoothly use it to assist with loading plugins on a production environment, you will need to tightly integrate its internals with your own application, not just use it on the side.

This is something I am interested in as well, and I think this has inspired me to look into it myself. So I'll let you know what I come up with, but this is the best I can advise you for now on what I consider to be the correct approach.

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