|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 2.9.7 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Config; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Config\Exceptions\ConfigException; |
|
18
|
|
|
use Quantum\Contracts\StorageInterface; |
|
19
|
|
|
use Quantum\Di\Exceptions\DiException; |
|
20
|
|
|
use Dflydev\DotAccessData\Data; |
|
21
|
|
|
use Quantum\Loader\Loader; |
|
22
|
|
|
use Quantum\Loader\Setup; |
|
23
|
|
|
use ReflectionException; |
|
24
|
|
|
use Quantum\Di\Di; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Class Config |
|
28
|
|
|
* @package Quantum\Libraries\Config |
|
29
|
|
|
*/ |
|
30
|
|
|
class Config implements StorageInterface |
|
31
|
|
|
{ |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Configs |
|
35
|
|
|
* @var Data|null |
|
36
|
|
|
*/ |
|
37
|
|
|
private static $configs = null; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Instance of Config |
|
41
|
|
|
* @var Config |
|
42
|
|
|
*/ |
|
43
|
|
|
private static $instance = null; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* GetInstance |
|
47
|
|
|
* @return Config |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function getInstance(): Config |
|
50
|
|
|
{ |
|
51
|
|
|
if (self::$instance === null) { |
|
52
|
|
|
self::$instance = new self(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
return self::$instance; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Loads configuration |
|
60
|
|
|
* @param Setup $setup |
|
61
|
|
|
* @throws ConfigException |
|
62
|
|
|
* @throws DiException |
|
63
|
|
|
* @throws ReflectionException |
|
64
|
|
|
*/ |
|
65
|
|
|
public function load(Setup $setup) |
|
66
|
|
|
{ |
|
67
|
|
|
if (self::$configs !== null) { |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
self::$configs = new Data(Di::get(Loader::class)->setup($setup)->load()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Imports new config file |
|
76
|
|
|
* @param Setup $setup |
|
77
|
|
|
* @throws ConfigException |
|
78
|
|
|
* @throws DiException |
|
79
|
|
|
* @throws ReflectionException |
|
80
|
|
|
*/ |
|
81
|
|
|
public function import(Setup $setup) |
|
82
|
|
|
{ |
|
83
|
|
|
$fileName = $setup->getFilename(); |
|
84
|
|
|
|
|
85
|
|
|
if ($fileName && $this->has($fileName)) { |
|
86
|
|
|
throw ConfigException::configCollision($fileName); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if (!self::$configs) { |
|
90
|
|
|
self::$configs = new Data([$fileName => Di::get(Loader::class)->setup($setup)->load()]); |
|
91
|
|
|
} else { |
|
92
|
|
|
self::$configs->import([$fileName => Di::get(Loader::class)->setup($setup)->load()]); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Gets the config item by given key |
|
98
|
|
|
* @param string $key |
|
99
|
|
|
* @param mixed $default |
|
100
|
|
|
* @return mixed|null |
|
101
|
|
|
*/ |
|
102
|
|
|
public function get(string $key, $default = null) |
|
103
|
|
|
{ |
|
104
|
|
|
if (self::$configs && self::$configs->has($key)) { |
|
105
|
|
|
return self::$configs->get($key); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return $default; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get all configs |
|
113
|
|
|
* @return Data|null |
|
114
|
|
|
*/ |
|
115
|
|
|
public function all(): ?Data |
|
116
|
|
|
{ |
|
117
|
|
|
return self::$configs; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Checks config data |
|
122
|
|
|
* @param string $key |
|
123
|
|
|
* @return bool |
|
124
|
|
|
*/ |
|
125
|
|
|
public function has(string $key): bool |
|
126
|
|
|
{ |
|
127
|
|
|
return self::$configs && !empty(self::$configs->has($key)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Sets new value |
|
132
|
|
|
* @param string $key |
|
133
|
|
|
* @param mixed $value |
|
134
|
|
|
* @return void |
|
135
|
|
|
*/ |
|
136
|
|
|
public function set(string $key, $value) |
|
137
|
|
|
{ |
|
138
|
|
|
if (!self::$configs) { |
|
139
|
|
|
self::$configs = new Data([$key => $value]); |
|
140
|
|
|
} else { |
|
141
|
|
|
self::$configs->set($key, $value); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Removes the data from config |
|
147
|
|
|
* @param string $key |
|
148
|
|
|
*/ |
|
149
|
|
|
public function delete(string $key) |
|
150
|
|
|
{ |
|
151
|
|
|
self::$configs && self::$configs->remove($key); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Deletes whole config data |
|
156
|
|
|
*/ |
|
157
|
|
|
public function flush() |
|
158
|
|
|
{ |
|
159
|
|
|
self::$configs = null; |
|
160
|
|
|
} |
|
161
|
|
|
} |