1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Asset Packagist. |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/asset-packagist |
6
|
|
|
* @package asset-packagist |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\assetpackagist\librariesio; |
12
|
|
|
|
13
|
|
|
use GuzzleHttp\Client; |
14
|
|
|
use yii\base\Component; |
15
|
|
|
|
16
|
|
|
class LibrariesioRepository extends Component |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string Base URI for https://libraries.io |
20
|
|
|
*/ |
21
|
|
|
public $baseUri = 'https://libraries.io/api/'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Without api key, has 30/request/minute hate limit |
25
|
|
|
* With api key, has 60/request/minute hate limit. |
26
|
|
|
* @var string The user API Key |
27
|
|
|
*/ |
28
|
|
|
public $apiKey; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Options for Guzzle client |
32
|
|
|
* Example: |
33
|
|
|
* [ |
34
|
|
|
* 'timeout' => 0, |
35
|
|
|
* 'allow_redirects' => false, |
36
|
|
|
* 'proxy' => '192.168.16.1:10' |
37
|
|
|
* ]. |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
public $clientOptions = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The Guzzle client. |
45
|
|
|
* @var Client |
46
|
|
|
*/ |
47
|
|
|
protected $client; |
48
|
|
|
|
49
|
|
|
public function init() |
50
|
|
|
{ |
51
|
|
|
parent::init(); |
52
|
|
|
|
53
|
|
|
$this->clientOptions['base_uri'] = $this->baseUri; |
54
|
|
|
|
55
|
|
|
$this->client = new Client($this->clientOptions); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Send request to server with api_key and return a Response. |
60
|
|
|
* @param string $method |
61
|
|
|
* @param string $uri |
62
|
|
|
* @param array $options |
63
|
|
|
* @throws \GuzzleHttp\Exception\BadResponseException |
64
|
|
|
* @return \GuzzleHttp\Psr7\Response |
65
|
|
|
*/ |
66
|
|
|
public function request($method, $uri = '', array $options = []) |
67
|
|
|
{ |
68
|
|
|
if (!isset($options['query'])) { |
69
|
|
|
$options['query'] = []; |
70
|
|
|
} |
71
|
|
|
if ($this->apiKey && !isset($options['query']['api_key'])) { |
72
|
|
|
$options['query']['api_key'] = $this->apiKey; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
try { |
76
|
|
|
return $this->client->request($method, $uri, $options); |
77
|
|
|
} catch (\GuzzleHttp\Exception\BadResponseException $ex) { |
78
|
|
|
if ($ex->hasResponse()) { |
79
|
|
|
return $ex->getResponse(); |
80
|
|
|
} |
81
|
|
|
throw $ex; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Search package in https://libraries.io. |
87
|
|
|
* @see https://libraries.io/api/#project-search |
88
|
|
|
* @param array $query |
89
|
|
|
* @return \GuzzleHttp\Psr7\Response |
90
|
|
|
*/ |
91
|
|
|
public function search($query = []) |
92
|
|
|
{ |
93
|
|
|
return $this->request('GET', 'search', ['query' => $query]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Return the package info from https://libraries.io. |
98
|
|
|
* @see https://libraries.io/api/#project |
99
|
|
|
* @param string $platform |
100
|
|
|
* @param string $name |
101
|
|
|
* @return \GuzzleHttp\Psr7\Response |
102
|
|
|
*/ |
103
|
|
|
public function getProject($platform, $name) |
104
|
|
|
{ |
105
|
|
|
return $this->request('GET', $platform . '/' . $name); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|