1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Asset Packagist |
5
|
|
|
* |
6
|
|
|
* @link https://github.com/hiqdev/asset-packagist |
7
|
|
|
* @package asset-packagist |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
* @copyright Copyright (c) 2016, HiQDev (http://hiqdev.com/) |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace hiqdev\assetpackagist\models; |
13
|
|
|
|
14
|
|
|
use Composer\Composer; |
15
|
|
|
use Composer\Factory; |
16
|
|
|
use Composer\IO\NullIO; |
17
|
|
|
use Exception; |
18
|
|
|
use Fxp\Composer\AssetPlugin\Repository\AssetVcsRepository; |
19
|
|
|
use hiqdev\assetpackagist\components\Storage; |
20
|
|
|
use hiqdev\assetpackagist\registry\BowerRegistry; |
21
|
|
|
use hiqdev\assetpackagist\registry\NpmRegistry; |
22
|
|
|
use hiqdev\assetpackagist\registry\RegistryFactory; |
23
|
|
|
use Yii; |
24
|
|
|
use yii\base\Object; |
25
|
|
|
|
26
|
|
|
class AssetPackage extends Object |
27
|
|
|
{ |
28
|
|
|
protected $_type; |
29
|
|
|
protected $_name; |
30
|
|
|
protected $_hash; |
31
|
|
|
protected $_releases = []; |
32
|
|
|
protected $_saved; |
33
|
|
|
/** |
34
|
|
|
* @var AssetVcsRepository|BowerRegistry|NpmRegistry |
35
|
|
|
*/ |
36
|
|
|
protected $_registry; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var integer UNIX Epoch timestamp of the latest package update |
40
|
|
|
*/ |
41
|
|
|
protected $_updateTime; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var NullIO |
45
|
|
|
*/ |
46
|
|
|
protected $_io; |
47
|
|
|
/** |
48
|
|
|
* @var Composer |
49
|
|
|
*/ |
50
|
|
|
protected $_composer; |
51
|
|
|
/** |
52
|
|
|
* @var Composer |
53
|
|
|
*/ |
54
|
|
|
protected static $_commonComposer; |
55
|
|
|
|
56
|
|
|
public static function normalizeName($name) |
57
|
|
|
{ |
58
|
|
|
return strtolower($name); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
public function __construct($type, $name, $config = []) |
62
|
|
|
{ |
63
|
1 |
|
parent::__construct($config); |
64
|
|
|
|
65
|
1 |
|
if (!$this->checkType($type)) { |
66
|
|
|
throw new Exception('wrong type'); |
67
|
|
|
} |
68
|
1 |
|
if (!$this->checkName($name)) { |
69
|
|
|
throw new Exception('wrong name'); |
70
|
|
|
} |
71
|
1 |
|
$this->_type = $type; |
72
|
1 |
|
$this->_name = $name; |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
public function getRegistry() { |
76
|
|
|
if ($this->_registry === null) { |
77
|
|
|
$this->_registry = RegistryFactory::getRegistry($this->getType(), $this->getComposer()->getRepositoryManager()); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->_registry; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function checkType($type) |
84
|
1 |
|
{ |
85
|
|
|
return $type === 'bower' || $type === 'npm'; |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
public function checkName($name) |
89
|
1 |
|
{ |
90
|
|
|
return strlen($name) > 1; |
91
|
1 |
|
} |
92
|
|
|
|
93
|
|
|
public function getFullName() |
94
|
1 |
|
{ |
95
|
|
|
return static::buildFullName($this->_type, $this->_name); |
96
|
1 |
|
} |
97
|
|
|
|
98
|
|
|
public static function buildFullName($type, $name) |
99
|
1 |
|
{ |
100
|
|
|
return $type . '-asset/' . $name; |
101
|
1 |
|
} |
102
|
|
|
|
103
|
|
|
public static function splitFullName($full) |
104
|
|
|
{ |
105
|
|
|
list($temp, $name) = explode('/', $full); |
106
|
|
|
list($type,) = explode('-', $temp); |
107
|
|
|
|
108
|
|
|
return [$type, $name]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getType() |
112
|
|
|
{ |
113
|
|
|
return $this->_type; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getNormalName() |
117
|
|
|
{ |
118
|
|
|
return static::buildFullName($this->_type, static::normalizeName($this->_name)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function getName() |
122
|
|
|
{ |
123
|
|
|
return $this->_name; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function getHash() |
127
|
|
|
{ |
128
|
|
|
return $this->_hash; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return Composer |
133
|
|
|
*/ |
134
|
|
|
public static function getCommonComposer() |
135
|
|
|
{ |
136
|
|
|
if (static::$_commonComposer === null) { |
137
|
|
|
static::$_commonComposer = (new Factory)->createComposer( |
138
|
|
|
new NullIO(), |
139
|
|
|
Yii::getAlias('@composer/composer.json'), |
|
|
|
|
140
|
|
|
false, |
141
|
|
|
Yii::getAlias('@composer') |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return static::$_commonComposer; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function setComposer($value) |
149
|
|
|
{ |
150
|
|
|
$this->_composer = $value; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return Composer |
155
|
|
|
*/ |
156
|
|
|
public function getComposer() |
157
|
|
|
{ |
158
|
|
|
if ($this->_composer === null) { |
159
|
|
|
$this->_composer = static::getCommonComposer(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $this->_composer; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function getIO() |
166
|
|
|
{ |
167
|
|
|
if ($this->_io === null) { |
168
|
|
|
$this->_io = new NullIO(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->_io; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* findOne. |
176
|
|
|
* |
177
|
|
|
* @param string $type |
178
|
|
|
* @param string $name |
179
|
|
|
* @return static|null |
180
|
|
|
*/ |
181
|
|
|
public static function findOne($type, $name) |
182
|
|
|
{ |
183
|
|
|
$package = new static($type, $name); |
184
|
|
|
$package->load(); |
185
|
|
|
|
186
|
|
|
return $package; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function load() |
190
|
|
|
{ |
191
|
|
|
$data = $this->getStorage()->readPackage($this); |
192
|
|
|
if ($data !== null) { |
193
|
|
|
$this->_hash = $data['hash']; |
194
|
|
|
$this->_releases = $data['releases']; |
195
|
|
|
$this->_updateTime = $data['updateTime']; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function update() |
200
|
|
|
{ |
201
|
|
|
$repo = $this->getRegistry()->buildVcsRepository($this->getName()); |
202
|
|
|
$this->_releases = $this->prepareReleases($repo); |
203
|
|
|
$this->_hash = $this->getStorage()->writePackage($this); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param AssetVcsRepository $repository |
208
|
|
|
* @return array |
209
|
|
|
*/ |
210
|
|
|
public function prepareReleases($repository) |
211
|
|
|
{ |
212
|
|
|
$releases = []; |
213
|
|
|
foreach ($repository->getPackages() as $package) { |
214
|
|
|
$version = $package->getVersion(); |
215
|
|
|
$release = [ |
216
|
|
|
'uid' => $this->prepareUid($version), |
217
|
|
|
'name' => $this->getNormalName(), |
218
|
|
|
'version' => $version, |
219
|
|
|
]; |
220
|
|
|
if ($package->getDistUrl()) { |
221
|
|
|
$release['dist'] = [ |
222
|
|
|
'type' => $package->getDistType(), |
223
|
|
|
'url' => $package->getDistUrl(), |
224
|
|
|
'reference' => $package->getDistReference(), |
225
|
|
|
]; |
226
|
|
|
} |
227
|
|
|
if ($package->getSourceUrl()) { |
228
|
|
|
$release['source'] = [ |
229
|
|
|
'type' => $package->getSourceType(), |
230
|
|
|
'url' => $package->getSourceUrl(), |
231
|
|
|
'reference' => $package->getSourceReference(), |
232
|
|
|
]; |
233
|
|
|
} |
234
|
|
|
if ($release['dist'] || $release['source']) { |
235
|
|
|
$releases[$version] = $release; |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
return $releases; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function prepareUid($version) |
243
|
|
|
{ |
244
|
|
|
$known = $this->getSaved()->getRelease($version); |
245
|
|
|
|
246
|
|
|
return isset($known['uid']) ? $known['uid'] : $this->getStorage()->getNextID(); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function getReleases() |
250
|
|
|
{ |
251
|
|
|
return $this->_releases; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
public function getRelease($version) |
255
|
|
|
{ |
256
|
|
|
return isset($this->_releases[$version]) ? $this->_releases[$version] : []; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
public function getSaved() |
260
|
|
|
{ |
261
|
|
|
if ($this->_saved === null) { |
262
|
|
|
$this->_saved = static::findOne($this->getType(), $this->getName()); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
return $this->_saved; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @return Storage |
270
|
|
|
*/ |
271
|
|
|
public function getStorage() |
272
|
|
|
{ |
273
|
|
|
return Yii::$app->get('packageStorage'); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Returns the latest update time (UNIX Epoch) |
278
|
|
|
* @return int|null |
279
|
|
|
*/ |
280
|
|
|
public function getUpdateTime() |
281
|
|
|
{ |
282
|
|
|
return $this->_updateTime; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Package can be updated not more often than once in 10 min |
287
|
|
|
* @return bool |
288
|
|
|
*/ |
289
|
|
|
public function canBeUpdated() |
290
|
|
|
{ |
291
|
|
|
return time() - $this->getUpdateTime() > 60*10; // 10 min |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Whether tha package should be auth-updated (if it is older than 1 day) |
296
|
|
|
* @return bool |
297
|
|
|
*/ |
298
|
|
|
public function canAutoUpdate() |
299
|
|
|
{ |
300
|
|
|
return time() - $this->getUpdateTime() > 60*60*24; // 1 day |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..