-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfig.hh
141 lines (122 loc) · 3.29 KB
/
Config.hh
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?hh // strict
/**
* @copyright 2010-2015, The Titon Project
* @license http://opensource.org/licenses/bsd-license.php
* @link http://titon.io
*/
namespace Titon\Utility;
use Titon\Io\Reader;
/**
* Stores the current configuration options for the application.
* Configuration can be loaded from multiple sources including environment, bootstraps and internal system classes.
* Various readers can be used to import specific configuration files.
*
* @package Titon\Utility
*/
class Config {
/**
* Current loaded configuration.
*
* @var \Titon\Utility\ConfigMap
*/
protected static ConfigMap $config = Map {};
/**
* Add a value to a key. If the value is not a vector, make it one.
*
* @param string $key
* @param mixed $value
*/
public static function add(string $key, mixed $value): void {
$data = Col::toVector(static::get($key, Vector {}));
$data[] = $value;
static::set($key, $data);
}
/**
* Return all configuration.
*
* @return \Titon\Utility\ConfigMap
*/
public static function all(): ConfigMap {
return static::$config;
}
/**
* Get the currently defined encoding for the application.
*
* @return string
*/
public static function encoding(): string {
return (string) static::get('app.encoding') ?: 'UTF-8';
}
/**
* Flush configuration by removing all settings.
*/
public static function flush(): void {
static::$config->clear();
}
/**
* Grab a value from the current configuration.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public static function get(string $key, mixed $default = null): mixed {
$value = Col::get(static::$config, $key);
if ($value === null) {
return $default;
}
return $value;
}
/**
* Checks to see if a key exists within the current configuration.
*
* @param string $key
* @return bool
*/
public static function has(string $key): bool {
return Col::has(static::$config, $key);
}
/**
* Loads a user created file into the configuration class.
* Uses the defined reader to parse the file.
*
* @param string $key
* @param \Titon\Io\Reader $reader
*/
public static function load(string $key, Reader $reader): void {
static::$config[$key] = $reader->readResource();
}
/**
* Grabs the defined project name.
*
* @return string
*/
public static function name(): string {
return (string) static::get('app.name', '');
}
/**
* Remove a value from the config.
*
* @param string $key
*/
public static function remove(string $key): void {
Col::remove(static::$config, $key);
}
/**
* Get the currently defined salt for the application.
*
* @return string
*/
public static function salt(): string {
return (string) static::get('app.salt', '');
}
/**
* Add values to the current loaded configuration.
*
* @param string $key
* @param mixed $value
*/
public static function set(string $key, mixed $value): void {
Col::set(static::$config, $key, $value);
}
}