-
Notifications
You must be signed in to change notification settings - Fork 0
/
Config.php
216 lines (196 loc) · 6.8 KB
/
Config.php
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
<?php declare(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.
*/
namespace Koded\Stdlib;
use Exception;
use function call_user_func;
use function class_exists;
use function current;
use function error_get_last;
use function error_log;
use function file_get_contents;
use function getcwd;
use function getenv;
use function is_a;
use function is_string;
use function iterator_to_array;
use function join;
use function json_decode;
use function parse_ini_file;
use function parse_ini_string;
use function pathinfo;
use function strtr;
use function ucfirst;
/**
* 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
{
private bool $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
*/
public function __construct(
public string $root = '',
Data $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
*/
public function __call(string $name, array|null $arguments): Configuration
{
if (false === $this->silent) {
throw new Exception('Unable to load the configuration file ' . current($arguments));
}
return $this;
}
public function build(string $context): Configuration
{
throw new Exception('Configuration factory should implement the method ' . __METHOD__);
}
public function withParameters(array $parameters): Configuration
{
return $this->import($parameters);
}
public function fromObject(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));
}
public function fromJsonFile(string $filename): Configuration
{
return $this->loadDataFrom($filename,
fn() => json_decode(file_get_contents($filename), true)
);
}
public function fromIniFile(string $filename): Configuration
{
return $this->loadDataFrom($filename,
fn() => parse_ini_file($filename, true, INI_SCANNER_TYPED) ?: []
);
}
public function fromEnvFile(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;
}
}
public function fromEnvVariable(string $variable): Configuration
{
if (false === empty($filename = getenv($variable))) {
$extension = ucfirst(pathinfo($filename, PATHINFO_EXTENSION));
return call_user_func([$this, "from{$extension}File"], $filename);
}
if (false === $this->silent) {
throw new Exception(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;
}
public function fromPhpFile(string $filename): Configuration
{
return $this->loadDataFrom($filename, fn() => include $filename);
}
public function fromEnvironment(
array $variableNames,
string $namespace = '',
bool $lowercase = true,
bool $trim = true): Configuration
{
$data = [];
foreach ($variableNames as $variable) {
$value = getenv($variable);
$data[] = $variable . '=' . (false === $value ? 'null' : $value);
}
$data = parse_ini_string(join(PHP_EOL, $data), true, INI_SCANNER_TYPED) ?: [];
$this->import($this->filter($data, $namespace, $lowercase, $trim));
return $this;
}
public function silent(bool $silent): Configuration
{
$this->silent = $silent;
return $this;
}
public function namespace(
string $prefix,
bool $lowercase = true,
bool $trim = true): static
{
return (new static($this->root))->import(
$this->filter($this->toArray(), $prefix, $lowercase, $trim)
);
}
protected function loadDataFrom(string $filename, callable $loader): Configuration
{
return $this->import($loader($this->filename($filename)));
}
private function filename(string $filename): string
{
return ('/' !== $filename[0])
? $this->root . '/' . $filename
: $filename;
}
}