0

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>
brzuchal
  • 649
  • 8
  • 19
  • Duplicate? http://stackoverflow.com/questions/17219436/run-composer-with-a-php-script-in-browser/17244866#17244866 – Danack Oct 10 '13 at 21:43
  • That's quite the same as @CreatoR 's answer, but I found out that I could extend https://github.com/composer/composer/blob/master/src/Composer/Command/UpdateCommand.php class (which noirmally is in composer.phar or just in vendor dir when added "composer/composer" in dev package to my composer.json) and write more like returning an array/object with pending updates info. – brzuchal Oct 11 '13 at 06:28

1 Answers1

0

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

CreatoR
  • 1,654
  • 10
  • 14
  • 1
    Your missing my question, I wan't somehow call Compsoer classes creating instance of composer and then run update to get update information. I'm not going to parse exec('php composer.phar update'), the cli ansi output is for human, parsing it is the last thing i want to do. – brzuchal Oct 10 '13 at 17:35
  • Ok. Another think: try to extract composer.phar and getting sources (I found this function http://php.net/manual/en/phar.extractto.php or you can get direct access to files from PhpStorm). Than you will see logic and correct it for your purposes – CreatoR Oct 11 '13 at 07:06
  • Yes, the logic I can see in code of package "composer/composer". I found out that there is possibility of getting composer instance, repository manager, package and installation manager through (post|pre)-(install|update)-cmd scripts, but haven't yet found out how to use them to get info about pending updates. – brzuchal Oct 11 '13 at 09:03
  • 2
    Look at file: `composer.phar/src/Composer/Command/StatusCommand.php [lines 43-92]`. I think it is code what you need. – CreatoR Oct 11 '13 at 11:49