Passed
Push — develop ( 26e410...f15708 )
by nguereza
02:21
created

Config::getValue()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 9
c 1
b 0
f 0
nc 5
nop 3
dl 0
loc 19
rs 9.2222
1
<?php
2
3
/**
4
 * Platine Config
5
 *
6
 * Platine Config is the library used to manage the application
7
 * configuration using differents loaders
8
 *
9
 * This content is released under the MIT License (MIT)
10
 *
11
 * Copyright (c) 2020 Platine Config
12
 *
13
 * Permission is hereby granted, free of charge, to any person obtaining a copy
14
 * of this software and associated documentation files (the "Software"), to deal
15
 * in the Software without restriction, including without limitation the rights
16
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
 * copies of the Software, and to permit persons to whom the Software is
18
 * furnished to do so, subject to the following conditions:
19
 *
20
 * The above copyright notice and this permission notice shall be included in all
21
 * copies or substantial portions of the Software.
22
 *
23
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
 * SOFTWARE.
30
 */
31
32
/**
33
 *  @file Config.php
34
 *
35
 *  The Config class used to manage application configuration
36
 *
37
 *  @package    Platine\Config
38
 *  @author Platine Developers Team
39
 *  @copyright  Copyright (c) 2020
40
 *  @license    http://opensource.org/licenses/MIT  MIT License
41
 *  @link   http://www.iacademy.cf
42
 *  @version 1.0.0
43
 *  @filesource
44
 */
45
46
declare(strict_types=1);
47
48
namespace Platine\Config;
49
50
use ArrayAccess;
51
use Platine\Stdlib\Helper\Arr;
52
53
/**
54
 * Class Config
55
 * @package Platine\Config
56
 * @template T
57
 * @implements ArrayAccess<string, mixed>
58
 */
59
class Config implements ArrayAccess
60
{
0 ignored issues
show
Coding Style introduced by
Opening brace must not be followed by a blank line
Loading history...
61
62
    /**
63
     * The configuration loader to use
64
     * @var LoaderInterface
65
     */
66
    protected LoaderInterface $loader;
67
68
    /**
69
     * The config environment to use
70
     * @var string
71
     */
72
    protected string $env;
73
74
    /**
75
     * The config items loaded
76
     * @var array<string, mixed>
77
     */
78
    protected array $items = [];
79
80
    /**
81
     * Create new configuration instance
82
     * @param LoaderInterface $loader the loader to use
83
     * @param string          $env    the name of the environment
84
     */
85
    public function __construct(LoaderInterface $loader, string $env = '')
86
    {
87
        $this->loader = $loader;
88
        $this->env = $env;
89
    }
90
91
    /**
92
     * Check whether the configuration for given key exists
93
     * @param  string  $key the name of the key
94
     * @return boolean
95
     */
96
    public function has(string $key): bool
97
    {
98
        return $this->get($key, $this) !== $this;
99
    }
100
101
    /**
102
     * Return the configuration value for the given key
103
     * @param  string $key     the name of the config item
104
     * @param  mixed $default the default value if can not find the config item
105
     * @return mixed
106
     */
107
    public function get(string $key, $default = null)
108
    {
109
        list($group, ) = $this->parseKey($key);
110
        $this->load($group);
111
112
        return Arr::get($this->items, $key, $default);
113
    }
114
115
    /**
116
     * Set the configuration value for the given key
117
     * @param  string $key     the name of the config item
118
     * @param  mixed $value the configuration value
119
     * @return void
120
     */
121
    public function set(string $key, $value): void
122
    {
123
        list($group, $item) = $this->parseKey($key);
124
125
        // We'll need to go ahead and lazy load each configuration groups even when
126
        // we're just setting a configuration item so that the set item does not
127
        // get overwritten if a different item in the group is requested later.
128
        $this->load($group);
129
130
        if (is_null($item)) {
131
            $this->items[$group] = $value;
132
        } else {
133
            Arr::set($this->items[$group], $item, $value);
134
        }
135
    }
136
137
    /**
138
     * Return all the configuration items
139
     * @return array<string, mixed>
140
     */
141
    public function getItems(): array
142
    {
143
        return $this->items;
144
    }
145
146
    /**
147
     * Return the configuration current environment
148
     * @return string
149
     */
150
    public function getEnvironment(): string
151
    {
152
        return $this->env;
153
    }
154
155
    /**
156
     * Set the configuration environment
157
     *
158
     * @param string $env
159
     * @return $this
160
     */
161
    public function setEnvironment(string $env): self
162
    {
163
        $this->env = $env;
164
165
        return $this;
166
    }
167
168
    /**
169
     * Return the configuration current loader
170
     * @return LoaderInterface
171
     */
172
    public function getLoader(): LoaderInterface
173
    {
174
        return $this->loader;
175
    }
176
177
    /**
178
     * Set the configuration loader
179
     *
180
     * @param LoaderInterface $loader
181
     * @return $this
182
     */
183
    public function setLoader(LoaderInterface $loader): self
184
    {
185
        $this->loader = $loader;
186
187
        return $this;
188
    }
189
190
    /**
191
     * {@inheritdoc}
192
     */
193
    public function offsetExists($key)
194
    {
195
        return $this->has($key);
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201
    public function offsetGet($key)
202
    {
203
        return $this->get($key);
204
    }
205
206
    /**
207
     *
208
     * @param string $key
209
     * @param mixed $value
210
     * @return void
211
     */
212
    public function offsetSet($key, $value)
213
    {
214
        $this->set($key, $value);
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     */
220
    public function offsetUnset($key)
221
    {
222
        $this->set($key, null);
223
    }
224
225
    /**
226
     * Load the configuration group for the key.
227
     * @param  string $group the name of group to load
228
     * @return void
229
     */
230
    protected function load(string $group): void
231
    {
232
        // If we've already loaded this collection, we will just bail out since we do
233
        // not want to load it again. Once items are loaded a first time they will
234
        // stay kept in memory within this class and not loaded from disk again.
235
        if (isset($this->items[$group])) {
236
            return;
237
        }
238
        $loaded = $this->loader->load($this->env, $group);
239
240
        if (!empty($loaded)) {
241
            $this->items[$group] = $loaded;
242
        }
243
    }
244
245
    /**
246
     * Parse the configuration key
247
     * @param  string $key the name of the key
248
     * @return array<int, mixed>
249
     */
250
    protected function parseKey(string $key): array
251
    {
252
        if (($pos = strpos($key, '.')) === false) {
253
            return [$key, null];
254
        }
255
        return [substr($key, 0, $pos), substr($key, $pos + 1)];
256
    }
257
}
258