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\registry\RegistryFactory; |
20
|
|
|
use Yii; |
21
|
|
|
|
22
|
|
|
class AssetPackage |
23
|
|
|
{ |
24
|
|
|
protected $_type; |
25
|
|
|
protected $_name; |
26
|
|
|
protected $_hash; |
27
|
|
|
protected $_releases = []; |
28
|
|
|
protected $_saved; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var NullIO |
32
|
|
|
*/ |
33
|
|
|
protected $_io; |
34
|
|
|
/** |
35
|
|
|
* @var Composer |
36
|
|
|
*/ |
37
|
|
|
protected $_composer; |
38
|
|
|
/** |
39
|
|
|
* @var Storage |
40
|
|
|
*/ |
41
|
|
|
protected $_storage; |
42
|
|
|
/** |
43
|
|
|
* @var Composer |
44
|
|
|
*/ |
45
|
|
|
protected static $_commonComposer; |
46
|
|
|
|
47
|
1 |
|
public function __construct($type, $name) |
48
|
|
|
{ |
49
|
1 |
|
if (!$this->checkType($type)) { |
50
|
|
|
throw new Exception('wrong type'); |
51
|
|
|
} |
52
|
1 |
|
if (!$this->checkName($name)) { |
53
|
|
|
throw new Exception('wrong name'); |
54
|
|
|
} |
55
|
1 |
|
$this->_type = $type; |
56
|
1 |
|
$this->_name = $name; |
57
|
1 |
|
} |
58
|
|
|
|
59
|
1 |
|
public function checkType($type) |
60
|
|
|
{ |
61
|
1 |
|
return $type === 'bower' || $type === 'npm'; |
62
|
|
|
} |
63
|
|
|
|
64
|
1 |
|
public function checkName($name) |
65
|
|
|
{ |
66
|
1 |
|
return strlen($name) > 1; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
public function getFullName() |
70
|
|
|
{ |
71
|
1 |
|
return static::buildFullName($this->_type, $this->_name); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
public static function buildFullName($type, $name) |
75
|
|
|
{ |
76
|
1 |
|
return $type . '-asset/' . $name; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public static function splitFullName($full) |
80
|
|
|
{ |
81
|
|
|
list($temp, $name) = explode('/', $full); |
82
|
|
|
list($type, $temp) = explode('-', $temp); |
|
|
|
|
83
|
|
|
|
84
|
|
|
return [$type, $name]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function getType() |
88
|
|
|
{ |
89
|
|
|
return $this->_type; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function getName() |
93
|
|
|
{ |
94
|
|
|
return $this->_name; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getHash() |
98
|
|
|
{ |
99
|
|
|
return $this->_hash; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return Composer |
104
|
|
|
*/ |
105
|
|
|
public static function getCommonComposer() |
106
|
|
|
{ |
107
|
|
|
if (static::$_commonComposer === null) { |
108
|
|
|
static::$_commonComposer = (new Factory)->createComposer( |
109
|
|
|
new NullIO(), |
110
|
|
|
Yii::getAlias('@composer/composer.json'), |
|
|
|
|
111
|
|
|
false, |
112
|
|
|
Yii::getAlias('@composer') |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return static::$_commonComposer; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function setComposer($value) |
120
|
|
|
{ |
121
|
|
|
$this->_composer = $value; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return Composer |
126
|
|
|
*/ |
127
|
|
|
public function getComposer() |
128
|
|
|
{ |
129
|
|
|
if ($this->_composer === null) { |
130
|
|
|
$this->_composer = static::getCommonComposer(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $this->_composer; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function getIO() |
137
|
|
|
{ |
138
|
|
|
if ($this->_io === null) { |
139
|
|
|
$this->_io = new NullIO(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return $this->_io; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* findOne. |
147
|
|
|
* |
148
|
|
|
* @param string $type |
149
|
|
|
* @param string $name |
150
|
|
|
* @return static|null |
151
|
|
|
*/ |
152
|
|
|
public static function findOne($type, $name) |
153
|
|
|
{ |
154
|
|
|
$package = new static($type, $name); |
155
|
|
|
$package->load(); |
156
|
|
|
|
157
|
|
|
return $package; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function load() |
161
|
|
|
{ |
162
|
|
|
$data = $this->getStorage()->readPackage($this); |
163
|
|
|
$this->_hash = $data['hash']; |
164
|
|
|
$this->_releases = $data['releases']; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function update() |
168
|
|
|
{ |
169
|
|
|
$registry = RegistryFactory::getRegistry($this->getType(), $this->getComposer()->getRepositoryManager()); |
170
|
|
|
$repo = $registry->buildVcsRepository($this->getName()); |
171
|
|
|
$this->_releases = $this->prepareReleases($repo); |
172
|
|
|
$this->_hash = $this->getStorage()->writePackage($this); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param AssetVcsRepository $repository |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
|
|
public function prepareReleases($repository) |
180
|
|
|
{ |
181
|
|
|
$releases = []; |
182
|
|
|
foreach ($repository->getPackages() as $package) { |
183
|
|
|
$version = $package->getVersion(); |
184
|
|
|
$release = [ |
185
|
|
|
'uid' => $this->prepareUid($version), |
186
|
|
|
'name' => $this->getFullName(), |
187
|
|
|
'version' => $version, |
188
|
|
|
]; |
189
|
|
|
if ($package->getDistUrl()) { |
190
|
|
|
$release['dist'] = [ |
191
|
|
|
'type' => $package->getDistType(), |
192
|
|
|
'url' => $package->getDistUrl(), |
193
|
|
|
'reference' => $package->getDistReference(), |
194
|
|
|
]; |
195
|
|
|
} |
196
|
|
|
if ($package->getSourceUrl()) { |
197
|
|
|
$release['source'] = [ |
198
|
|
|
'type' => $package->getSourceType(), |
199
|
|
|
'url' => $package->getSourceUrl(), |
200
|
|
|
'reference' => $package->getSourceReference(), |
201
|
|
|
]; |
202
|
|
|
} |
203
|
|
|
if ($release['dist'] || $release['source']) { |
204
|
|
|
$releases[$version] = $release; |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
return $releases; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function prepareUid($version) |
212
|
|
|
{ |
213
|
|
|
$known = $this->getSaved()->getRelease($version); |
214
|
|
|
|
215
|
|
|
return isset($known['uid']) ? $known['uid'] : $this->getStorage()->getNextID(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function getReleases() |
219
|
|
|
{ |
220
|
|
|
return $this->_releases; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function getRelease($version) |
224
|
|
|
{ |
225
|
|
|
return isset($this->_releases[$version]) ? $this->_releases[$version] : []; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function getSaved() |
229
|
|
|
{ |
230
|
|
|
if ($this->_saved === null) { |
231
|
|
|
$this->_saved = static::findOne($this->getType(), $this->getName()); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return $this->_saved; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function getStorage() |
238
|
|
|
{ |
239
|
|
|
if ($this->_storage === null) { |
240
|
|
|
$this->_storage = Storage::getInstance(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return $this->_storage; |
244
|
|
|
} |
245
|
|
|
} |
246
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.