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
|
|
|
* Decision between Main or Development packages |
41
|
|
|
* |
42
|
|
|
* @param boolean $devInstead |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
private function decisionPackageOrPackageDev($devInstead) |
46
|
|
|
{ |
47
|
|
|
$sReturn = 'packages'; |
48
|
|
|
if ($devInstead) { |
49
|
|
|
$sReturn = 'packages-dev'; |
50
|
|
|
} |
51
|
|
|
return $sReturn; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Exposes few Environment details |
56
|
|
|
* |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
protected function exposeEnvironmentDetails() |
60
|
|
|
{ |
61
|
|
|
$knownValues = [ |
62
|
|
|
'AMD64' => 'x64 (64 bit)', |
63
|
|
|
'i386' => 'x86 (32 bit)', |
64
|
|
|
'i586' => 'x86 (32 bit)', |
65
|
|
|
]; |
66
|
|
|
return [ |
67
|
|
|
'Host Name' => php_uname('n'), |
68
|
|
|
'Machine Type' => php_uname('m'), |
69
|
|
|
'Operating System Architecture' => $knownValues[php_uname('m')], |
70
|
|
|
'Operating System Name' => php_uname('s'), |
71
|
|
|
'Operating System Version' => php_uname('r') . ' ' . php_uname('v'), |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
protected function exposePhpDetails($skipAging = false) |
80
|
|
|
{ |
81
|
|
|
$aReturn = [ |
82
|
|
|
'Aging' => $this->getPkgAging($this->getFileModifiedTimestampOfFile(PHP_BINARY, 'PHPtime')), |
83
|
|
|
'Architecture' => (PHP_INT_SIZE === 4 ? 'x86 (32 bit)' : 'x64 (64 bit)'), |
84
|
|
|
'Description' => 'PHP is a popular general-purpose scripting language' |
85
|
|
|
. ' that is especially suited to web development', |
86
|
|
|
'Homepage' => 'https://secure.php.net/', |
87
|
|
|
'License' => 'PHP License v3.01', |
88
|
|
|
'Package Name' => 'ZendEngine/PHP', |
89
|
|
|
'Product' => 'PHP', |
90
|
|
|
'Time' => $this->getFileModifiedTimestampOfFile(PHP_BINARY, 'l, d F Y H:i:s'), |
91
|
|
|
'Time as PHP no.' => $this->getFileModifiedTimestampOfFile(PHP_BINARY, 'PHPtime'), |
92
|
|
|
'Type' => 'scripting language', |
93
|
|
|
'Url' => 'https://github.com/php/php-src', |
94
|
|
|
'Vendor' => 'The PHP Group', |
95
|
|
|
'Version' => PHP_VERSION, |
96
|
|
|
'Version no.' => PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION, |
97
|
|
|
]; |
98
|
|
|
if ($skipAging) { |
99
|
|
|
unset($aReturn['Aging']); |
100
|
|
|
} |
101
|
|
|
return $aReturn; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Returns Modified date and time of a given file |
106
|
|
|
* |
107
|
|
|
* @param string $fileName |
108
|
|
|
* @param string $format |
109
|
|
|
* @param boolean $resultInUtc |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
protected function getFileModifiedTimestampOfFile($fileName, $format = 'Y-m-d H:i:s', $resultInUtc = false) |
113
|
|
|
{ |
114
|
|
|
if (!file_exists($fileName)) { |
115
|
|
|
return ['error' => $fileName . ' was not found']; |
116
|
|
|
} |
117
|
|
|
$info = new \SplFileInfo($fileName); |
118
|
|
|
if ($format === 'PHPtime') { |
119
|
|
|
return $info->getMTime(); |
120
|
|
|
} |
121
|
|
|
$sReturn = date($format, $info->getMTime()); |
122
|
|
|
if ($resultInUtc) { |
123
|
|
|
$sReturn = gmdate($format, $info->getMTime()); |
124
|
|
|
} |
125
|
|
|
return $sReturn; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns a complete list of packages and respective details from a composer.lock file |
130
|
|
|
* |
131
|
|
|
* @param string $fileToRead |
132
|
|
|
* @return array |
133
|
|
|
*/ |
134
|
|
|
protected function getPackageDetailsFromGivenComposerLockFile($fileToRead, $devInstead = false, $skipAging = false) |
135
|
|
|
{ |
136
|
|
|
if (!file_exists($fileToRead)) { |
137
|
|
|
return ['error' => $fileToRead . ' was not found']; |
138
|
|
|
} |
139
|
|
|
$dNA = '---'; |
140
|
|
|
$alnfo = []; |
141
|
|
|
$packages = $this->getPkgFileInListOfPackageArrayOut($fileToRead); |
142
|
|
|
foreach ($packages[$this->decisionPackageOrPackageDev($devInstead)] as $value) { |
143
|
|
|
$atr = $this->getPkgOptAtributeAll($value, $dNA); |
144
|
|
|
$basic = $this->getPkgBasicInfo($value, $dNA); |
145
|
|
|
$vrs = $this->getPkgVersion($value, $dNA); |
146
|
|
|
$alnfo[$value['name']] = array_merge($atr, $basic, $vrs, $this->getPkgTiming($value, $dNA)); |
147
|
|
|
if ($skipAging) { |
148
|
|
|
unset($alnfo[$value['name']]['Aging']); |
149
|
|
|
} |
150
|
|
|
ksort($alnfo[$value['name']]); |
151
|
|
|
} |
152
|
|
|
ksort($alnfo); |
153
|
|
|
return $alnfo; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
private function getPkgAging($timePkg) |
157
|
|
|
{ |
158
|
|
|
$dateTimeToday = new \DateTime(date('Y-m-d', strtotime('today'))); |
159
|
|
|
$dateTime = new \DateTime(date('Y-m-d', strtotime($timePkg))); |
160
|
|
|
$interval = $dateTimeToday->diff($dateTime); |
161
|
|
|
return $interval->format('%a days ago'); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
private function getPkgBasicInfo($value, $defaultNA) |
165
|
|
|
{ |
166
|
|
|
return [ |
167
|
|
|
'License' => (isset($value['license']) ? $this->getPkgLcns($value['license']) : $defaultNA), |
168
|
|
|
'Package Name' => $value['name'], |
169
|
|
|
'PHP required' => (isset($value['require']['php']) ? $value['require']['php'] : $defaultNA), |
170
|
|
|
'Product' => explode('/', $value['name'])[1], |
171
|
|
|
'Vendor' => explode('/', $value['name'])[0], |
172
|
|
|
]; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
private function getPkgFileInListOfPackageArrayOut($fileToRead) |
176
|
|
|
{ |
177
|
|
|
$handle = fopen($fileToRead, 'r'); |
178
|
|
|
$fileContents = fread($handle, filesize($fileToRead)); |
179
|
|
|
fclose($handle); |
180
|
|
|
return json_decode($fileContents, true); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
private function getPkgLcns($license) |
184
|
|
|
{ |
185
|
|
|
$lcns = $license; |
186
|
|
|
if (is_array($license)) { |
187
|
|
|
$lcns = implode(', ', $license); |
188
|
|
|
} |
189
|
|
|
return $lcns; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
private function getPkgOptAtributeAll($value, $defaultNA) |
193
|
|
|
{ |
194
|
|
|
$attr = ['description', 'homepage', 'type', 'url', 'version']; |
195
|
|
|
$aReturn = []; |
196
|
|
|
foreach ($attr as $valueA) { |
197
|
|
|
$aReturn[ucwords($valueA)] = $defaultNA; |
198
|
|
|
if (array_key_exists($valueA, $value)) { |
199
|
|
|
$aReturn[ucwords($valueA)] = $value[$valueA]; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
return $aReturn; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
private function getPkgTiming($value, $defaultNA) |
206
|
|
|
{ |
207
|
|
|
if (isset($value['time'])) { |
208
|
|
|
return [ |
209
|
|
|
'Aging' => $this->getPkgAging($value['time']), |
210
|
|
|
'Time' => date('l, d F Y H:i:s', strtotime($value['time'])), |
211
|
|
|
'Time as PHP no.' => strtotime($value['time']), |
212
|
|
|
]; |
213
|
|
|
} |
214
|
|
|
return ['Aging' => $defaultNA, 'Time' => $defaultNA, 'Time as PHP no.' => $defaultNA]; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
private function getPkgVerNo($version) |
218
|
|
|
{ |
219
|
|
|
$vrs = $version; |
220
|
|
|
if (substr($version, 0, 1) == 'v') { |
221
|
|
|
$vrs = substr($version, 1, strlen($version) - 1); |
222
|
|
|
} |
223
|
|
|
if (strpos($vrs, '-') !== false) { |
224
|
|
|
$vrs = substr($vrs, 0, strpos($vrs, '-')); |
225
|
|
|
} |
226
|
|
|
return $vrs; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
private function getPkgVersion($value, $defaultNA) |
230
|
|
|
{ |
231
|
|
|
if (isset($value['version'])) { |
232
|
|
|
return [ |
233
|
|
|
'Notification URL' => $value['notification-url'], |
234
|
|
|
'Version no.' => $this->getPkgVerNo($value['version']), |
235
|
|
|
]; |
236
|
|
|
} |
237
|
|
|
return ['Notification URL' => $defaultNA, 'Version no.' => $defaultNA]; |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|