Skip to content

Instantly share code, notes, and snippets.

@alankent
Created May 13, 2016 04:59
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save alankent/aeacf82f86627dd96aa4df242493103f to your computer and use it in GitHub Desktop.
Save alankent/aeacf82f86627dd96aa4df242493103f to your computer and use it in GitHub Desktop.
Connect to Magento 2 Composer repo and list packages you can access
<?php
/**
* Return the path of the auth.json file.
*/
function findAuthJson() {
// Windows sets HOMEDRIVE and HOMEPATH, but cygwin sets HOME.
if (!isset($_SERVER["HOME"]) && isset($_SERVER["HOMEDRIVE"])) {
$home = $_SERVER["HOMEDRIVE"] . $_SERVER["HOMEPATH"];
} else {
$home = getenv("HOME");
}
$paths = [
$home . '/.composer/auth.json',
'auth.json',
'app/etc/composer/auth.json'
];
foreach ($paths as $path) {
if (file_exists($path)) {
return $path;
}
}
echo "Unable to find 'auth.json' file holding composer repo keys\n";
exit(1);
}
/**
* Returns username and password from ~/.composer/auth.json.
* Returned in assciation with keys "username" and "password".
*/
function getAuthUsernamePassword() {
$authName = findAuthJson();
$auth = json_decode(file_get_contents($authName), true);
$up = $auth["http-basic"]["repo.magento.com"];
return $up;
}
/**
* Fetch packages.json file.
*/
function getPackagesJson() {
$userpass = getAuthUsernamePassword();
$process = curl_init();
curl_setopt($process, CURLOPT_URL, 'https://repo.magento.com/packages.json');
curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($process, CURLOPT_USERPWD, $userpass['username'] . ":" . $userpass['password']);
curl_setopt($process, CURLOPT_TIMEOUT, 30);
curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($process);
curl_close($process);
return json_decode($return, true);
}
$packagesJson = getPackagesJson();
foreach ($packagesJson['packages'] as $packageName => $versions) {
$keep = false;
foreach ($versions as $version => $package) {
$description = $package['description'];
$type = $package['type'];
if ($type !== 'magento2-module'
&& $type !== 'magento2-library'
&& $type !== 'magento2-component'
&& $type !== 'library') {
$keep = true;
} else if (substr($packageName, 0, 8) !== "magento/") {
$keep = true;
}
}
if ($keep) {
$versionNumbers = implode(' ', array_keys($versions));
echo "$packageName [$type]\n";
foreach ($versions as $version => $package) {
$dep = '';
if (isset($package['require'])) {
foreach ($package['require'] as $dp => $dv) {
if ($dp !== 'php' && $dp !== 'composer/composer') {
$dep .= " $dp:$dv";
}
}
}
$line = " $version$dep";
if (strlen($line) > 78) {
$line = substr($line, 0, 75) . '...';
}
echo "$line\n";
}
$d = wordwrap($description, 72, "\n ");
echo " $d\n\n";
}
}
@alankent
Copy link
Author

Possible improvements

  • Read the composer.json file and highlight entries not in the current file
  • Highlight when a different version is available (from what is in composer.json file)
  • Report only version numbers compatible with current composer.json file (hard!)

@hostep
Copy link

hostep commented May 22, 2016

Hi Alan

Thanks for this small script.

But you should re-order your paths where your script searches for the auth.json file, the $home . '/.composer/auth.json' should be the last place to search.

For example, I have a global auth.json file for my user in ~/.composer/auth.json but this only contains my github credentials, not the credentials for my Magento 2 projects, as I use an auth.json file per project inside the root of that project.
So your current script would fail to find the correct credentials in this case.

I would suggest you change the order to:

    $paths = [
        'auth.json',
        'app/etc/composer/auth.json',
        $home . '/.composer/auth.json',
    ];

I'm not sure about the app/etc/composer/auth.json file, I've never seen that before, can you shed some light on in what circumstances this file can be used?

Thanks!

@andyexeter
Copy link

For future searchers, this information can now be found within the My Purchases section of Magento Marketplace.

It is listed as the "Component name" in the Technical details popover that appears below the Download button for each module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment