1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\Packagist; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
|
8
|
|
|
class Packagist |
9
|
|
|
{ |
10
|
|
|
/** @var \GuzzleHttp\Client */ |
11
|
|
|
protected $client; |
12
|
|
|
|
13
|
|
|
/** @var string */ |
14
|
|
|
protected $baseUrl; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param \GuzzleHttp\Client $client |
18
|
|
|
* @param string $baseUrl |
19
|
|
|
*/ |
20
|
|
|
public function __construct(Client $client, $baseUrl = 'https://packagist.org') |
21
|
|
|
{ |
22
|
|
|
$this->client = $client; |
23
|
|
|
|
24
|
|
|
$this->baseUrl = $baseUrl; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param string $type |
29
|
|
|
* |
30
|
|
|
* @return array |
31
|
|
|
*/ |
32
|
|
|
public function getPackagesByType($type) |
33
|
|
|
{ |
34
|
|
|
if (empty($type)) { |
35
|
|
|
throw new Exception('You must pass a non-empty value'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this->makeRequest('/packages/list.json', compact('type')); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $vendor |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public function getPackagesByVendor($vendor) |
47
|
|
|
{ |
48
|
|
|
if (empty($vendor)) { |
49
|
|
|
throw new Exception('You must pass a non empty value'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this->makeRequest('/packages/list.json', compact('vendor')); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $name |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
*/ |
60
|
|
|
public function getPackagesByName($name, $type = '') |
61
|
|
|
{ |
62
|
|
|
$query = ['q' => $name]; |
63
|
|
|
|
64
|
|
|
if ($type != '') { |
65
|
|
|
$query['type'] = $type; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->makeRequest('/search.json', $query); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $vendor |
73
|
|
|
* @param string $packageName |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
|
|
public function findPackageByName($vendor, $packageName = '') |
78
|
|
|
{ |
79
|
|
|
if ($packageName === '') { |
80
|
|
|
if (strpos($vendor, '/') === false) { |
81
|
|
|
throw new Exception('Invalid package name'); |
82
|
|
|
} |
83
|
|
|
[$vendor, $packageName] = explode('/', $vendor); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $this->makeRequest("/packages/{$vendor}/{$packageName}.json"); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $resource |
91
|
|
|
* @param array $query |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function makeRequest($resource, array $query = []) |
96
|
|
|
{ |
97
|
|
|
$packages = $this->client |
98
|
|
|
->get("{$this->baseUrl}{$resource}", compact('query')) |
99
|
|
|
->getBody() |
100
|
|
|
->getContents(); |
101
|
|
|
|
102
|
|
|
return json_decode($packages, true); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param string $name |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
public function getPackageMetadata($name) |
111
|
|
|
{ |
112
|
|
|
if ($name === '') { |
113
|
|
|
throw new Exception('You must pass a non empty value'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
[$vendor, $packageName] = explode('/', $name); |
|
|
|
|
117
|
|
|
|
118
|
|
|
return $this->makeRequest("/packages/{$vendor}/{$packageName}.json"); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.