Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfig.php
More file actions
Latest commit
216 lines (196 loc) · 6.81 KB
/
Copy pathConfig.php
File metadata and controls
216 lines (196 loc) · 6.81 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?phpdeclare(strict_types=1);
/*
* This file is part of the Koded package.
*
* (c) Mihail Binev <mihail@kodeart.com>
*
* Please view the LICENSE distributed with this source code
* for the full copyright and license information.
*/
namespaceKoded\Stdlib;
useException;
usefunctioncall_user_func;
usefunctionclass_exists;
usefunctioncurrent;
usefunctionerror_get_last;
usefunctionerror_log;
usefunctionfile_get_contents;
usefunctiongetcwd;
usefunctiongetenv;
usefunctionimplode;
usefunctionis_a;
usefunctionis_string;
usefunctioniterator_to_array;
usefunctionjson_decode;
usefunctionparse_ini_file;
usefunctionparse_ini_string;
usefunctionpathinfo;
usefunctionstrtr;
usefunctionucfirst;
/**
* Class Config works as a parameter bag that provides ways to fill it
* from files or other Config instances. There are 2 common patterns
* to populate the config,
*
* either you can fill the Config instance from config files:
*
* $app->config()->fromPhpFile('myconfig.php');
* $app->config()->fromJsonFile('myconfig.json');
* $app->config()->fromEnvFile('.env');
* $app->config()->fromIniFile('myconfig.ini');
*
* or alternatively you can define the configuration options in the instance
* that calls `fromObject`,
*
* $app->config()->fromObject(MyConfig::class);
* $app->config()->fromObject($myconfig); // $myconfig is instance of Config
*
* Other interesting way to load configuration is from an environment variable
* that points to a file
*
* $app->config()->fromEnvVariable('MY_APP_SETTINGS');
*
* In this case, before launching the application you have to set the env variable
* to the file you want to use. On Linux and OSX use the export statement
*
* export MY_APP_SETTINGS='/path/to/config/file.php'
*
* or somewhere in your app bootstrap phase before constructing the Api instance
*
* putenv('MY_APP_SETTINGS=/path/to/config/file.php');
*
*/
#[\AllowDynamicProperties]
class Config extends Arguments implements Configuration
{
privatebool$silent = false;
/**
* Config constructor.
*
* @param string $root Path to which files are read relative from.
* When the config object is created by an application/library
* this is the application's root path
* @param Data|null $defaults [optional] An Optional config object with default values
*/
publicfunction__construct(
publicstring$root = '',
Data|null$defaults = null)
{
parent::__construct($defaults?->toArray() ?? []);
$this->root = $root ?: getcwd();
}
/**
* Bad method calls can be suppressed and allow the app
* to continue execution by setting the silent(true).
*
* The app should handle their configuration appropriately.
*
* @param string $name Method name
* @param array|null $arguments [optional]
* @return Configuration
* @throws Exception
*/
publicfunction__call(string$name, array|null$arguments): Configuration
{
if (false === $this->silent) {
thrownewException('Unable to load the configuration file ' . current($arguments));
}
return$this;
}
publicfunctionbuild(string$context): Configuration
{
thrownewException('Configuration factory should implement the method ' . __METHOD__);
}
publicfunctionwithParameters(array$parameters): Configuration
{
return$this->import($parameters);
}
publicfunctionfromObject(Configuration|string$object): Configuration
{
if (is_string($object) && class_exists($object) && is_a($object, Configuration::class, true)) {
$object = new$object;
}
$this->root = $object->root;
return$this->import(iterator_to_array($object));
}
publicfunctionfromJsonFile(string$filename): Configuration
{
return$this->loadDataFrom($filename,
fn() => json_decode(file_get_contents($filename), true)
);
}
publicfunctionfromIniFile(string$filename): Configuration
{
return$this->loadDataFrom($filename,
fn() => parse_ini_file($filename, true, INI_SCANNER_TYPED) ?: []
);
}
publicfunctionfromEnvFile(string$filename, string$namespace = ''): Configuration
{
try {
$data = parse_ini_file($this->filename($filename), true, INI_SCANNER_TYPED) ?: [];
env('', null, $this->filter($data, $namespace, false));
} catch (Exception$e) {
error_log('[Configuration error]: ' . $e->getMessage());
} finally {
return$this;
}
}
publicfunctionfromEnvVariable(string$variable): Configuration
{
if (false === empty($filename = getenv($variable))) {
$extension = ucfirst(pathinfo($filename, PATHINFO_EXTENSION));
returncall_user_func([$this, "from{$extension}File"], $filename);
}
if (false === $this->silent) {
thrownewException(strtr('The environment variable ":variable" is not set
and as such configuration could not be loaded. Set this variable and
make it point to a configuration file', [':variable' => $variable]));
}
error_log('[Configuration error]: ' . (error_get_last()['message'] ?? "env var: $variable"));
return$this;
}
publicfunctionfromPhpFile(string$filename): Configuration
{
return$this->loadDataFrom($filename, fn() => include$filename);
}
publicfunctionfromEnvironment(
array$variableNames,
string$namespace = '',
bool$lowercase = true,
bool$trim = true): Configuration
{
$data = [];
foreach ($variableNamesas$variable) {
$value = getenv($variable);
$data[] = $variable . '=' . (false === $value ? 'null' : $value);
}
$data = parse_ini_string(implode(PHP_EOL, $data), true, INI_SCANNER_TYPED) ?: [];
$this->import($this->filter($data, $namespace, $lowercase, $trim));
return$this;
}
publicfunctionsilent(bool$silent): Configuration
{
$this->silent = $silent;
return$this;
}
publicfunctionnamespace(
string$prefix,
bool$lowercase = true,
bool$trim = true): static
{
return (newstatic($this->root))->import(
$this->filter($this->toArray(), $prefix, $lowercase, $trim)
);
}
protectedfunctionloadDataFrom(string$filename, callable$loader): Configuration
{
return$this->import($loader($this->filename($filename)));
}
privatefunctionfilename(string$filename): string
{
return ('/' !== $filename[0])
? $this->root . '/' . $filename
: $filename;
}
}