From 54774535fd7e067722a2cacc87faf30214ab9f5e Mon Sep 17 00:00:00 2001 From: Remi Collet Date: Sun, 27 Jul 2014 17:46:16 +0200 Subject: Add Packagist parser --- class/PackagistClient.php | 115 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 class/PackagistClient.php (limited to 'class/PackagistClient.php') diff --git a/class/PackagistClient.php b/class/PackagistClient.php new file mode 100644 index 0000000..2cb1260 --- /dev/null +++ b/class/PackagistClient.php @@ -0,0 +1,115 @@ + + * + * @category Main + * @package PackagistClient + * + * @author Remi Collet + * @copyright 2010-2014 Remi Collet + * @license http://www.gnu.org/licenses/lgpl-2.1.txt LGPL License 2.1 or (at your option) any later version + * @link http://github.com/remicollet/rpmphp/ + * @since The begining of times. + */ + +if (!function_exists('curl_version')) { + die("curl extension required\n"); +} +require_once 'Cache/Lite.php'; + +class PackagistClient +{ + const URL = 'https://packagist.org/'; + protected $cache; + + function __construct () + { + $dir = "/tmp/pkgist-".posix_getlogin()."/"; + @mkdir($dir); + $this->cache = new Cache_Lite( + array( + 'memoryCaching' => true, + 'cacheDir' => $dir, + 'automaticSerialization' => true + ) + ); + } + + function getPackageData($name) + { + $url = self::URL.'packages/'.$name.'.json'; + $rep = $this->cache->get(__METHOD__, $url); + if (!$rep) { + $rep = @file_get_contents($url); + $this->cache->save($rep, __METHOD__, $url); + } + return ($rep ? json_decode($rep, true) : false); + } + + function getPackage($name) + { + $unstable = array('alpha', 'beta', 'rc'); + + $ret = false; + $pkgs = $this->getPackageData($name); + if ($pkgs) { + $ret = array( + 'name' => $name, + 'stable' => NULL, + 'unstable' => NULL, + 'state' => NULL, + ); + foreach ($pkgs['package']['versions'] as $pkver => $pkg) { + if (preg_match('/^v[0-9]/', $pkver)) { + $pkver = substr($pkver, 1); + } + if (strpos($pkver, 'dev')) { + continue; + } + $type = 'stable'; + $subt = false; + foreach($unstable as $i) { + if (stripos($pkver, $i)) { + $type = 'unstable'; + $subt = $i; + } + } + if (version_compare($pkver, $ret[$type], 'gt')) { + $ret[$type] = $pkver; + if ($subt) { + $ret['state'] = $subt; + } + } + } + if ($ret['stable']) { + if (version_compare($ret['stable'], $ret['unstable'], 'gt')) { + $ret['unstable'] = NULL; + $ret['state'] = NULL; + } + } + } + return $ret; + } +} -- cgit