|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PiouPiou\RibsAdminBundle\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
6
|
|
|
use PiouPiou\RibsAdminBundle\Entity\Package; |
|
7
|
|
|
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; |
|
8
|
|
|
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; |
|
9
|
|
|
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; |
|
10
|
|
|
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
|
11
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface; |
|
12
|
|
|
use Exception; |
|
13
|
|
|
|
|
14
|
|
|
class Version |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var EntityManagerInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
private $em; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var HttpClientInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $client; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var Package |
|
28
|
|
|
*/ |
|
29
|
|
|
private $package; |
|
30
|
|
|
|
|
31
|
|
|
private $messages = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Version constructor. |
|
35
|
|
|
* @param EntityManagerInterface $em |
|
36
|
|
|
* @param HttpClientInterface $client |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct(EntityManagerInterface $em, HttpClientInterface $client) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->em = $em; |
|
41
|
|
|
$this->client = $client; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getMessages(): array |
|
48
|
|
|
{ |
|
49
|
|
|
return $this->messages; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param Package $package |
|
54
|
|
|
*/ |
|
55
|
|
|
public function setPackageEntity(Package $package) { |
|
56
|
|
|
$this->package = $package; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @return mixed|null |
|
61
|
|
|
* @throws ClientExceptionInterface |
|
62
|
|
|
* @throws RedirectionExceptionInterface |
|
63
|
|
|
* @throws ServerExceptionInterface |
|
64
|
|
|
* @throws TransportExceptionInterface |
|
65
|
|
|
*/ |
|
66
|
|
|
private function getComposerLockJson() |
|
67
|
|
|
{ |
|
68
|
|
|
if ($this->package && !$this->package->isIsLocal()) { |
|
69
|
|
|
$response = $this->client->request("GET", $this->package->getProjectUrl().$this->package->getComposerLockUrl()); |
|
70
|
|
|
$composer_lock = $response->getStatusCode() == 200 ? $response->getContent() : null; |
|
71
|
|
|
} else { |
|
72
|
|
|
$composer_lock = file_get_contents('../composer.lock'); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if ($composer_lock) { |
|
76
|
|
|
return json_decode($composer_lock, true); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return null; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param $package_name |
|
84
|
|
|
* @return mixed|null |
|
85
|
|
|
* @throws ClientExceptionInterface |
|
86
|
|
|
* @throws RedirectionExceptionInterface |
|
87
|
|
|
* @throws ServerExceptionInterface |
|
88
|
|
|
* @throws TransportExceptionInterface |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getPackage($package_name) |
|
91
|
|
|
{ |
|
92
|
|
|
$composer_lock = $this->getComposerLockJson(); |
|
93
|
|
|
if ($composer_lock) { |
|
94
|
|
|
$packages = $composer_lock["packages"]; |
|
95
|
|
|
$key = array_search($package_name, array_column($packages, 'name')); |
|
96
|
|
|
|
|
97
|
|
|
if ($key) { |
|
98
|
|
|
return $packages[$key]; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->messages["composer_lock"] = "Composer lock not found at " . $this->package->getProjectUrl(); |
|
103
|
|
|
|
|
104
|
|
|
return null; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param $package_name |
|
109
|
|
|
* @return mixed|null |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getVersion($package_name) |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->getPackage($package_name) ? $this->getPackage($package_name)["version"] : null; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param $package_name |
|
118
|
|
|
* @return mixed|null |
|
119
|
|
|
* @throws Exception |
|
120
|
|
|
*/ |
|
121
|
|
|
public function getVersionDate($package_name) |
|
122
|
|
|
{ |
|
123
|
|
|
$string_date = $this->getPackage($package_name) ? explode("T", $this->getPackage($package_name)["time"])[0] : null; |
|
124
|
|
|
$version_date = null; |
|
125
|
|
|
|
|
126
|
|
|
if ($string_date) { |
|
127
|
|
|
$version_date = new \DateTime($string_date); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $version_date; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param $package_name |
|
135
|
|
|
* @return false|int|string|null |
|
136
|
|
|
* @throws ClientExceptionInterface |
|
137
|
|
|
* @throws RedirectionExceptionInterface |
|
138
|
|
|
* @throws ServerExceptionInterface |
|
139
|
|
|
* @throws TransportExceptionInterface |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getLastPackagistVersion($package_name) |
|
142
|
|
|
{ |
|
143
|
|
|
if (!strpos($package_name, "/")) { |
|
144
|
|
|
return false; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$packgist_url = "https://repo.packagist.org/p/".$package_name.".json"; |
|
148
|
|
|
|
|
149
|
|
|
$response = $this->client->request("GET", $packgist_url); |
|
150
|
|
|
|
|
151
|
|
|
if ($response->getStatusCode() == 200) { |
|
152
|
|
|
$content = json_decode($response->getContent(), true); |
|
153
|
|
|
if (is_array($content) && $content["packages"] && $content["packages"][$package_name]) { |
|
154
|
|
|
return array_key_first($content["packages"][$package_name]); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param $package_name |
|
161
|
|
|
* @throws Exception |
|
162
|
|
|
*/ |
|
163
|
|
|
public function save($package_name) |
|
164
|
|
|
{ |
|
165
|
|
|
$package = $this->em->getRepository(Package::class)->findOneBy(["package_name" => $package_name]); |
|
166
|
|
|
|
|
167
|
|
|
if (!$package) { |
|
168
|
|
|
$package = new Package(); |
|
169
|
|
|
$package->setProjectName($package_name); |
|
170
|
|
|
$package->setPackageName($package_name); |
|
171
|
|
|
$package->setProjectUrl($package_name); |
|
172
|
|
|
$package->setCheckVersionUrl($package_name); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$package->setVersion($this->getVersion($package_name)); |
|
176
|
|
|
$package->setVersionDate($this->getVersionDate($package_name)); |
|
177
|
|
|
$package->setLastPackagistVersion($this->getLastPackagistVersion($package_name)); |
|
|
|
|
|
|
178
|
|
|
$package->setLastCheck(new \DateTime()); |
|
179
|
|
|
|
|
180
|
|
|
$this->em->persist($package); |
|
181
|
|
|
$this->em->flush(); |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|