|
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.0.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Config; |
|
16
|
|
|
|
|
17
|
|
|
use Quantum\Exceptions\ConfigException; |
|
18
|
|
|
use Quantum\Contracts\StorageInterface; |
|
19
|
|
|
use Dflydev\DotAccessData\Data; |
|
20
|
|
|
use Quantum\Loader\Loader; |
|
21
|
|
|
use Quantum\Loader\Setup; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class Config |
|
25
|
|
|
* @package Quantum\Libraries\Config |
|
26
|
|
|
*/ |
|
27
|
|
|
class Config implements StorageInterface |
|
28
|
|
|
{ |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Configs |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
private static $configs = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Instance of Config |
|
38
|
|
|
* @var Config |
|
39
|
|
|
*/ |
|
40
|
|
|
private static $configInstance = null; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* GetInstance |
|
44
|
|
|
* @return Config |
|
45
|
|
|
*/ |
|
46
|
|
|
public static function getInstance() |
|
47
|
|
|
{ |
|
48
|
|
|
if (self::$configInstance === null) { |
|
49
|
|
|
self::$configInstance = new self(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return self::$configInstance; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Loads configuration |
|
57
|
|
|
* @param Loader $loader |
|
58
|
|
|
* @throws \Quantum\Exceptions\LoaderException |
|
59
|
|
|
*/ |
|
60
|
|
|
public function load(Loader $loader) |
|
61
|
|
|
{ |
|
62
|
|
|
if (empty(self::$configs)) { |
|
63
|
|
|
self::$configs = $loader->setup(new Setup('config', 'config', true))->load(); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Imports new config file |
|
69
|
|
|
* @param Loader $loader |
|
70
|
|
|
* @param string $fileName |
|
71
|
|
|
* @throws ConfigException |
|
72
|
|
|
* @throws \Quantum\Exceptions\LoaderException |
|
73
|
|
|
*/ |
|
74
|
|
|
public function import(Loader $loader, $fileName) |
|
75
|
|
|
{ |
|
76
|
|
|
if ($this->has($fileName)) { |
|
77
|
|
|
throw new ConfigException(_message(ConfigException::CONFIG_COLLISION, $fileName)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
self::$configs[$fileName] = $loader->load(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Gets the config item by given key |
|
85
|
|
|
* @param string $key |
|
86
|
|
|
* @param mixed $default |
|
87
|
|
|
* @return mixed|null |
|
88
|
|
|
*/ |
|
89
|
|
|
public function get($key, $default = null) |
|
90
|
|
|
{ |
|
91
|
|
|
$data = new Data(self::$configs); |
|
92
|
|
|
|
|
93
|
|
|
if ($this->has($key)) { |
|
94
|
|
|
return $data->get($key); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
return $default; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Get all configs |
|
102
|
|
|
* @return array |
|
103
|
|
|
*/ |
|
104
|
|
|
public function all(): array |
|
105
|
|
|
{ |
|
106
|
|
|
return self::$configs; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Checks config data |
|
111
|
|
|
* @param string $key |
|
112
|
|
|
* @return bool |
|
113
|
|
|
*/ |
|
114
|
|
|
public function has($key) |
|
115
|
|
|
{ |
|
116
|
|
|
$data = new Data(self::$configs); |
|
117
|
|
|
|
|
118
|
|
|
return ($data->has($key)); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Sets new value |
|
123
|
|
|
* @param string $key |
|
124
|
|
|
* @param mixed $value |
|
125
|
|
|
* @return void |
|
126
|
|
|
*/ |
|
127
|
|
|
public function set($key, $value) |
|
128
|
|
|
{ |
|
129
|
|
|
$data = new Data(self::$configs); |
|
130
|
|
|
|
|
131
|
|
|
$data->set($key, $value); |
|
132
|
|
|
self::$configs = $data->export(); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Removes the data from config |
|
137
|
|
|
* @param $key |
|
138
|
|
|
*/ |
|
139
|
|
|
public function delete($key) |
|
140
|
|
|
{ |
|
141
|
|
|
$data = new Data(self::$configs); |
|
142
|
|
|
|
|
143
|
|
|
$data->remove($key); |
|
144
|
|
|
self::$configs = $data->export(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Deletes whole config data |
|
149
|
|
|
* @return void |
|
150
|
|
|
*/ |
|
151
|
|
|
public function flush() |
|
152
|
|
|
{ |
|
153
|
|
|
self::$configs = []; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
|