• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

aplus-framework / config / 13066327659

29 Jan 2025 11:27PM UTC coverage: 100.0%. Remained the same
13066327659

push

github

natanfelles
Add debug icon

311 of 311 relevant lines covered (100.0%)

3.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Config.php
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of Aplus Framework Config Library.
4
 *
5
 * (c) Natan Felles <natanfelles@gmail.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Framework\Config;
11

12
use Framework\Config\Debug\ConfigCollector;
13
use Framework\Helpers\Isolation;
14
use LogicException;
15
use SensitiveParameter;
16

17
/**
18
 * Class Config.
19
 *
20
 * @package config
21
 */
22
class Config
23
{
24
    /**
25
     * @var array<string,array<string,array<mixed>>>
26
     */
27
    protected array $configs = [];
28
    protected ?string $configsDir = null;
29
    /**
30
     * @var array<string,array<string,array<mixed>>>
31
     */
32
    protected array $persistence = [];
33
    protected string $suffix;
34
    protected ConfigCollector $debugCollector;
35

36
    /**
37
     * Config constructor.
38
     *
39
     * @param array<string,array<string,array<mixed>>>|string|null $configs An
40
     * array to set many configs, the config base directory or null
41
     * @param array<string,array<string,array<mixed>>> $persistence Configs that
42
     * will always overwrite custom added, loaded or set configs
43
     * @param string $suffix The services filenames suffix used when the config
44
     * directory is set
45
     */
46
    public function __construct(
47
        #[SensitiveParameter]
48
        array | string | null $configs = null,
49
        #[SensitiveParameter]
50
        array $persistence = [],
51
        string $suffix = '.php'
52
    ) {
53
        if ($configs !== null) {
17✔
54
            \is_array($configs)
10✔
55
                ? $this->setMany($configs)
1✔
56
                : $this->setDir($configs);
10✔
57
        }
58
        $this->setPersistence($persistence);
17✔
59
        $this->suffix = $suffix;
17✔
60
    }
61

62
    /**
63
     * Set persistent configs.
64
     *
65
     * @param array<string,array<mixed>> $configs
66
     */
67
    protected function setPersistence(#[SensitiveParameter] array $configs) : void
68
    {
69
        $this->persistence = $configs;
17✔
70
    }
71

72
    /**
73
     * Replace configs with persistent configs.
74
     */
75
    protected function replacePersistence() : void
76
    {
77
        if (empty($this->persistence)) {
10✔
78
            return;
9✔
79
        }
80
        $this->configs = \array_replace_recursive($this->configs, $this->persistence);
1✔
81
    }
82

83
    /**
84
     * Get configs with persistence.
85
     *
86
     * @param string $name The service name
87
     * @param string $instance The service instance
88
     *
89
     * @return array<mixed> The service instance custom configs with
90
     * persistent configs
91
     */
92
    protected function getPersistentConfigs(string $name, string $instance) : array
93
    {
94
        $this->replacePersistence();
10✔
95
        return $this->configs[$name][$instance] ?? [];
10✔
96
    }
97

98
    /**
99
     * Set configs to a service instance.
100
     *
101
     * NOTE: These configs will replace an existing instance (except persistence).
102
     *
103
     * @param string $name The service name
104
     * @param array<mixed> $configs The new configs
105
     * @param string $instance The service instance
106
     *
107
     * @return array<mixed> The service instance configs
108
     */
109
    public function set(
110
        string $name,
111
        #[SensitiveParameter]
112
        array $configs,
113
        string $instance = 'default'
114
    ) : array {
115
        $this->configs[$name][$instance] = $configs;
10✔
116
        return $this->getPersistentConfigs($name, $instance);
10✔
117
    }
118

119
    /**
120
     * Get configs by a service instance.
121
     *
122
     * @param string $name The service name
123
     * @param string $instance The service instance
124
     *
125
     * @throws LogicException If the service configs are empty and the Config
126
     * directory is set, and the config file is not found
127
     *
128
     * @return array<mixed>|null The instance configs as array or null
129
     * if is not set
130
     */
131
    public function get(string $name, string $instance = 'default') : ?array
132
    {
133
        if (empty($this->configs[$name]) && isset($this->configsDir)) {
3✔
134
            $this->load($name);
1✔
135
        }
136
        return $this->configs[$name][$instance] ?? null;
3✔
137
    }
138

139
    /**
140
     * Get service instances configs.
141
     *
142
     * @param string $name The service name
143
     *
144
     * @return array<string,array<string,mixed>>|null The service instance names as
145
     * keys and its configs as values or null if the service is not set
146
     */
147
    public function getInstances(string $name) : ?array
148
    {
149
        return $this->configs[$name] ?? null;
1✔
150
    }
151

152
    /**
153
     * Add configs to a service instance.
154
     *
155
     * NOTE: IF the service instance already exists, the configs will be merged
156
     *
157
     * @param string $name The service name
158
     * @param array<mixed> $configs The service configs
159
     * @param string $instance The service instance
160
     *
161
     * @return array<mixed> The service instance configs
162
     */
163
    public function add(
164
        string $name,
165
        #[SensitiveParameter]
166
        array $configs,
167
        string $instance = 'default'
168
    ) : array {
169
        if (isset($this->configs[$name][$instance])) {
2✔
170
            $this->configs[$name][$instance] = \array_replace_recursive(
2✔
171
                $this->configs[$name][$instance],
2✔
172
                $configs
2✔
173
            );
2✔
174
            return $this->getPersistentConfigs($name, $instance);
2✔
175
        }
176
        return $this->set($name, $configs, $instance);
1✔
177
    }
178

179
    /**
180
     * Set many configs in one call.
181
     *
182
     * NOTE: The $configs will replace existing instances (except persistence).
183
     *
184
     * @param array<string,array<string,array<mixed>>> $configs The service
185
     * names as keys and its instance configs as values
186
     *
187
     * @return static
188
     */
189
    public function setMany(#[SensitiveParameter] array $configs) : static
190
    {
191
        foreach ($configs as $name => $values) {
8✔
192
            foreach ($values as $instance => $config) {
8✔
193
                $this->set($name, $config, $instance);
8✔
194
            }
195
        }
196
        return $this;
8✔
197
    }
198

199
    /**
200
     * Get all configs.
201
     *
202
     * @return array<string,array<string,array<mixed>>> All many configs
203
     */
204
    public function getAll() : array
205
    {
206
        return $this->configs;
8✔
207
    }
208

209
    /**
210
     * Set the base directory.
211
     *
212
     * @param string $directory Directory path
213
     *
214
     * @throws LogicException If the config directory is not found
215
     *
216
     * @return static
217
     */
218
    public function setDir(string $directory) : static
219
    {
220
        $dir = \realpath($directory);
14✔
221
        if ($dir === false || !\is_dir($dir)) {
14✔
222
            throw new LogicException('Config directory not found: ' . $directory);
1✔
223
        }
224
        $this->configsDir = $dir . \DIRECTORY_SEPARATOR;
14✔
225
        return $this;
14✔
226
    }
227

228
    /**
229
     * Get the base directory.
230
     *
231
     * @return string|null The directory realpath or null if it was not set
232
     */
233
    public function getDir() : ?string
234
    {
235
        return $this->configsDir;
6✔
236
    }
237

238
    /**
239
     * Load a config file.
240
     *
241
     * @param string $name The file name without the directory path and the suffix
242
     *
243
     * @throws LogicException If the config file is not found
244
     *
245
     * @return static
246
     */
247
    public function load(string $name) : static
248
    {
249
        if (isset($this->debugCollector)) {
7✔
250
            $start = \microtime(true);
3✔
251
            $this->loadFile($name);
3✔
252
            $end = \microtime(true);
3✔
253
            $this->debugCollector->addData([
3✔
254
                'start' => $start,
3✔
255
                'end' => $end,
3✔
256
                'name' => $name,
3✔
257
            ]);
3✔
258
            return $this;
3✔
259
        }
260
        return $this->loadFile($name);
4✔
261
    }
262

263
    protected function loadFile(string $name) : static
264
    {
265
        $filename = $this->configsDir . $name . $this->suffix;
7✔
266
        $filename = \realpath($filename);
7✔
267
        if ($filename === false || !\is_file($filename)) {
7✔
268
            throw new LogicException('Config file not found: ' . $name);
2✔
269
        }
270
        $configs = Isolation::require($filename);
6✔
271
        $this->setMany([$name => $configs]);
6✔
272
        return $this;
6✔
273
    }
274

275
    public function setDebugCollector(ConfigCollector $debugCollector) : static
276
    {
277
        $this->debugCollector = $debugCollector;
6✔
278
        $this->debugCollector->setConfig($this);
6✔
279
        return $this;
6✔
280
    }
281
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc