1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PiouPiou\RibsAdminBundle\Service; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface; |
7
|
|
|
|
8
|
|
|
class PackagistApi |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var HttpClientInterface |
12
|
|
|
*/ |
13
|
|
|
private $client; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $package_name = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $package_info = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* PackagistApi constructor. |
27
|
|
|
* @param HttpClientInterface $client |
28
|
|
|
*/ |
29
|
|
|
public function __construct(HttpClientInterface $client) |
30
|
|
|
{ |
31
|
|
|
$this->client = $client; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return false|mixed |
36
|
|
|
*/ |
37
|
|
|
private function getPackageInformation() |
38
|
|
|
{ |
39
|
|
|
if ($this->package_info) { |
|
|
|
|
40
|
|
|
return $this->package_info; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (!$this->package_name || !strpos($this->package_name, "/")) { |
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$packgist_url = "https://repo.packagist.org/p/".$this->package_name.".json"; |
48
|
|
|
$response = $this->client->request("GET", $packgist_url); |
49
|
|
|
|
50
|
|
|
if ($response->getStatusCode() == 200) { |
51
|
|
|
$content = json_decode($response->getContent(), true); |
52
|
|
|
if (is_array($content) && $content["packages"] && $content["packages"][$this->package_name]) { |
53
|
|
|
$this->package_info = $content["packages"][$this->package_name]; |
54
|
|
|
return $this->package_info; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $package_name |
63
|
|
|
* @return false|int|string|null |
64
|
|
|
*/ |
65
|
|
|
public function getLastPackagistVersion(string $package_name) |
66
|
|
|
{ |
67
|
|
|
$this->package_name = $package_name; |
68
|
|
|
if ($package = $this->getPackageInformation()) { |
69
|
|
|
return array_key_first($package); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getAllPackagistVersions(string $package_name) |
76
|
|
|
{ |
77
|
|
|
$this->package_name = $package_name; |
78
|
|
|
if ($package = $this->getPackageInformation()) { |
79
|
|
|
return array_keys($package); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.