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