1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* The MIT License (MIT) |
6
|
|
|
* |
7
|
|
|
* Copyright (c) 2015 Daniel Popiniuc |
8
|
|
|
* |
9
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
10
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
11
|
|
|
* in the Software without restriction, including without limitation the rights |
12
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
14
|
|
|
* furnished to do so, subject to the following conditions: |
15
|
|
|
* |
16
|
|
|
* The above copyright notice and this permission notice shall be included in all |
17
|
|
|
* copies or substantial portions of the Software. |
18
|
|
|
* |
19
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25
|
|
|
* SOFTWARE. |
26
|
|
|
* |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
namespace danielgp\composer_packages_listing; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* usefull functions to get quick results |
33
|
|
|
* |
34
|
|
|
* @author Daniel Popiniuc |
35
|
|
|
*/ |
36
|
|
|
trait ComposerPackagesListing |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns a complete list of packages and respective details from a composer.lock file |
41
|
|
|
* |
42
|
|
|
* @param string $fileToRead |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
protected function getPackageDetailsFromGivenComposerLockFile($fileToRead) |
46
|
|
|
{ |
47
|
|
|
if (!file_exists($fileToRead)) { |
48
|
|
|
return ['error' => $fileToRead . ' was not found']; |
49
|
|
|
} |
50
|
|
|
$dNA = '---'; |
51
|
|
|
$alnfo = []; |
52
|
|
|
$packages = $this->getPkgFileInListOfPackageArrayOut($fileToRead); |
53
|
|
|
foreach ($packages['packages'] as $value) { |
54
|
|
|
$atr = $this->getPkgOptAtributeAll($value, $dNA); |
55
|
|
|
$basic = $this->getPkgBasicInfo($value, $dNA); |
56
|
|
|
$vrs = $this->getPkgVersion($value, $dNA); |
57
|
|
|
$alnfo[$value['name']] = array_merge($atr, $basic, $vrs, $this->getPkgTiming($value, $dNA)); |
58
|
|
|
ksort($alnfo[$value['name']]); |
59
|
|
|
} |
60
|
|
|
ksort($alnfo); |
61
|
|
|
return $alnfo; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function getPkgAging($timePkg) |
65
|
|
|
{ |
66
|
|
|
$dateTimeToday = new \DateTime(date('Y-m-d', strtotime('today'))); |
67
|
|
|
$dateTime = new \DateTime(date('Y-m-d', strtotime($timePkg))); |
68
|
|
|
$interval = $dateTimeToday->diff($dateTime); |
69
|
|
|
return $interval->format('%a days ago'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function getPkgBasicInfo($value, $defaultNA) |
73
|
|
|
{ |
74
|
|
|
return [ |
75
|
|
|
'License' => (isset($value['license']) ? $this->getPkgLcns($value['license']) : $defaultNA), |
76
|
|
|
'Package Name' => $value['name'], |
77
|
|
|
'PHP required' => (isset($value['require']['php']) ? $value['require']['php'] : $defaultNA), |
78
|
|
|
'Product' => explode('/', $value['name'])[1], |
79
|
|
|
'Vendor' => explode('/', $value['name'])[0], |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function getPkgFileInListOfPackageArrayOut($fileToRead) |
84
|
|
|
{ |
85
|
|
|
$handle = fopen($fileToRead, 'r'); |
86
|
|
|
$fileContents = fread($handle, filesize($fileToRead)); |
87
|
|
|
fclose($handle); |
88
|
|
|
return json_decode($fileContents, true); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function getPkgLcns($license) |
92
|
|
|
{ |
93
|
|
|
$lcns = $license; |
94
|
|
|
if (is_array($license)) { |
95
|
|
|
$lcns = implode(', ', $license); |
96
|
|
|
} |
97
|
|
|
return $lcns; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function getPkgOptAtributeAll($value, $defaultNA) |
101
|
|
|
{ |
102
|
|
|
$attr = ['description', 'homepage', 'type', 'url', 'version']; |
103
|
|
|
$aReturn = []; |
104
|
|
|
foreach ($attr as $valueA) { |
105
|
|
|
$aReturn[ucwords($valueA)] = $defaultNA; |
106
|
|
|
if (array_key_exists($valueA, $value)) { |
107
|
|
|
$aReturn[ucwords($valueA)] = $value[$valueA]; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
return $aReturn; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
private function getPkgTiming($value, $defaultNA) |
114
|
|
|
{ |
115
|
|
|
if (isset($value['time'])) { |
116
|
|
|
return [ |
117
|
|
|
'Aging' => $this->getPkgAging($value['time']), |
118
|
|
|
'Time' => date('l, d F Y H:i:s', strtotime($value['time'])), |
119
|
|
|
'Time as PHP no.' => strtotime($value['time']), |
120
|
|
|
]; |
121
|
|
|
} |
122
|
|
|
return ['Aging' => $defaultNA, 'Time' => $defaultNA, 'Time as PHP no.' => $defaultNA]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function getPkgVerNo($version) |
126
|
|
|
{ |
127
|
|
|
$vrs = $version; |
128
|
|
|
if (substr($version, 0, 1) == 'v') { |
129
|
|
|
$vrs = substr($version, 1, strlen($version) - 1); |
130
|
|
|
} |
131
|
|
|
if (strpos($vrs, '-') !== false) { |
132
|
|
|
$vrs = substr($vrs, 0, strpos($vrs, '-')); |
133
|
|
|
} |
134
|
|
|
return $vrs; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
private function getPkgVersion($value, $defaultNA) |
138
|
|
|
{ |
139
|
|
|
if (isset($value['version'])) { |
140
|
|
|
return [ |
141
|
|
|
'Notification URL' => $value['notification-url'], |
142
|
|
|
'Version no.' => $this->getPkgVerNo($value['version']), |
143
|
|
|
]; |
144
|
|
|
} |
145
|
|
|
return ['Notification URL' => $defaultNA, 'Version no.' => $defaultNA]; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|