简体   繁体   中英

Calling Composer from php

Is it possible to call composer update from application code with dry-run thru \\Composer\\ namespace to get information about udpates ?

I have searched in google and found only info about Composer plugins or writing (post|pre)-(install|update) hook scripts, but haven't found any info about getting such information.

SOLVED: Worked putting custom composer script in pre-update-cmd :

<?php

namespace MyNamespace;

use Composer\Script\Event;
use Composer\Plugin\CommandEvent;
use Composer\Plugin\PluginEvents;

class CheckStatus
{
    public static function preUpdate(Event $event)
    {
        /* get composer */
        $composer = $event->getComposer();

        $installedRepo = $composer->getRepositoryManager()->getLocalRepository();

        $dm = $composer->getDownloadManager();
        $im = $composer->getInstallationManager();

        $errors = array();

        /* list packages */
        foreach ($installedRepo->getPackages() as $package) {
            $downloader = $dm->getDownloaderForInstalledPackage($package);

            if ($downloader instanceof ChangeReportInterface) {
                $targetDir = $im->getInstallPath($package);

                if ($changes = $downloader->getLocalChanges($package, $targetDir)) {
                    $errors[$targetDir] = $changes;
                }
            }
        }

        if (!$errors) {
            $status['changes'] = null;
        } else {
            $status['changes'] = $errors;
        }

        // in $status['changes'] we have all pending updates
    }
}

</code>

Did you try command exec("php composer.phar update") exec description ?

UPDATED AFTER DISCUSSION:
The answer on code composer.phar/src/Composer/Command/StatusCommand.php [lines 43-92] - it can used for check updates of repositories

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