Skip to content

Instantly share code, notes, and snippets.

@mlocati
Created December 8, 2020 11:44
Show Gist options
  • Save mlocati/985ea9d260adf7a27e7191229386c6f8 to your computer and use it in GitHub Desktop.
Save mlocati/985ea9d260adf7a27e7191229386c6f8 to your computer and use it in GitHub Desktop.
List all currently defined versions of PECL packages
<?php
set_error_handler(
static function ($errno, $errstr, $errfile, $errline)
{
$message = trim((string) $errstr);
if ($message === '') {
$message = "Unknown error (code: {$errno})";
}
if ($errfile) {
$message .= "\\File: {$errfile}";
if ($errline) {
$message .= "\nLine: {$errline}";
}
}
throw new RuntimeException($message);
},
-1
);
/**
* @return string[]
*/
function listPackages(): Generator
{
$xml = file_get_contents('https://pecl.php.net/rest/p/packages.xml');
$doc = new DOMDocument();
$doc->loadXML($xml);
$xpath = new DOMXpath($doc);
$xpath->registerNamespace('p', 'http://pear.php.net/dtd/rest.allpackages');
foreach ($xpath->query('/p:a/p:p') as $p) {
yield $p->nodeValue;
}
}
/**
* @return string[]
*/
function listPackageVersions(string $package): Generator
{
try {
$xml = file_get_contents("https://pecl.php.net/rest/r/{$package}/allreleases.xml");
} catch (RuntimeException $x) {
if (strpos($x->getMessage(), '404 Not Found') === false) {
throw $x;
}
return;
}
$doc = new DOMDocument();
$doc->loadXML($xml);
$xpath = new DOMXpath($doc);
$xpath->registerNamespace('ar', 'http://pear.php.net/dtd/rest.allreleases');
foreach ($xpath->query('/ar:a/ar:r/ar:v') as $v) {
yield $v->nodeValue;
}
}
try {
$versions = [];
foreach (listPackages() as $p) {
$p = strtolower($p);
foreach (listPackageVersions($p) as $v) {
if (!in_array($v, $versions, true)) {
$versions[] = $v;
}
}
}
usort($versions, 'version_compare');
echo implode("\n", $versions);
} catch (RuntimeException $x) {
fwrite(STDERR, $x->getMessage() . "\n");
exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment