This repository was archived by the owner on Mar 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathGithubStrategy.php
234 lines (205 loc) · 5.63 KB
/
GithubStrategy.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* Humbug
*
* @category Humbug
* @package Humbug
* @copyright Copyright (c) 2015 Pádraic Brady (http://blog.astrumfutura.com)
* @license https://github.com/padraic/phar-updater/blob/master/LICENSE New BSD License
*
* This class is partially patterned after Composer's self-update.
*/
namespace Humbug\SelfUpdate\Strategy;
use Humbug\SelfUpdate\Updater;
use Humbug\SelfUpdate\VersionParser;
use Humbug\SelfUpdate\Exception\HttpRequestException;
use Humbug\SelfUpdate\Exception\InvalidArgumentException;
use Humbug\SelfUpdate\Exception\JsonParsingException;
class GithubStrategy implements StrategyInterface
{
const API_URL = 'https://packagist.org/packages/%s.json';
const STABLE = 'stable';
const UNSTABLE = 'unstable';
const ANY = 'any';
/**
* @var string
*/
private $localVersion;
/**
* @var string
*/
private $remoteVersion;
/**
* @var string
*/
private $remoteUrl;
/**
* @var string
*/
private $pharName;
/**
* @var string
*/
private $packageName;
/**
* @var string
*/
private $stability = self::STABLE;
/**
* Download the remote Phar file.
*
* @param Updater $updater
* @return void
*/
public function download(Updater $updater)
{
/** Switch remote request errors to HttpRequestExceptions */
set_error_handler(array($updater, 'throwHttpRequestException'));
$result = humbug_get_contents($this->remoteUrl);
restore_error_handler();
if (false === $result) {
throw new HttpRequestException(sprintf(
'Request to URL failed: %s', $this->remoteUrl
));
}
$result = file_put_contents($updater->getTempPharFile(), $result);
if (false === $result) {
throw new RuntimeException(
sprintf('Failed to write file: %s', $updater->getTempPharFile())
);
}
}
/**
* Retrieve the current version available remotely.
*
* @param Updater $updater
* @return string|bool
*/
public function getCurrentRemoteVersion(Updater $updater)
{
/** Switch remote request errors to HttpRequestExceptions */
set_error_handler(array($updater, 'throwHttpRequestException'));
$packageUrl = $this->getApiUrl();
$package = json_decode(humbug_get_contents($packageUrl), true);
restore_error_handler();
if (null === $package || json_last_error() !== JSON_ERROR_NONE) {
throw new JsonParsingException(
'Error parsing JSON package data'
. (function_exists('json_last_error_msg') ? ': ' . json_last_error_msg() : '')
);
}
$versions = array_keys($package['package']['versions']);
$versionParser = new VersionParser($versions);
if ($this->getStability() === self::STABLE) {
$this->remoteVersion = $versionParser->getMostRecentStable();
} elseif ($this->getStability() === self::UNSTABLE) {
$this->remoteVersion = $versionParser->getMostRecentUnstable();
} else {
$this->remoteVersion = $versionParser->getMostRecentAll();
}
/**
* Setup remote URL if there's an actual version to download
*/
if (!empty($this->remoteVersion)) {
$this->remoteUrl = $this->getDownloadUrl($package);
}
return $this->remoteVersion;
}
/**
* Retrieve the current version of the local phar file.
*
* @param Updater $updater
* @return string
*/
public function getCurrentLocalVersion(Updater $updater)
{
return $this->localVersion;
}
/**
* Set version string of the local phar
*
* @param string $version
*/
public function setCurrentLocalVersion($version)
{
$this->localVersion = $version;
}
/**
* Set Package name
*
* @param string $name
*/
public function setPackageName($name)
{
$this->packageName = $name;
}
/**
* Get Package name
*
* @return string
*/
public function getPackageName()
{
return $this->packageName;
}
/**
* Set phar file's name
*
* @param string $name
*/
public function setPharName($name)
{
$this->pharName = $name;
}
/**
* Get phar file's name
*
* @return string
*/
public function getPharName()
{
return $this->pharName;
}
/**
* Set target stability
*
* @param string $stability
*/
public function setStability($stability)
{
if ($stability !== self::STABLE && $stability !== self::UNSTABLE && $stability !== self::ANY) {
throw new InvalidArgumentException(
'Invalid stability value. Must be one of "stable", "unstable" or "any".'
);
}
$this->stability = $stability;
}
/**
* Get target stability
*
* @return string
*/
public function getStability()
{
return $this->stability;
}
protected function getApiUrl()
{
return sprintf(self::API_URL, $this->getPackageName());
}
protected function getDownloadUrl(array $package)
{
$baseUrl = preg_replace(
'{\.git$}',
'',
$package['package']['versions'][$this->remoteVersion]['source']['url']
);
$downloadUrl = sprintf(
'%s/releases/download/%s/%s',
$baseUrl,
$this->remoteVersion,
$this->getPharName()
);
return $downloadUrl;
}
}