<?php

$modules = scandir('modules');

$packages = [];

$origin = [];

foreach (getNeeds(file_get_contents('composer.json')) as $package => $version) {
    if ($package !== 'php' && substr($package, 0,4) !== 'ext-') {
        $origin[$package] = $version;
    }
}

foreach ($modules as $module) {
    if ($module == '.' || $module == '..') {
        continue;
    }

    if (! is_dir('modules/'.$module)) {
        continue;
    }

    $needs = getNeeds(file_get_contents('modules/'.$module.'/composer.json'));

    foreach ($needs as $package => $version) {
        if (isset($packages[$package])) {
            if (version_compare($version, $packages[$package]) == 1) {
                $packages[$package] = $version;
            }
        } else {
            $packages[$package] = $version;
        }
    }
}

$string = '';

foreach (array_diff_key($packages, $origin) as $package => $version) {
    $string .= $package.':'.$version.' ';
}

echo $string;

function getNeeds($json): array
{
    return json_decode($json, true)['require'];
}

