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