summaryrefslogtreecommitdiffstats
path: root/class/PackagistClient.php
blob: 2cb1260655de935695293bb6c5290570e1af2987 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * PackagistClient is a PHP class to interact with web services
 *
 * PHP version 5
 *
 * Copyright (C) 2014 Remi Collet
 * http://github.com/remicollet/rpmphp.
 *
 * Inspired from python-fedora
 *
 * PackagistClient is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * PackagistClient is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * See <http://www.gnu.org/licenses/>
 *
 * @category  Main
 * @package   PackagistClient
 *
 * @author    Remi Collet <remi@fedoraproject.org>
 * @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;
    }
}