/
ComposerInfo.php
97 lines (86 loc) · 1.97 KB
/
ComposerInfo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/*
* File: ComposerInfo.php
* Category: -
* Author: M.Goldenbaum
* Created: 13.10.20 01:16
* Updated: -
*
* Description:
* -
*/
namespace Webklex\ComposerInfo;
/**
* Class ComposerInfo
*
* @package Webklex\ComposerInfo
*/
class ComposerInfo {
/**
* All library config
*
* @var Config $config
*/
public static $config;
/** @var array $packages */
public static $packages = [];
/**
* ComposerInfo constructor.
* @param array|string $config
*/
public function __construct($config = []) {
$this->setConfig($config);
$this->load(self::$config->get("location"));
}
/**
* Get a specific package
* @param string $name
*
* @return mixed|null
*/
public static function getPackage($name) {
if (isset(self::$packages[$name])) {
return self::$packages[$name];
}
return null;
}
/**
* Get a specific package version
* @param string $name
*
* @return mixed|null
*/
public static function getPackageVersion($name) {
if (($package = self::getPackage($name)) !== null) {
if (isset($package["version"])) {
return $package["version"];
}
}
return null;
}
/**
* @param $location
*/
public function load($location) {
if (file_exists($location)) {
$content = file_get_contents($location);
$data = json_decode($content, true);
if (isset($data["packages"])) {
foreach($data["packages"] as $package) {
if (isset($package["name"])) {
self::$packages[$package["name"]] = $package;
}
}
}
}
}
/**
* @param array|string $config
*
* @return $this
*/
public function setConfig($config) {
self::$config = new Config($config);
return $this;
}
}