|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Scabbia2 Config Component |
|
4
|
|
|
* https://github.com/eserozvataf/scabbia2 |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
* |
|
9
|
|
|
* @link https://github.com/eserozvataf/scabbia2-config for the canonical source repository |
|
10
|
|
|
* @copyright 2010-2016 Eser Ozvataf. (http://eser.ozvataf.com/) |
|
11
|
|
|
* @license http://www.apache.org/licenses/LICENSE-2.0 - Apache License, Version 2.0 |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Scabbia\Config; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* ConfigCollection |
|
18
|
|
|
* |
|
19
|
|
|
* @package Scabbia\Config |
|
20
|
|
|
* @author Eser Ozvataf <[email protected]> |
|
21
|
|
|
* @since 2.0.0 |
|
22
|
|
|
*/ |
|
23
|
|
|
class ConfigCollection |
|
24
|
|
|
{ |
|
25
|
|
|
/** @type int NONE no flag */ |
|
26
|
|
|
const NONE = 0; |
|
27
|
|
|
/** @type int OVERWRITE overwrite existing nodes by default */ |
|
28
|
|
|
const OVERWRITE = 1; |
|
29
|
|
|
/** @type int FLATTEN flatten nodes by default */ |
|
30
|
|
|
const FLATTEN = 2; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
/** @type array configuration flags */ |
|
34
|
|
|
public $configFlags = [ |
|
35
|
|
|
"disabled" => false // always false |
|
36
|
|
|
]; |
|
37
|
|
|
/** @type array configuration content */ |
|
38
|
|
|
public $content = []; |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Adds a piece into configuration compilation |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $uConfig configuration |
|
45
|
|
|
* @param int $uLoadingFlags loading flags |
|
46
|
|
|
* |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
|
|
public function add($uConfig, $uLoadingFlags = self::NONE) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->process($this->content, $uConfig, $uLoadingFlags); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Sets a configuration flag |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $uName name of the configuration flag |
|
58
|
|
|
* @param bool $uValue value |
|
59
|
|
|
* |
|
60
|
|
|
* @return void |
|
61
|
|
|
*/ |
|
62
|
|
|
public function setFlag($uName, $uValue) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->configFlags[$uName] = $uValue; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Returns route information in order to store it |
|
69
|
|
|
* |
|
70
|
|
|
* @return array |
|
71
|
|
|
*/ |
|
72
|
|
|
public function save() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->content; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Processes the configuration file in order to simplify its accessibility |
|
79
|
|
|
* |
|
80
|
|
|
* @param mixed $uTarget target reference |
|
81
|
|
|
* @param mixed $uNode source object |
|
82
|
|
|
* @param int $uLoadingFlags loading flags |
|
83
|
|
|
* |
|
84
|
|
|
* @return void |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function process(&$uTarget, $uNode, $uLoadingFlags) |
|
87
|
|
|
{ |
|
88
|
|
|
$tQueue = [ |
|
89
|
|
|
[[], $uNode, $uLoadingFlags, &$uTarget, null, false] |
|
90
|
|
|
]; |
|
91
|
|
|
|
|
92
|
|
|
do { |
|
93
|
|
|
$tItem = array_pop($tQueue); |
|
94
|
|
|
|
|
95
|
|
|
if ($tItem[4] === null) { |
|
96
|
|
|
$tRef = &$tItem[3]; |
|
97
|
|
|
} else { |
|
98
|
|
|
$tRef = &$tItem[3][$tItem[4]]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
if (is_scalar($tItem[1]) || $tItem[1] === null) { |
|
102
|
|
|
if (!isset($tRef) || ($tItem[2] & self::OVERWRITE) === self::OVERWRITE) { |
|
103
|
|
|
$tRef = $tItem[1]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
continue; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
if (!is_array($tRef) || ($tItem[2] & self::OVERWRITE) === self::OVERWRITE) { |
|
110
|
|
|
$tRef = []; // initialize as an empty array |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
foreach ($tItem[1] as $tKey => $tSubnode) { |
|
114
|
|
|
$tFlags = $tItem[2]; |
|
115
|
|
|
$tListNode = false; |
|
116
|
|
|
|
|
117
|
|
|
$tNodeParts = explode("|", $tKey); |
|
118
|
|
|
$tNodeKey = array_shift($tNodeParts); |
|
119
|
|
|
|
|
120
|
|
|
if ($tItem[5] && is_numeric($tNodeKey)) { |
|
121
|
|
|
$tNodeKey = count($tRef); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
foreach ($tNodeParts as $tNodePart) { |
|
125
|
|
|
if (array_key_exists($tNodePart, $this->configFlags)) { |
|
126
|
|
|
if ($this->configFlags[$tNodePart] !== true) { |
|
127
|
|
|
continue 2; |
|
128
|
|
|
} |
|
129
|
|
|
} elseif ($tNodePart === "list") { |
|
130
|
|
|
$tListNode = true; |
|
131
|
|
|
} elseif ($tNodePart === "important") { |
|
132
|
|
|
$tFlags |= self::OVERWRITE; |
|
133
|
|
|
} elseif ($tNodePart === "flat") { |
|
134
|
|
|
$tFlags |= self::FLATTEN; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
$tNewNodeKey = $tItem[0]; |
|
139
|
|
|
if (($tFlags & self::FLATTEN) === self::FLATTEN) { |
|
140
|
|
|
$tNodeKey = ltrim("{$tItem[4]}/{$tNodeKey}", "/"); |
|
141
|
|
|
$tNewNodeKey[] = $tNodeKey; |
|
142
|
|
|
|
|
143
|
|
|
$tQueue[] = [$tNewNodeKey, $tSubnode, $tFlags, &$tRef, $tNodeKey, $tListNode]; |
|
144
|
|
|
} else { |
|
145
|
|
|
$tNewNodeKey[] = $tNodeKey; |
|
146
|
|
|
$tQueue[] = [$tNewNodeKey, $tSubnode, $tFlags, &$tRef[$tNodeKey], null, $tListNode]; |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
} while (count($tQueue) > 0); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|