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
|
|
|
* Version constructor. |
27
|
|
|
* @param EntityManagerInterface $em |
28
|
|
|
* @param HttpClientInterface $client |
29
|
|
|
*/ |
30
|
|
|
public function __construct(EntityManagerInterface $em, HttpClientInterface $client) |
31
|
|
|
{ |
32
|
|
|
$this->em = $em; |
33
|
|
|
$this->client = $client; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return mixed|null |
38
|
|
|
*/ |
39
|
|
|
private function getComposerLockJson() |
40
|
|
|
{ |
41
|
|
|
$composer_lock = file_get_contents('../composer.lock'); |
42
|
|
|
|
43
|
|
|
if ($composer_lock) { |
44
|
|
|
return json_decode($composer_lock, true); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param $package_name |
52
|
|
|
* @return mixed|null |
53
|
|
|
*/ |
54
|
|
|
private function getPackage($package_name) |
55
|
|
|
{ |
56
|
|
|
$composer_lock = $this->getComposerLockJson(); |
57
|
|
|
if ($composer_lock) { |
58
|
|
|
$packages = $composer_lock["packages"]; |
59
|
|
|
$key = array_search($package_name, array_column($packages, 'name')); |
60
|
|
|
|
61
|
|
|
if ($key) { |
62
|
|
|
return $packages[$key]; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $package_name |
71
|
|
|
* @return mixed|null |
72
|
|
|
*/ |
73
|
|
|
public function getVersion($package_name) |
74
|
|
|
{ |
75
|
|
|
return $this->getPackage($package_name) ? $this->getPackage($package_name)["version"] : null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param $package_name |
80
|
|
|
* @return mixed|null |
81
|
|
|
* @throws Exception |
82
|
|
|
*/ |
83
|
|
|
public function getVersionDate($package_name) |
84
|
|
|
{ |
85
|
|
|
$string_date = $this->getPackage($package_name) ? explode("T", $this->getPackage($package_name)["time"])[0] : null; |
86
|
|
|
$version_date = null; |
87
|
|
|
|
88
|
|
|
if ($string_date) { |
89
|
|
|
$version_date = new \DateTime($string_date); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $version_date; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $package_name |
97
|
|
|
* @return false|int|string|null |
98
|
|
|
* @throws ClientExceptionInterface |
99
|
|
|
* @throws RedirectionExceptionInterface |
100
|
|
|
* @throws ServerExceptionInterface |
101
|
|
|
* @throws TransportExceptionInterface |
102
|
|
|
*/ |
103
|
|
|
public function getLastPackagistVersion($package_name) |
104
|
|
|
{ |
105
|
|
|
if (!strpos($package_name, "/")) { |
106
|
|
|
return false; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$packgist_url = "https://repo.packagist.org/p/".$package_name.".json"; |
110
|
|
|
|
111
|
|
|
$response = $this->client->request("GET", $packgist_url); |
112
|
|
|
|
113
|
|
|
if ($response->getStatusCode() == 200) { |
114
|
|
|
$content = json_decode($response->getContent(), true); |
115
|
|
|
if (is_array($content) && $content["packages"] && $content["packages"][$package_name]) { |
116
|
|
|
return array_key_first($content["packages"][$package_name]); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param $package_name |
123
|
|
|
* @throws Exception |
124
|
|
|
*/ |
125
|
|
|
public function save($package_name) |
126
|
|
|
{ |
127
|
|
|
$version = $this->em->getRepository(\PiouPiou\RibsAdminBundle\Entity\Version::class)->findOneBy(["package_name" => $package_name]); |
128
|
|
|
|
129
|
|
|
if (!$version) { |
130
|
|
|
$version = new \PiouPiou\RibsAdminBundle\Entity\Version(); |
131
|
|
|
$version->setProjectName($package_name); |
132
|
|
|
$version->setPackageName($package_name); |
133
|
|
|
$version->setProjectUrl($package_name); |
134
|
|
|
$version->setCheckVersionUrl($package_name); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$version->setVersion($this->getVersion($package_name)); |
|
|
|
|
138
|
|
|
$version->setVersionDate($this->getVersionDate($package_name)); |
|
|
|
|
139
|
|
|
$version->setLastPackagistVersion($this->getLastPackagistVersion($package_name)); |
|
|
|
|
140
|
|
|
$version->setLastCheck(new \DateTime()); |
141
|
|
|
|
142
|
|
|
$this->em->persist($version); |
143
|
|
|
$this->em->flush(); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|