Completed
Push — dev ( 8a0e98...4f1f6b )
by Zach
02:15
created

Config::refresh()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yarak\Config;
4
5
use Phalcon\DI;
6
use Yarak\Exceptions\InvalidConfig;
7
use Phalcon\Config as PhalconConfig;
8
9
class Config
10
{
11
    use PathHelpers;
12
13
    /**
14
     * Instance of self.
15
     *
16
     * @var Config
17
     */
18
    private static $instance;
19
20
    /**
21
     * Phalcon config instance.
22
     *
23
     * @var PhalconConfig
24
     */
25
    protected $config;
26
27
    /**
28
     * The original config array.
29
     *
30
     * @var array
31
     */
32
    protected $original;
33
34
    /**
35
     * Default setting values.
36
     *
37
     * @var array
38
     */
39
    const DEFAULTS = [
40
        'migratorType' => 'fileDate',
41
        'migrationRepository' => 'database',
42
    ];
43
44
    /**
45
     * Private constructor.
46
     *
47
     * @param array $configArray
48
     */
49
    private function __construct()
50
    {
51
    }
52
53
    /**
54
     * Get config values by key from PhalconConfig.
55
     *
56
     * @param string $key
57
     *
58
     * @return mixed
59
     */
60
    public function __get($key)
61
    {
62
        try {
63
            return $this->config->$key;
64
        } catch (\Exception $e) {
65
            return $this->getDefault($key);
66
        }
67
    }
68
69
    /**
70
     * Get instance of self with config array set.
71
     *
72
     * @param array $configArray
73
     *
74
     * @return Config
75
     */
76
    public static function getInstance()
77
    {
78
        if (empty(self::$instance)) {
79
            self::$instance = new self();
80
        }
81
82
        return self::$instance;
83
    }
84
85
    /**
86
     * Set config using config data in di or passed data.
87
     *
88
     * @param array|string $userConfig If string, config path. If array, data.
89
     * @param bool         $merge      If true, merge given config array into current.
90
     */
91
    public function setConfig($userConfig = null, $merge = true)
92
    {
93
        $this->config = \Phalcon\Di::getDefault()->get('config');
94
95
        if (is_string($userConfig)) {
96
            $this->config = $this->getNested(explode('.', $userConfig));
97
        } elseif (is_array($userConfig)) {
98
            $userConfig = new PhalconConfig($userConfig);
99
100
            if ($merge === true) {
101
                $this->merge($userConfig);
102
            } else {
103
                $this->config = $userConfig;
104
            }
105
        }
106
107
        $this->original = $this->toArray();
108
109
        return $this;
110
    }
111
112
    /**
113
     * Get a value by key from config.
114
     *
115
     * @param string|array $key
116
     *
117
     * @return mixed
118
     */
119
    public function get($key)
120
    {
121
        return $this->$key;
122
    }
123
124
    /**
125
     * Get nested values.
126
     *
127
     * @param array $keyArray
128
     *
129
     * @return mixed
130
     */
131
    public function getNested(array $keyArray)
132
    {
133
        $current = $this->config;
134
135
        foreach ($keyArray as $key) {
136
            if ($current[$key] === null) {
137
                return $this->getDefault($key);
138
            }
139
140
            $current = $current->$key;
141
        }
142
143
        return $current;
144
    }
145
146
    /**
147
     * Return true if config array has given value.
148
     *
149
     * @param mixed $value
150
     *
151
     * @return bool
152
     */
153
    public function has($value)
154
    {
155
        $current = $this->config;
156
157
        foreach ($this->makeArray($value) as $configItem) {
158
            if ($current[$configItem] === null) {
159
                $current = $this->getDefault($configItem);
160
161
                break;
162
            }
163
164
            $current = $current->$configItem;
165
        }
166
167
        return $current !== null;
168
    }
169
170
    /**
171
     * Get a setting's default value.
172
     *
173
     * @param string $key
174
     *
175
     * @return mixed|null
176
     */
177
    public function getDefault($key)
178
    {
179
        if (array_key_exists($key, self::DEFAULTS)) {
180
            return self::DEFAULTS[$key];
181
        }
182
183
        return;
184
    }
185
186
    /**
187
     * Set an item in the config.
188
     *
189
     * @param mixed $keys
190
     * @param mixed $value
191
     */
192
    public function set($keys, $value)
193
    {
194
        $temp = &$this->config;
195
196
        $keys = $this->makeArray($keys);
197
198
        $count = count($keys);
199
200
        foreach ($keys as $index => $key) {
201
            if ($count === $index + 1) {
202
                return $temp[$key] = $value;
203
            } elseif ($temp[$key] === null) {
204
                $temp[$key] = new PhalconConfig();
205
            }
206
207
            $temp = &$temp[$key];
208
        }
209
    }
210
211
    /**
212
     * Remove an item from the config.
213
     *
214
     * @param mixed $keys
215
     */
216
    public function remove($keys)
217
    {
218
        $temp = &$this->config;
219
220
        $keys = $this->makeArray($keys);
221
222
        $count = count($keys);
223
224
        foreach ($keys as $key) {
225
            if ($key === $keys[$count - 1]) {
226
                unset($temp[$key]);
227
            } else {
228
                $temp = &$temp[$key];
229
            }
230
        }
231
    }
232
233
    /**
234
     * Set the config array to its original values.
235
     */
236
    public function refresh()
237
    {
238
        $this->config = $this->config->__set_state($this->original);
239
    }
240
241
    /**
242
     * Return the config array.
243
     *
244
     * @return array
245
     */
246
    public function toArray()
247
    {
248
        if (is_string($this->config)) {
249
            return $this->config;
250
        }
251
252
        return $this->config->toArray();
253
    }
254
255
    /**
256
     * Make a variable an array if not one already.
257
     *
258
     * @param mixed $value
259
     *
260
     * @return array
261
     */
262
    protected function makeArray($value)
263
    {
264
        if (!is_array($value)) {
265
            return [$value];
266
        }
267
268
        return $value;
269
    }
270
271
    /**
272
     * Validate that a setting exists.
273
     *
274
     * @param array $settings
275
     *
276
     * @throws InvalidConfig
277
     */
278
    public function validate(array $settings)
279
    {
280
        if (!$this->has($settings)) {
281
            throw InvalidConfig::configValueNotFound(implode(' -> ', $settings));
282
        }
283
    }
284
285
    /**
286
     * Merge the given config with the current one.
287
     *
288
     * @param PhalconConfig $config
289
     */
290
    public function merge(PhalconConfig $config)
291
    {
292
        $this->config = $this->config->merge($config);
293
    }
294
295
    /**
296
     * Return the config key count.
297
     *
298
     * @return int
299
     */
300
    public function count()
301
    {
302
        return $this->config->count();
303
    }
304
}
305