1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Cervo package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2010-2019 Nevraxe inc. & Marc André Audet <[email protected]>. |
7
|
|
|
* |
8
|
|
|
* @package Cervo |
9
|
|
|
* @author Marc André Audet <[email protected]> |
10
|
|
|
* @copyright 2010 - 2019 Nevraxe inc. & Marc André Audet |
11
|
|
|
* @license See LICENSE.md MIT |
12
|
|
|
* @link https://github.com/Nevraxe/Cervo |
13
|
|
|
* @since 5.0.0 |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
declare(strict_types=1); |
17
|
|
|
|
18
|
|
|
namespace Cervo\Config; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Configuration manager for Cervo. |
22
|
|
|
* |
23
|
|
|
* @author Marc André Audet <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class BaseConfig |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* The currently set values in a multi-dimensional array. |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $values = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set the value at the specified configuration path. |
35
|
|
|
* |
36
|
|
|
* @param string|array $name The configuration path |
37
|
|
|
* @param mixed $value |
38
|
|
|
* |
39
|
|
|
* @return $this |
40
|
|
|
*/ |
41
|
|
|
public function set($name, $value) |
42
|
|
|
{ |
43
|
|
|
if (!is_array($name)) { |
44
|
|
|
$name = explode('/', $name); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$current = &$this->values; |
48
|
|
|
|
49
|
|
|
foreach ($name as $key) { |
50
|
|
|
$current = &$current[$key]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$current = $value; |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Return the value for the specified configuration path. |
60
|
|
|
* If this value is not set, return the default value. |
61
|
|
|
* Return null if not set. |
62
|
|
|
* |
63
|
|
|
* @param string $name The configuration path |
64
|
|
|
* |
65
|
|
|
* @return mixed |
66
|
|
|
*/ |
67
|
|
|
public function get(string $name) |
68
|
|
|
{ |
69
|
|
|
$current = &$this->values; |
70
|
|
|
|
71
|
|
|
foreach (explode('/', $name) as $key) { |
72
|
|
|
|
73
|
|
|
if (isset($current[$key])) { |
74
|
|
|
$current = &$current[$key]; |
75
|
|
|
} else { |
76
|
|
|
return null; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $current; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Recursively set all the values from an array. |
86
|
|
|
* Usually used when importing. |
87
|
|
|
* |
88
|
|
|
* @param array $array |
89
|
|
|
* @param array $currentPath |
90
|
|
|
*/ |
91
|
|
|
public function setFromArrayRecursive(array $array, array $currentPath = []) |
92
|
|
|
{ |
93
|
|
|
foreach ($array as $key => $el) { |
94
|
|
|
|
95
|
|
|
if (is_array($el)) { |
96
|
|
|
$this->setFromArrayRecursive($el, array_merge($currentPath, [$key])); |
97
|
|
|
} else { |
98
|
|
|
$this->set(array_merge($currentPath, [$key]), $el); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|