1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Nikita Kotenko |
5
|
|
|
* Date: 26.11.2014 |
6
|
|
|
* Time: 14:12 |
7
|
|
|
*/ |
8
|
|
|
namespace samsonphp\composer; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Provide creating sorting list of composer packages |
13
|
|
|
* @package samson |
14
|
|
|
*/ |
15
|
|
|
class Composer |
16
|
|
|
{ |
17
|
|
|
/** @var string Path to current web-application */ |
18
|
|
|
private $systemPath; |
19
|
|
|
|
20
|
|
|
/** @var string composer lock file name */ |
21
|
|
|
private $lockFileName = 'composer.lock'; |
22
|
|
|
|
23
|
|
|
/** @var array List of available vendors */ |
24
|
|
|
private $vendorsList = array(); |
25
|
|
|
|
26
|
|
|
/** @var string $ignoreKey */ |
27
|
|
|
private $ignoreKey; |
28
|
|
|
|
29
|
|
|
/** @var string $includeKey */ |
30
|
|
|
private $includeKey; |
31
|
|
|
|
32
|
|
|
/** @var array List of ignored packages */ |
33
|
|
|
private $ignorePackages = array(); |
34
|
|
|
|
35
|
|
|
/** @var array Packages list with require packages*/ |
36
|
|
|
private $packagesList = array(); |
37
|
|
|
|
38
|
|
|
private $packagesListResorted = array(); |
39
|
|
|
|
40
|
|
|
private $packagesListExtra = array(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Add available vendor |
44
|
|
|
* @param $vendor Available vendor |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
|
|
public function vendor($vendor) |
48
|
|
|
{ |
49
|
|
|
if (!in_array($vendor, $this->vendorsList)) { |
50
|
|
|
$this->vendorsList[] = $vendor.'/'; |
51
|
|
|
} |
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Set name of composer extra parameter to ignore package |
58
|
|
|
* @param $ignoreKey Name |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public function ignoreKey($ignoreKey) |
62
|
|
|
{ |
63
|
|
|
$this->ignoreKey = $ignoreKey; |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Set name of composer extra parameter to include package |
69
|
|
|
* @param $includeKey Name |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function includeKey($includeKey) |
73
|
|
|
{ |
74
|
|
|
$this->includeKey = $includeKey; |
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Add ignored package |
80
|
|
|
* @param $vendor Ignored package |
81
|
|
|
* @return $this |
82
|
|
|
*/ |
83
|
|
|
public function ignorePackage($package) |
84
|
|
|
{ |
85
|
|
|
if (!in_array($package, $this->ignorePackages)) { |
86
|
|
|
$this->ignorePackages[] = $package; |
87
|
|
|
} |
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Create sorted packages list |
93
|
|
|
* @return array Packages list ('package name'=>'rating') |
94
|
|
|
*/ |
95
|
|
|
public function create( & $packages, $systemPath, $parameters = array() ) |
|
|
|
|
96
|
|
|
{ |
97
|
|
|
$class_vars = get_class_vars(get_class($this)); |
98
|
|
|
|
99
|
|
|
foreach ($class_vars as $name => $value) { |
100
|
|
|
if (isset($parameters[$name])) { |
101
|
|
|
$this->$name = $parameters[$name]; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
// Composer.lock is always in the project root folder |
105
|
|
|
$path = $systemPath.$this->lockFileName; |
106
|
|
|
|
107
|
|
|
// Create list of relevant packages with there require packages |
108
|
|
|
$this->packagesFill($this->readFile($path)); |
109
|
|
|
|
110
|
|
|
$resultList = $this->sort(); |
111
|
|
|
|
112
|
|
|
foreach ($resultList as $package => $rating) { |
113
|
|
|
$required = $this->getRequiredList($package); |
114
|
|
|
$packages[$package] = $this->packagesListExtra[$package]; |
115
|
|
|
$packages[$package]['required'] = $required; |
116
|
|
|
$packages[$package]['composerName'] =$package; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Provide creating sorting list |
122
|
|
|
* @return array list of sorted packages |
123
|
|
|
*/ |
124
|
|
|
public function sort() |
125
|
|
|
{ |
126
|
|
|
$list = array(); |
127
|
|
|
foreach ($this->packagesList as $package => $requiredList) { |
128
|
|
|
if (!sizeof($list)||!isset($list[$package])) { |
129
|
|
|
$list[$package] = 1; |
130
|
|
|
} |
131
|
|
|
foreach ($requiredList as $requiredPackage) { |
132
|
|
|
if (isset($list[$requiredPackage])) { |
133
|
|
|
$packagePos = array_search($package, array_keys($list)); |
|
|
|
|
134
|
|
|
$requiredPackagePos = array_search($requiredPackage, array_keys($list)); |
135
|
|
|
if ($packagePos < $requiredPackagePos) { |
136
|
|
|
unset($list[$requiredPackage]); |
137
|
|
|
$list = $this->arrayInsertBefore($list, $package, $requiredPackage); |
138
|
|
|
} |
139
|
|
|
} else { |
140
|
|
|
$list = $this->arrayInsertBefore($list, $package, $requiredPackage); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
//$this->checkSort($list); |
146
|
|
|
|
147
|
|
|
return $list; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
*Check result of sorting |
152
|
|
|
* @param $list final list of packages |
153
|
|
|
* |
154
|
|
|
* @return bool result |
155
|
|
|
*/ |
156
|
|
|
public function checkSort($list) |
157
|
|
|
{ |
158
|
|
|
$status = true; |
159
|
|
|
foreach ($this->packagesList as $package => $requiredList) { |
160
|
|
|
foreach ($requiredList as $requiredPackage) { |
161
|
|
|
if (isset($list[$requiredPackage])) { |
162
|
|
|
$packagePos = array_search($package, array_keys($list)); |
163
|
|
|
$requiredPackagePos = array_search($requiredPackage, array_keys($list)); |
164
|
|
|
if ($packagePos < $requiredPackagePos) { |
165
|
|
|
trace('error pos - '.$packagePos.' < '.$requiredPackagePos); |
166
|
|
|
$status = false; |
167
|
|
|
} |
168
|
|
|
} else { |
169
|
|
|
$status = false; |
170
|
|
|
trace('error not isset!!!!! - '.$requiredPackage); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
return $status; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Create list of relevant packages |
181
|
|
|
* @param $packages Composer lock list of packages |
182
|
|
|
* @return array List of relevant packages |
183
|
|
|
*/ |
184
|
|
|
private function includeList($packages) |
185
|
|
|
{ |
186
|
|
|
$includePackages = array(); |
187
|
|
|
foreach ($packages as $package) { |
188
|
|
|
if (!$this->isIgnore($package)) { |
189
|
|
|
if ($this->isInclude($package)) { |
190
|
|
|
$includePackages[] = $package['name']; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
return $includePackages; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Is package include |
199
|
|
|
* @param $package Composer package |
200
|
|
|
* @return bool - is package include |
201
|
|
|
*/ |
202
|
|
|
private function isInclude($package) |
203
|
|
|
{ |
204
|
|
|
$include = true; |
205
|
|
|
if (sizeof($this->vendorsList)) { |
206
|
|
|
if (!isset($this->includeKey) || !isset($package['extra'][$this->includeKey])) { |
207
|
|
|
$packageName = $package['name']; |
208
|
|
|
$vendorName = substr($packageName, 0, strpos($packageName,"/")+1); |
209
|
|
|
$include = in_array($vendorName, $this->vendorsList); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
return $include; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Is package ignored |
217
|
|
|
* @param $package Composer package |
218
|
|
|
* @return bool - is package ignored |
219
|
|
|
*/ |
220
|
|
|
private function isIgnore($package) |
221
|
|
|
{ |
222
|
|
|
$isIgnore = false; |
223
|
|
|
if (in_array($package['name'], $this->ignorePackages)) { |
224
|
|
|
$isIgnore = true; |
225
|
|
|
} |
226
|
|
|
if (isset($this->ignoreKey)&&(isset($package['extra'][$this->ignoreKey]))) { |
227
|
|
|
$isIgnore = true; |
228
|
|
|
} |
229
|
|
|
return $isIgnore; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Fill list of relevant packages with there require packages |
234
|
|
|
* @param $packages Composer lock file object |
235
|
|
|
*/ |
236
|
|
|
private function packagesFill($packages) |
237
|
|
|
{ |
238
|
|
|
// Get included packages list |
239
|
|
|
$includePackages = $this->includeList($packages); |
240
|
|
|
|
241
|
|
|
// Create list of relevant packages with there require packages |
242
|
|
|
foreach ($packages as $package) { |
243
|
|
|
$requirement = $package['name']; |
244
|
|
|
if (in_array($requirement, $includePackages)) { |
245
|
|
|
$this->packagesList[$requirement] = array(); |
246
|
|
|
$this->packagesListExtra[$requirement] = isset($package['extra'])?$package['extra']:array(); |
247
|
|
|
if (isset($package['require'])) { |
248
|
|
|
$this->packagesList[$requirement] = array_intersect(array_keys($package['require']), $includePackages); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
private function readFile($path) |
255
|
|
|
{ |
256
|
|
|
$packages = array(); |
257
|
|
|
// If we have composer configuration file |
258
|
|
|
if (file_exists($path)) { |
259
|
|
|
// Read file into object |
260
|
|
|
$composerObject = json_decode(file_get_contents($path), true); |
261
|
|
|
|
262
|
|
|
// Gather all possible packages |
263
|
|
|
$packages = array_merge( |
264
|
|
|
array(), |
265
|
|
|
isset($composerObject['packages']) ? $composerObject['packages'] : array(), |
266
|
|
|
isset($composerObject['packages-dev']) ? $composerObject['packages-dev'] : array() |
267
|
|
|
); |
268
|
|
|
} |
269
|
|
|
return $packages; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Create list of of required packages |
274
|
|
|
* @param null $includeModule Dependent package |
275
|
|
|
* @param array $ignoreModules |
276
|
|
|
* |
277
|
|
|
* @return array required packages |
278
|
|
|
*/ |
279
|
|
|
private function getRequiredList($includeModule = null, $ignoreModules = array()) |
280
|
|
|
{ |
281
|
|
|
$list = isset($includeModule)?$this->packagesList[$includeModule]:$this->packagesList; |
282
|
|
|
$ignoreList = array(); |
283
|
|
|
foreach ($ignoreModules as $module) { |
284
|
|
|
$ignoreList[] = $module; |
285
|
|
|
if (is_array($this->packagesList[$module])) { |
286
|
|
|
$ignoreList = array_merge($ignoreList, $this->packagesList[$module]); |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
$result = array(); |
291
|
|
|
foreach ($list as $k=>$v) { |
|
|
|
|
292
|
|
|
$module = is_array($v)?$k:$v; |
293
|
|
View Code Duplication |
if (!in_array($module, $ignoreList)) { |
|
|
|
|
294
|
|
|
$result[] = $module; |
295
|
|
|
$moduleList = $this->getReqList($this->packagesList[$module]); |
296
|
|
|
$result = array_merge($result, $moduleList); |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
return array_values(array_unique($result)); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Recursive function that get list of required packages |
304
|
|
|
* @param $list List of packages |
305
|
|
|
* @param array $result |
306
|
|
|
* |
307
|
|
|
* @return array required packages |
308
|
|
|
*/ |
309
|
|
|
private function getReqList($list, $result = array()) { |
310
|
|
|
$return = array(); |
311
|
|
|
if (is_array($list)) { |
312
|
|
|
foreach ($list as $module) { |
313
|
|
View Code Duplication |
if (!in_array($module, $result)) { |
|
|
|
|
314
|
|
|
$getList = $this->getReqList($this->packagesList[$module], $return); |
315
|
|
|
$return[] = $module; |
316
|
|
|
$return = array_merge($return, $getList); |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
return $return; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* Clear object parameters |
325
|
|
|
*/ |
326
|
|
|
public function clear() |
327
|
|
|
{ |
328
|
|
|
$this->vendorsList = array(); |
329
|
|
|
$this->ignoreKey = null; |
330
|
|
|
$this->includeKey = null; |
331
|
|
|
$this->ignorePackages = array(); |
332
|
|
|
$this->packageRating = array(); |
|
|
|
|
333
|
|
|
$this->packagesList = array(); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Insert a key after a specific key in an array. If key doesn't exist, value is appended |
338
|
|
|
* to the end of the array. |
339
|
|
|
* |
340
|
|
|
* @param array $array |
341
|
|
|
* @param string $key |
342
|
|
|
* @param integer $newKey |
343
|
|
|
* |
344
|
|
|
* @return array |
345
|
|
|
*/ |
346
|
|
|
public function arrayInsertAfter( array $array, $key, $newKey ) { |
|
|
|
|
347
|
|
|
$keys = array_keys( $array ); |
348
|
|
|
$index = array_search( $key, $keys ); |
349
|
|
|
$pos = false === $index ? count( $array ) : $index + 1; |
350
|
|
|
return array_merge( array_slice( $array, 0, $pos ), array($newKey=>1), array_slice( $array, $pos ) ); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Insert a key before a specific key in an array. If key doesn't exist, value is prepended |
355
|
|
|
* to the beginning of the array. |
356
|
|
|
* |
357
|
|
|
* @param array $array |
358
|
|
|
* @param string $key |
359
|
|
|
* @param integer $newKey |
360
|
|
|
* |
361
|
|
|
* @return array |
362
|
|
|
*/ |
363
|
|
|
public function arrayInsertBefore( array $array, $key, $newKey ) { |
|
|
|
|
364
|
|
|
$keys = array_keys( $array ); |
365
|
|
|
$pos = (int) array_search( $key, $keys ); |
366
|
|
|
return array_merge( array_slice( $array, 0, $pos ), array($newKey=>1), array_slice( $array, $pos ) ); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
} |
370
|
|
|
|