Skip to content

Instantly share code, notes, and snippets.

@mlocati
Created December 8, 2020 11:44

Revisions

  1. mlocati created this gist Dec 8, 2020.
    73 changes: 73 additions & 0 deletions list-pecl-versions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    <?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);
    }