1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2010-2018 Nevraxe inc. & Marc André Audet <[email protected]>. All rights reserved. |
7
|
|
|
* |
8
|
|
|
* Redistribution and use in source and binary forms, with or without modification, are |
9
|
|
|
* permitted provided that the following conditions are met: |
10
|
|
|
* |
11
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this list of |
12
|
|
|
* conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice, this list |
15
|
|
|
* of conditions and the following disclaimer in the documentation and/or other materials |
16
|
|
|
* provided with the distribution. |
17
|
|
|
* |
18
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
19
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
20
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
21
|
|
|
* DISCLAIMED. IN NO EVENT SHALL NEVRAXE INC. & MARC ANDRÉ AUDET BE LIABLE FOR ANY |
22
|
|
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
23
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
24
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
25
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
27
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28
|
|
|
* |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
namespace Cervo\Config; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Configuration manager for Cervo. |
37
|
|
|
* |
38
|
|
|
* @author Marc André Audet <[email protected]> |
39
|
|
|
*/ |
40
|
|
|
class BaseConfig |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* The currently set values in a multi-dimensional array. |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
private $values = []; |
47
|
|
|
|
48
|
|
|
public function __construct() |
49
|
|
|
{ |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Set the value at the specified configuration path. |
55
|
|
|
* |
56
|
|
|
* @param string|array $name The configuration path |
57
|
|
|
* @param mixed $value |
58
|
|
|
* |
59
|
|
|
* @return $this |
60
|
|
|
*/ |
61
|
|
|
public function set($name, $value) |
62
|
|
|
{ |
63
|
|
|
if (!is_array($name)) { |
64
|
|
|
$name = explode('/', $name); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$current = &$this->values; |
68
|
|
|
|
69
|
|
|
foreach ($name as $key) { |
70
|
|
|
$current = &$current[$key]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$current = $value; |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Return the value for the specified configuration path. |
80
|
|
|
* If this value is not set, return the default value. |
81
|
|
|
* Return null if not set. |
82
|
|
|
* |
83
|
|
|
* @param string $name The configuration path |
84
|
|
|
* |
85
|
|
|
* @return mixed |
86
|
|
|
*/ |
87
|
|
|
public function get(string $name) |
88
|
|
|
{ |
89
|
|
|
$current = &$this->values; |
90
|
|
|
|
91
|
|
|
foreach (explode('/', $name) as $key) { |
92
|
|
|
|
93
|
|
|
if ($current[$key]) { |
94
|
|
|
$current = &$current[$key]; |
95
|
|
|
} else { |
96
|
|
|
return null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $current; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Recursively set all the values from an array. |
106
|
|
|
* Usually used when importing. |
107
|
|
|
* |
108
|
|
|
* @param array $array |
109
|
|
|
* @param array $current_path |
110
|
|
|
*/ |
111
|
|
|
public function setFromArrayRecursive(array $array, array $current_path = []) |
112
|
|
|
{ |
113
|
|
|
foreach ($array as $key => $el) { |
114
|
|
|
|
115
|
|
|
if (is_array($el)) { |
116
|
|
|
$this->setFromArrayRecursive($el, array_merge($current_path, [$key])); |
117
|
|
|
} else { |
118
|
|
|
$this->set(array_merge($current_path, [$key]), $el); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|