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
|
1 |
|
public function __construct($type, $name, $config = []) |
57
|
|
|
{ |
58
|
1 |
|
parent::__construct($config); |
59
|
|
|
|
60
|
1 |
|
if (!$this->checkType($type)) { |
61
|
|
|
throw new Exception('wrong type'); |
62
|
|
|
} |
63
|
1 |
|
if (!$this->checkName($name)) { |
64
|
|
|
throw new Exception('wrong name'); |
65
|
|
|
} |
66
|
1 |
|
$this->_type = $type; |
67
|
1 |
|
$this->_name = $name; |
68
|
1 |
|
} |
69
|
|
|
|
70
|
|
|
public function getRegistry() { |
71
|
|
|
if ($this->_registry === null) { |
72
|
|
|
$this->_registry = RegistryFactory::getRegistry($this->getType(), $this->getComposer()->getRepositoryManager()); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $this->_registry; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function checkType($type) |
79
|
1 |
|
{ |
80
|
|
|
return $type === 'bower' || $type === 'npm'; |
81
|
1 |
|
} |
82
|
|
|
|
83
|
|
|
public function checkName($name) |
84
|
1 |
|
{ |
85
|
|
|
return strlen($name) > 1; |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
public function getFullName() |
89
|
1 |
|
{ |
90
|
|
|
return static::buildFullName($this->_type, $this->_name); |
91
|
1 |
|
} |
92
|
|
|
|
93
|
|
|
public static function buildFullName($type, $name) |
94
|
1 |
|
{ |
95
|
|
|
return $type . '-asset/' . $name; |
96
|
1 |
|
} |
97
|
|
|
|
98
|
|
|
public static function splitFullName($full) |
99
|
|
|
{ |
100
|
|
|
list($temp, $name) = explode('/', $full); |
101
|
|
|
list($type,) = explode('-', $temp); |
102
|
|
|
|
103
|
|
|
return [$type, $name]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getType() |
107
|
|
|
{ |
108
|
|
|
return $this->_type; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getName() |
112
|
|
|
{ |
113
|
|
|
return $this->_name; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function getHash() |
117
|
|
|
{ |
118
|
|
|
return $this->_hash; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return Composer |
123
|
|
|
*/ |
124
|
|
|
public static function getCommonComposer() |
125
|
|
|
{ |
126
|
|
|
if (static::$_commonComposer === null) { |
127
|
|
|
static::$_commonComposer = (new Factory)->createComposer( |
128
|
|
|
new NullIO(), |
129
|
|
|
Yii::getAlias('@composer/composer.json'), |
|
|
|
|
130
|
|
|
false, |
131
|
|
|
Yii::getAlias('@composer') |
132
|
|
|
); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return static::$_commonComposer; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function setComposer($value) |
139
|
|
|
{ |
140
|
|
|
$this->_composer = $value; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return Composer |
145
|
|
|
*/ |
146
|
|
|
public function getComposer() |
147
|
|
|
{ |
148
|
|
|
if ($this->_composer === null) { |
149
|
|
|
$this->_composer = static::getCommonComposer(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $this->_composer; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function getIO() |
156
|
|
|
{ |
157
|
|
|
if ($this->_io === null) { |
158
|
|
|
$this->_io = new NullIO(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $this->_io; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* findOne. |
166
|
|
|
* |
167
|
|
|
* @param string $type |
168
|
|
|
* @param string $name |
169
|
|
|
* @return static|null |
170
|
|
|
*/ |
171
|
|
|
public static function findOne($type, $name) |
172
|
|
|
{ |
173
|
|
|
$package = new static($type, $name); |
174
|
|
|
$package->load(); |
175
|
|
|
|
176
|
|
|
return $package; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function load() |
180
|
|
|
{ |
181
|
|
|
$data = $this->getStorage()->readPackage($this); |
182
|
|
|
if ($data !== null) { |
183
|
|
|
$this->_hash = $data['hash']; |
184
|
|
|
$this->_releases = $data['releases']; |
185
|
|
|
$this->_updateTime = $data['updateTime']; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function update() |
190
|
|
|
{ |
191
|
|
|
$repo = $this->getRegistry()->buildVcsRepository($this->getName()); |
192
|
|
|
$this->_releases = $this->prepareReleases($repo); |
193
|
|
|
$this->_hash = $this->getStorage()->writePackage($this); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @param AssetVcsRepository $repository |
198
|
|
|
* @return array |
199
|
|
|
*/ |
200
|
|
|
public function prepareReleases($repository) |
201
|
|
|
{ |
202
|
|
|
$releases = []; |
203
|
|
|
foreach ($repository->getPackages() as $package) { |
204
|
|
|
$version = $package->getVersion(); |
205
|
|
|
$release = [ |
206
|
|
|
'uid' => $this->prepareUid($version), |
207
|
|
|
'name' => $this->getFullName(), |
208
|
|
|
'version' => $version, |
209
|
|
|
]; |
210
|
|
|
if ($package->getDistUrl()) { |
211
|
|
|
$release['dist'] = [ |
212
|
|
|
'type' => $package->getDistType(), |
213
|
|
|
'url' => $package->getDistUrl(), |
214
|
|
|
'reference' => $package->getDistReference(), |
215
|
|
|
]; |
216
|
|
|
} |
217
|
|
|
if ($package->getSourceUrl()) { |
218
|
|
|
$release['source'] = [ |
219
|
|
|
'type' => $package->getSourceType(), |
220
|
|
|
'url' => $package->getSourceUrl(), |
221
|
|
|
'reference' => $package->getSourceReference(), |
222
|
|
|
]; |
223
|
|
|
} |
224
|
|
|
if ($release['dist'] || $release['source']) { |
225
|
|
|
$releases[$version] = $release; |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
return $releases; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
public function prepareUid($version) |
233
|
|
|
{ |
234
|
|
|
$known = $this->getSaved()->getRelease($version); |
235
|
|
|
|
236
|
|
|
return isset($known['uid']) ? $known['uid'] : $this->getStorage()->getNextID(); |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function getReleases() |
240
|
|
|
{ |
241
|
|
|
return $this->_releases; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
public function getRelease($version) |
245
|
|
|
{ |
246
|
|
|
return isset($this->_releases[$version]) ? $this->_releases[$version] : []; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
public function getSaved() |
250
|
|
|
{ |
251
|
|
|
if ($this->_saved === null) { |
252
|
|
|
$this->_saved = static::findOne($this->getType(), $this->getName()); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return $this->_saved; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @return Storage |
260
|
|
|
*/ |
261
|
|
|
public function getStorage() |
262
|
|
|
{ |
263
|
|
|
return Yii::$app->get('packageStorage'); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* Returns the latest update time (UNIX Epoch) |
268
|
|
|
* @return int|null |
269
|
|
|
*/ |
270
|
|
|
public function getUpdateTime() |
271
|
|
|
{ |
272
|
|
|
return $this->_updateTime; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* Package can be updated not more often than once in 10 min |
277
|
|
|
* @return bool |
278
|
|
|
*/ |
279
|
|
|
public function canBeUpdated() |
280
|
|
|
{ |
281
|
|
|
return time() - $this->getUpdateTime() > 60*10; // 10 min |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Whether tha package should be auth-updated (if it is older than 1 day) |
286
|
|
|
* @return bool |
287
|
|
|
*/ |
288
|
|
|
public function canAutoUpdate() |
289
|
|
|
{ |
290
|
|
|
return time() - $this->getUpdateTime() > 60*60*24; // 1 day |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|
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..