This repository has been archived by the owner on Feb 13, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 90
/
Config.php
138 lines (120 loc) · 2.74 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
<?php declare(strict_types=1);
namespace Siler\Config;
use Laminas\Config\Config;
use Laminas\Config\Factory;
use Laminas\Config\Processor\ProcessorInterface;
use Laminas\Config\Processor\Queue;
use Laminas\Config\Reader\ReaderInterface;
use Laminas\Config\Reader\Yaml;
use Laminas\ConfigAggregator\ConfigAggregator;
use Laminas\ConfigAggregator\LaminasConfigProvider;
use Siler\Container;
const CONFIG = 'siler_config';
const CONFIG_PROCESSORS = 'siler_config_processors';
/**
* Registers custom readers.
*
* @param array<string, ReaderInterface> $readers
*/
function readers(array $readers): void
{
foreach ($readers as $extension => $reader) {
Factory::registerReader($extension, $reader);
}
}
/**
* Registers configuration processors.
*
* @param array<ProcessorInterface> $processors
*/
function processors(array $processors): void
{
Container\set(CONFIG_PROCESSORS, $processors);
}
/**
* Gets a configuration.
*
* @template T
* @param string $path
* @param T|null $default
* @return T|null
*/
function config(string $path, $default = null)
{
$keys = explode('.', $path);
$pointer = all();
foreach ($keys as $key) {
if (empty($pointer)) {
return $default;
}
if (\is_array($pointer)) {
/** @var T|array|null $pointer */
$pointer = $pointer[$key] ?? null;
}
}
return $pointer ?? $default;
}
/**
* Initializes configuration object.
*
* @param string $directory
* @param string $blob
* @return Config
*/
function load(string $directory, string $blob = '/*.*'): Config
{
$aggregator = new ConfigAggregator([
new LaminasConfigProvider($directory . $blob)
]);
$data = $aggregator->getMergedConfig();
$config = new Config($data, true);
/** @var array<ProcessorInterface> */
$processors = Container\get(CONFIG_PROCESSORS, []);
$queue = new Queue();
foreach ($processors as $processor) {
$queue->insert($processor);
}
$queue->process($config);
Container\set(CONFIG, $config);
return $config;
}
/**
* Checks if the key exists on the config.
*
* @param string $path
* @return bool
*/
function has(string $path): bool
{
return config($path) !== null;
}
/**
* Returns all the configurations.
*
* @return array|null
*/
function all(): ?array
{
/** @var Config|null $config */
$config = Container\get(CONFIG);
if ($config === null) {
return null;
}
return $config->toArray();
}
/**
* Sets callback for decoding Yaml.
*
* @param string|callable $yamlDecoder
*/
function yaml($yamlDecoder): void
{
$yaml = new Yaml();
$yaml->setYamlDecoder($yamlDecoder);
readers(
[
'yaml' => $yaml,
'yml' => $yaml
]
);
}