|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PiouPiou\RibsAdminBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface; |
|
6
|
|
|
|
|
7
|
|
|
class PackagistApi |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var HttpClientInterface |
|
11
|
|
|
*/ |
|
12
|
|
|
private $client; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* PackagistApi constructor. |
|
16
|
|
|
* @param HttpClientInterface $client |
|
17
|
|
|
*/ |
|
18
|
|
|
public function __construct(HttpClientInterface $client) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->client = $client; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param string $package_name |
|
25
|
|
|
* @return false|mixed |
|
26
|
|
|
*/ |
|
27
|
|
|
public function getPackageInformation(string $package_name) |
|
28
|
|
|
{ |
|
29
|
|
|
if ($this->package_info) { |
|
30
|
|
|
return $this->package_info; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
if (!strpos($package_name, "/")) { |
|
34
|
|
|
return false; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$packgist_url = "https://repo.packagist.org/p/".$package_name.".json"; |
|
38
|
|
|
$response = $this->client->request("GET", $packgist_url); |
|
39
|
|
|
|
|
40
|
|
|
if ($response->getStatusCode() == 200) { |
|
41
|
|
|
$content = json_decode($response->getContent(), true); |
|
42
|
|
|
if (is_array($content) && $content["packages"] && $content["packages"][$this->package->getPackageName()]) { |
|
|
|
|
|
|
43
|
|
|
$this->package_info = $content["packages"][$this->package->getPackageName()]; |
|
|
|
|
|
|
44
|
|
|
return $this->package_info; |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
return false; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $package_name |
|
53
|
|
|
* @return false|int|string|null |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getLastPackagistVersion(string $package_name) |
|
56
|
|
|
{ |
|
57
|
|
|
if (!strpos($package_name, "/")) { |
|
58
|
|
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$packgist_url = "https://repo.packagist.org/p/".$package_name.".json"; |
|
62
|
|
|
|
|
63
|
|
|
$response = $this->client->request("GET", $packgist_url); |
|
64
|
|
|
|
|
65
|
|
|
if ($response->getStatusCode() == 200) { |
|
66
|
|
|
$content = json_decode($response->getContent(), true); |
|
67
|
|
|
if (is_array($content) && $content["packages"] && $content["packages"][$this->package->getPackageName()]) { |
|
|
|
|
|
|
68
|
|
|
return array_key_first($content["packages"][$this->package->getPackageName()]); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|