ComposerInfo::setConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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