1
|
|
|
<?php |
2
|
|
|
header('HTTP/1.0 404 Not Found'); |
3
|
|
|
|
4
|
|
|
class ComposerInfo |
5
|
|
|
{ |
6
|
|
|
public static function getComposerInfo(XoopsTpl $xoopsTpl) |
7
|
|
|
{ |
8
|
|
|
global $xoopsLogger; |
9
|
|
|
|
10
|
|
|
try { |
11
|
|
|
// Define the path to the composer.lock file |
12
|
|
|
$composerLockPath = XOOPS_ROOT_PATH . '/class/libraries/composer'; |
13
|
|
|
// Get the packages data from composer.lock file |
14
|
|
|
$packages = self::getComposerData($composerLockPath); |
15
|
|
|
// Extract package name and version |
16
|
|
|
$composerPackages = self::extractPackages($packages); |
17
|
|
|
// Assign the $composerPackages array to the Smarty template |
18
|
|
|
$xoopsTpl->assign('composerPackages', $composerPackages); |
19
|
|
|
} catch (Exception $e) { |
20
|
|
|
// Handle any exception and log the error using XOOPS Logger |
21
|
|
|
$xoopsLogger->handleError(E_USER_ERROR, $e->getMessage(), __FILE__, __LINE__); |
22
|
|
|
echo "An error occurred. Please try again later."; |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
// Function to read and parse composer.lock file |
27
|
|
|
private static function getComposerData(string $composerLockPath): array |
28
|
|
|
{ |
29
|
|
|
$composerkLock = $composerLockPath . '.lock'; |
30
|
|
|
if (!file_exists($composerkLock)) { |
31
|
|
|
$composerLockPathDist = $composerLockPath . '.dist.lock'; |
32
|
|
|
if (!file_exists($composerLockPathDist)) { |
33
|
|
|
throw new InvalidArgumentException("File not found at: " . $composerLockPath); |
34
|
|
|
} |
35
|
|
|
$composerLockPath = $composerLockPathDist; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$composerLockData = file_get_contents($composerLockPath); |
39
|
|
|
|
40
|
|
|
if ($composerLockData === false) { |
41
|
|
|
throw new RuntimeException("Failed to read the file: " . $composerLockPath); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$composerData = json_decode($composerLockData, true); |
45
|
|
|
|
46
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) { |
47
|
|
|
throw new JsonException("Failed to decode JSON data: " . json_last_error_msg()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $composerData['packages'] ?? []; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
|
55
|
|
|
// Function to extract package name and version (using array_map for optimization) |
56
|
|
|
private static function extractPackages(array $packages): array |
57
|
|
|
{ |
58
|
|
|
return array_map( |
59
|
|
|
static fn($package) => [ |
60
|
|
|
'name' => $package['name'], |
61
|
|
|
'version' => $package['version'] |
62
|
|
|
], $packages |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
|
68
|
|
|
|
69
|
|
|
|
70
|
|
|
|
71
|
|
|
|
72
|
|
|
|
73
|
|
|
|
74
|
|
|
|
75
|
|
|
|
76
|
|
|
|
77
|
|
|
|
78
|
|
|
|
79
|
|
|
|
80
|
|
|
|
81
|
|
|
|
82
|
|
|
|
83
|
|
|
|
84
|
|
|
|
85
|
|
|
} |
86
|
|
|
|