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

aplus-framework / config / 7243496232

06 Dec 2023 08:09PM UTC coverage: 100.0%. Remained the same
7243496232

push

github

natanfelles
Test with PHP latest version

206 of 206 relevant lines covered (100.0%)

2.76 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\Helpers\Isolation;
13
use LogicException;
14
use SensitiveParameter;
15

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

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

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

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

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

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

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

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

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

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

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

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

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

236
    /**
237
     * Load a config file.
238
     *
239
     * @param string $name The file name without the directory path and the suffix
240
     *
241
     * @throws LogicException If the config file is not found
242
     *
243
     * @return static
244
     */
245
    public function load(string $name) : static
246
    {
247
        $filename = $this->configsDir . $name . $this->suffix;
4✔
248
        $filename = \realpath($filename);
4✔
249
        if ($filename === false || !\is_file($filename)) {
4✔
250
            throw new LogicException('Config file not found: ' . $name);
2✔
251
        }
252
        $configs = Isolation::require($filename);
3✔
253
        $this->setMany([$name => $configs]);
3✔
254
        return $this;
3✔
255
    }
256
}
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