|
1
|
|
|
<?php |
|
2
|
|
|
namespace mxdiModule\Service; |
|
3
|
|
|
|
|
4
|
|
|
use mxdiModule\Annotation\AnnotationInterface; |
|
5
|
|
|
use mxdiModule\Annotation\InjectParams; |
|
6
|
|
|
|
|
7
|
|
|
class XmlExtractor implements ExtractorInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* Path to the XML file. |
|
11
|
|
|
* @var array |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $file; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Parsed results |
|
17
|
|
|
* @var \SimpleXMLElement|null |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $config; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Was config parsed? |
|
23
|
|
|
* @var bool |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $isConfigParsed; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param array $options |
|
29
|
|
|
*/ |
|
30
|
10 |
View Code Duplication |
public function __construct(array $options) |
|
31
|
|
|
{ |
|
32
|
10 |
|
if (!isset($options['file']) || empty($options['file'])) { |
|
33
|
2 |
|
throw new \InvalidArgumentException('XML file path is missing'); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
10 |
|
if (! file_exists($options['file'])) { |
|
37
|
1 |
|
throw new \InvalidArgumentException('XML file is missing'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
10 |
|
$this->file = $options['file']; |
|
41
|
10 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* {@inheritdoc} |
|
45
|
|
|
*/ |
|
46
|
3 |
|
public function getConstructorInjections($fqcn) |
|
47
|
|
|
{ |
|
48
|
3 |
|
$this->isConfigParsed($fqcn); |
|
49
|
|
|
|
|
50
|
3 |
|
if (empty($this->config)) { |
|
51
|
1 |
|
return null; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
2 |
|
$values = []; |
|
55
|
2 |
|
foreach ($this->config->xpath('constructor/inject') as $spec) { |
|
56
|
1 |
|
$values[] = $this->createInjectionObject($spec); |
|
57
|
2 |
|
} |
|
58
|
|
|
|
|
59
|
2 |
|
if (!count($values)) { |
|
60
|
1 |
|
return null; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
$injections = new InjectParams(); |
|
64
|
1 |
|
$injections->value = $values; |
|
65
|
|
|
|
|
66
|
1 |
|
return $injections; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
3 |
|
public function getMethodsInjections($fqcn) |
|
73
|
|
|
{ |
|
74
|
3 |
|
$this->isConfigParsed($fqcn); |
|
75
|
|
|
|
|
76
|
3 |
|
if (empty($this->config)) { |
|
77
|
1 |
|
return []; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
$injections = []; |
|
81
|
2 |
|
foreach ($this->config->xpath('methods/method') as $spec) { |
|
82
|
1 |
|
$params = new InjectParams(); |
|
83
|
1 |
|
foreach ($spec->xpath('inject') as $methodSpec) { |
|
84
|
1 |
|
$params->value[] = $this->createInjectionObject($methodSpec); |
|
85
|
1 |
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
$injections[(string)$spec['name']] = $params; |
|
88
|
2 |
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
return $injections; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* {@inheritdoc} |
|
95
|
|
|
*/ |
|
96
|
3 |
|
public function getPropertiesInjections($fqcn) |
|
97
|
|
|
{ |
|
98
|
3 |
|
$this->isConfigParsed($fqcn); |
|
99
|
|
|
|
|
100
|
3 |
|
if (empty($this->config)) { |
|
101
|
1 |
|
return []; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
2 |
|
$injections = []; |
|
105
|
2 |
|
foreach ($this->config->xpath('properties/property') as $spec) { |
|
106
|
1 |
|
$injections[(string)$spec['name']] = $this->createInjectionObject($spec->xpath('inject')[0]); |
|
107
|
2 |
|
} |
|
108
|
|
|
|
|
109
|
2 |
|
return $injections; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* {@inheritdoc} |
|
114
|
|
|
*/ |
|
115
|
1 |
|
public function getChangeSet($fqcn) |
|
116
|
|
|
{ |
|
117
|
1 |
|
$this->isConfigParsed($fqcn); |
|
118
|
|
|
|
|
119
|
1 |
|
if (!empty((string)$this->config['fqcn'])) { |
|
120
|
|
|
$fqcn = (string)$this->config['fqcn']; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
return new ChangeSet($this, $fqcn); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return array |
|
128
|
|
|
*/ |
|
129
|
1 |
|
public function getFile() |
|
130
|
|
|
{ |
|
131
|
1 |
|
return $this->file; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Parse the XML. |
|
136
|
|
|
* @param string $fqcn |
|
137
|
|
|
*/ |
|
138
|
6 |
|
private function isConfigParsed($fqcn) |
|
139
|
|
|
{ |
|
140
|
6 |
|
if ($this->isConfigParsed) { |
|
141
|
3 |
|
return; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
6 |
|
$module = simplexml_load_file($this->file); |
|
145
|
|
|
|
|
146
|
6 |
|
foreach ($module->xpath('/mxdiModule/service') as $service) { |
|
147
|
6 |
|
if ((string)$service['id'] === $fqcn) { |
|
148
|
5 |
|
$this->config = $service; |
|
149
|
5 |
|
break; |
|
150
|
|
|
} |
|
151
|
6 |
|
} |
|
152
|
|
|
|
|
153
|
6 |
|
$this->isConfigParsed = true; |
|
154
|
6 |
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param \SimpleXMLElement $spec |
|
158
|
|
|
* @return AnnotationInterface |
|
159
|
|
|
*/ |
|
160
|
3 |
|
private function createInjectionObject(\SimpleXMLElement $spec) |
|
161
|
|
|
{ |
|
162
|
3 |
|
$injectionFqcn = (string)$spec['type']; |
|
163
|
3 |
|
$injection = new $injectionFqcn; |
|
164
|
|
|
|
|
165
|
3 |
|
foreach ($spec->xpath('param') as $param) { |
|
166
|
3 |
|
$this->createProperty($injection, $param); |
|
167
|
3 |
|
} |
|
168
|
|
|
|
|
169
|
3 |
|
return $injection; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param object $injection |
|
174
|
|
|
* @param \SimpleXMLElement $param |
|
175
|
|
|
*/ |
|
176
|
3 |
|
private function createProperty($injection, $param) |
|
177
|
|
|
{ |
|
178
|
3 |
|
$property = (string)$param['name']; |
|
179
|
|
|
|
|
180
|
3 |
|
switch ((string)$param['type']) { |
|
181
|
3 |
|
case 'boolean': $value = (bool)(string)$param; break; |
|
|
|
|
|
|
182
|
3 |
|
case 'integer': $value = (int)(string)$param; break; |
|
|
|
|
|
|
183
|
3 |
|
case 'float': $value = (float)(string)$param; break; |
|
|
|
|
|
|
184
|
3 |
|
default: $value = (string)$param; |
|
|
|
|
|
|
185
|
3 |
|
} |
|
186
|
|
|
|
|
187
|
3 |
|
$injection->$property = $value; |
|
188
|
3 |
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.