1 | <?php |
||
2 | /* |
||
3 | * File: Config.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 Config |
||
17 | * |
||
18 | * @package Webklex\ComposerInfo |
||
19 | */ |
||
20 | class Config { |
||
21 | |||
22 | /** |
||
23 | * All library config |
||
24 | * |
||
25 | * @var array $config |
||
26 | */ |
||
27 | public static $config = []; |
||
28 | |||
29 | |||
30 | /** |
||
31 | * Config constructor. |
||
32 | * @param array|string $config |
||
33 | */ |
||
34 | public function __construct($config = []) { |
||
35 | $this->setConfig($config); |
||
36 | } |
||
37 | |||
38 | /** |
||
39 | * Get a dotted config parameter |
||
40 | * @param string $key |
||
41 | * @param null $default |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
42 | * |
||
43 | * @return mixed|null |
||
44 | */ |
||
45 | public static function get($key, $default = null) { |
||
46 | $parts = explode('.', $key); |
||
47 | $value = null; |
||
48 | foreach($parts as $part) { |
||
49 | if($value === null) { |
||
50 | if(isset(self::$config[$part])) { |
||
51 | $value = self::$config[$part]; |
||
52 | }else{ |
||
53 | break; |
||
54 | } |
||
55 | }else{ |
||
56 | if(isset($value[$part])) { |
||
57 | $value = $value[$part]; |
||
58 | }else{ |
||
59 | break; |
||
60 | } |
||
61 | } |
||
62 | } |
||
63 | |||
64 | return $value === null ? $default : $value; |
||
65 | } |
||
66 | |||
67 | |||
68 | /** |
||
69 | * Merge the vendor settings with the local config |
||
70 | * @param array|string $config |
||
71 | * |
||
72 | * @return $this |
||
73 | */ |
||
74 | public function setConfig($config) { |
||
75 | |||
76 | if(is_array($config) === false) { |
||
77 | $config = require $config; |
||
78 | } |
||
79 | |||
80 | $vendor_config = require __DIR__.'/config/composer-info.php'; |
||
81 | self::$config = $this->array_merge_recursive_distinct($vendor_config, $config); |
||
82 | |||
83 | return $this; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * Marge arrays recursively and distinct |
||
88 | * |
||
89 | * Merges any number of arrays / parameters recursively, replacing |
||
90 | * entries with string keys with values from latter arrays. |
||
91 | * If the entry or the next value to be assigned is an array, then it |
||
92 | * automatically treats both arguments as an array. |
||
93 | * Numeric entries are appended, not replaced, but only if they are |
||
94 | * unique |
||
95 | * |
||
96 | * @param array $array1 Initial array to merge. |
||
97 | * @param array ... Variable list of arrays to recursively merge. |
||
98 | * |
||
99 | * @return array|mixed |
||
100 | * |
||
101 | * @link http://www.php.net/manual/en/function.array-merge-recursive.php#96201 |
||
102 | * @author Mark Roduner <[email protected]> |
||
103 | */ |
||
104 | private function array_merge_recursive_distinct() { |
||
105 | |||
106 | $arrays = func_get_args(); |
||
107 | $base = array_shift($arrays); |
||
108 | |||
109 | if(!is_array($base)) $base = empty($base) ? array() : array($base); |
||
110 | |||
111 | foreach($arrays as $append) { |
||
112 | |||
113 | if(!is_array($append)) $append = array($append); |
||
114 | |||
115 | foreach($append as $key => $value) { |
||
116 | |||
117 | if(!array_key_exists($key, $base) and !is_numeric($key)) { |
||
118 | $base[$key] = $append[$key]; |
||
119 | continue; |
||
120 | } |
||
121 | |||
122 | if(is_array($value) or is_array($base[$key])) { |
||
123 | $base[$key] = $this->array_merge_recursive_distinct($base[$key], $append[$key]); |
||
124 | } else if(is_numeric($key)) { |
||
125 | if(!in_array($value, $base)) $base[] = $value; |
||
126 | } else { |
||
127 | $base[$key] = $value; |
||
128 | } |
||
129 | |||
130 | } |
||
131 | |||
132 | } |
||
133 | |||
134 | return $base; |
||
135 | } |
||
136 | } |