1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @description Dice - A minimal Dependency Injection Container for PHP |
5
|
|
|
* |
6
|
|
|
* @author Tom Butler [email protected] |
7
|
|
|
* @author Garrett Whitehorn http://garrettw.net/ |
8
|
|
|
* @copyright 2012-2018 Tom Butler <[email protected]> | https://r.je/dice.html |
9
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
10
|
|
|
* |
11
|
|
|
* @version 3.0 |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Dice\Loader; |
15
|
|
|
|
16
|
|
|
class Xml |
17
|
|
|
{ |
18
|
|
|
public function load($xml, \Dice\Dice $dice = null) |
19
|
|
|
{ |
20
|
|
|
if ($dice === null) { |
21
|
|
|
$dice = new \Dice\Dice(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
if (!($xml instanceof \SimpleXmlElement)) { |
25
|
|
|
$xml = simplexml_load_file($xml); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
$ns = $xml->getNamespaces(); |
29
|
|
|
|
30
|
|
|
if (isset($ns['']) && $ns[''] == 'https://r.je/dice/2.0') { |
31
|
|
|
return $this->loadV2($xml, $dice); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this->loadV1($xml, $dice); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
private function doConstructParams(\SimpleXmlElement $value, $rule) |
38
|
|
|
{ |
39
|
|
|
foreach ($value->constructParams->children() as $child) { |
40
|
|
|
$rule['constructParams'][] = $this->getComponent($child); |
41
|
|
|
} |
42
|
|
|
return $rule; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private function getComponent(\SimpleXmlElement $element, $forceInstance = false) |
46
|
|
|
{ |
47
|
|
|
if ($forceInstance) { |
48
|
|
|
return [\Dice\Dice::INSTANCE => (string) $element]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($element->instance) { |
52
|
|
|
return [\Dice\Dice::INSTANCE => (string) $element->instance]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return (string) $element; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
private function loadV1(\SimpleXmlElement $xml, \Dice\Dice $dice) |
59
|
|
|
{ |
60
|
|
|
foreach ($xml as $key => $value) { |
61
|
|
|
$rule = $dice->getRule((string) $value->name); |
62
|
|
|
|
63
|
|
|
if (isset($value->shared)) { |
64
|
|
|
$rule['shared'] = ((string) $value->shared === 'true'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (isset($value->inherit)) { |
68
|
|
|
$rule['inherit'] = (bool) $value->inherit; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if ($value->call) { |
72
|
|
|
foreach ($value->call as $name => $call) { |
73
|
|
|
$callArgs = []; |
74
|
|
|
if ($call->params) { |
75
|
|
|
foreach ($call->params->children() as $key => $param) { |
|
|
|
|
76
|
|
|
$callArgs[] = $this->getComponent($param); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
$rule['call'][] = [(string) $call->method, $callArgs]; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($value->instanceOf) { |
84
|
|
|
$rule['instanceOf'] = (string) $value->instanceOf; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($value->newInstances) { |
88
|
|
|
foreach ($value->newInstances as $ni) { |
89
|
|
|
$rule['newInstances'][] = (string) $ni; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if ($value->substitutions) { |
94
|
|
|
foreach ($value->substitutions as $use) { |
95
|
|
|
$rule['substitutions'][(string) $use->as] = $this->getComponent($use->use, true); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($value->constructParams) { |
100
|
|
|
$rule = $this->doConstructParams($value, $rule); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if ($value->shareInstances) { |
104
|
|
|
foreach ($value->shareInstances->children() as $share) { |
105
|
|
|
$rule['shareInstances'][] = $this->getComponent($share); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$dice->addRule((string) $value->name, $rule); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $dice; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
private function loadV2(\SimpleXmlElement $xml, \Dice\Dice $dice) |
116
|
|
|
{ |
117
|
|
|
foreach ($xml as $key => $value) { |
118
|
|
|
$rule = $dice->getRule((string) $value->name); |
119
|
|
|
|
120
|
|
|
if ($value->call) { |
121
|
|
|
foreach ($value->call as $name => $call) { |
122
|
|
|
$callArgs = []; |
123
|
|
|
foreach ($call->children() as $key => $param) { |
|
|
|
|
124
|
|
|
$callArgs[] = $this->getComponent($param); |
125
|
|
|
} |
126
|
|
|
$rule['call'][] = [(string) $call['method'], $callArgs]; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if (isset($value['inherit'])) { |
131
|
|
|
$rule['inherit'] = (bool) $value['inherit']; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($value['instanceOf']) { |
135
|
|
|
$rule['instanceOf'] = (string) $value['instanceOf']; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (isset($value['shared'])) { |
139
|
|
|
$rule['shared'] = ((string) $value['shared'] === 'true'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if ($value->constructParams) { |
143
|
|
|
$rule = $this->doConstructParams($value, $rule); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if ($value->substitute) { |
147
|
|
|
foreach ($value->substitute as $use) { |
148
|
|
|
$rule['substitutions'][(string) $use['as']] = $this->getComponent($use['use'], true); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if ($value->shareInstances) { |
153
|
|
|
foreach ($value->shareInstances->children() as $share) { |
154
|
|
|
$rule['shareInstances'][] = $this->getComponent($share); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$dice->addRule((string) $value['name'], $rule); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $dice; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|