1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PEIP\Service; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* To change this template, choose Tools | Templates |
7
|
|
|
* and open the template in the editor. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/* |
11
|
|
|
* Description of ServiceProvider |
12
|
|
|
* |
13
|
|
|
* @author timo |
14
|
|
|
*/ |
15
|
|
|
use PEIP\Context\XMLContext; |
16
|
|
|
|
17
|
|
|
class ServiceProvider extends \PEIP\Service\ServiceContainer |
18
|
|
|
{ |
19
|
|
|
const |
20
|
|
|
/* Headers */ |
21
|
|
|
HEADER_KEY = 'KEY', |
22
|
|
|
HEADER_SERVICE = 'SERVICE', |
23
|
|
|
HEADER_MESSAGE = 'MESSAGE', |
24
|
|
|
HEADER_NODE = 'NODE', |
25
|
|
|
HEADER_NODE_CONFIG = 'NODE_CONFIG', |
26
|
|
|
HEADER_NODE_NAME = 'NODE_NAME', |
27
|
|
|
HEADER_NODE_ID = 'NODE_ID', |
28
|
|
|
HEADER_COUNT_CONFIG = 'COUNT_CONFIG', |
29
|
|
|
/* Events */ |
30
|
|
|
EVENT_BEFORE_BUILD_NODE = 'before_build_node', |
31
|
|
|
EVENT_BUILD_NODE_SUCCESS = 'success_build_node', |
32
|
|
|
EVENT_BUILD_NODE_ERROR = 'error_build_node', |
33
|
|
|
EVENT_BEFORE_ADD_CONFIG = 'before_add_config', |
34
|
|
|
EVENT_AFTER_ADD_CONFIG = 'after_add_config', |
35
|
|
|
EVENT_BEFORE_PROVIDE_SERVICE = 'before_provide_service', |
36
|
|
|
EVENT_AFTER_PROVIDE_SERVICE = 'after_provide_service', |
37
|
|
|
EVENT_BEFORE_CREATE_SERVICE = 'before_create_service', |
38
|
|
|
EVENT_CREATE_SERVICE_SUCCESS = 'success_create_service', |
39
|
|
|
EVENT_CREATE_SERVICE_ERROR = 'error_create_service'; |
40
|
|
|
|
41
|
|
|
protected $config = [], |
42
|
|
|
$ids = [], |
43
|
|
|
$nodeBuilders = [], |
44
|
|
|
$idAttribute; |
45
|
|
|
|
46
|
|
|
public function __construct(array $config = [], $idAttribute = 'id') |
47
|
|
|
{ |
48
|
|
|
$this->idAttribute = $idAttribute; |
49
|
|
|
$this->initNodeBuilders(); |
50
|
|
|
foreach ($config as $serviceConfig) { |
51
|
|
|
$this->addConfig($serviceConfig); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* returns all registered services. |
57
|
|
|
* |
58
|
|
|
* @return array registered services |
59
|
|
|
*/ |
60
|
|
|
public function getServices() |
61
|
|
|
{ |
62
|
|
|
return $this->services; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Registers a callable as builder for given node-name. |
67
|
|
|
* |
68
|
|
|
* @implements \PEIP\INF\Context\Context |
69
|
|
|
* |
70
|
|
|
* @param string $nodeName the name of the node |
71
|
|
|
* @param callable $callable a callable which creates instances for node-name |
72
|
|
|
*/ |
73
|
|
|
public function registerNodeBuilder($nodeName, $callable) |
74
|
|
|
{ |
75
|
|
|
$this->nodeBuilders[$nodeName] = $callable; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Registers the build-methods for the main-components with this context. |
80
|
|
|
* Note: This method and subsequent registered methods of this class are |
81
|
|
|
* candidates for refactoring. Because this class has grown much to large |
82
|
|
|
* and for better design and flexibility the core builder-methods should be |
83
|
|
|
* put into a core context-plugin. |
84
|
|
|
* |
85
|
|
|
* @see XMLContext::includeContext |
86
|
|
|
*/ |
87
|
|
|
protected function initNodeBuilders() |
88
|
|
|
{ |
89
|
|
|
$builders = [ |
90
|
|
|
'service' => 'initService', |
91
|
|
|
]; |
92
|
|
|
foreach ($builders as $nodeName => $method) { |
93
|
|
|
$this->registerNodeBuilder($nodeName, [$this, $method]); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function addConfig($config) |
98
|
|
|
{ |
99
|
|
|
$this->doFireEvent( |
100
|
|
|
self::EVENT_BEFORE_ADD_CONFIG, |
101
|
|
|
[ |
102
|
|
|
self::HEADER_NODE_CONFIG => $config, |
103
|
|
|
] |
104
|
|
|
); |
105
|
|
|
$countConfig = $this->doAddConfig($config); |
106
|
|
|
$id = $this->doRegisterConfig($config); |
107
|
|
|
$this->doFireEvent( |
108
|
|
|
self::EVENT_AFTER_ADD_CONFIG, |
109
|
|
|
[ |
110
|
|
|
self::HEADER_NODE_CONFIG => $config, |
111
|
|
|
self::HEADER_NODE_ID => $id, |
112
|
|
|
self::HEADER_COUNT_CONFIG => $countConfig, |
113
|
|
|
] |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function provideService($key) |
118
|
|
|
{ |
119
|
|
|
$this->doFireEvent(self::EVENT_BEFORE_PROVIDE_SERVICE, [ |
120
|
|
|
self::HEADER_KEY => $key, ] |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
if ($this->hasService($key)) { |
124
|
|
|
$service = $this->getService($key); |
125
|
|
|
} else { |
126
|
|
|
$service = $this->createService($key); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$this->doFireEvent(self::EVENT_AFTER_PROVIDE_SERVICE, [ |
130
|
|
|
self::HEADER_KEY => $key, |
131
|
|
|
self::HEADER_SERVICE => $service, ] |
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
return $service; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
protected function createService($key) |
138
|
|
|
{ |
139
|
|
|
$this->doFireEvent(self::EVENT_BEFORE_CREATE_SERVICE, [ |
140
|
|
|
self::HEADER_KEY => $key, ] |
141
|
|
|
); |
142
|
|
|
$errorMessage = ''; |
|
|
|
|
143
|
|
|
$config = $this->getServiceConfig($key); |
144
|
|
|
|
145
|
|
|
if ($config) { |
146
|
|
|
$node = $this->buildNode($config); |
147
|
|
|
if ($node) { |
148
|
|
|
$this->setService( |
149
|
|
|
$key, |
150
|
|
|
$node |
151
|
|
|
); |
152
|
|
|
$this->doFireEvent(self::EVENT_CREATE_SERVICE_SUCCESS, [ |
153
|
|
|
self::HEADER_KEY => $key, |
154
|
|
|
self::HEADER_SERVICE => $node, |
155
|
|
|
]); |
156
|
|
|
|
157
|
|
|
return $node; |
158
|
|
|
} else { |
159
|
|
|
$errorMessage = 'COULD NOT BUILD NODE FOR KEY: '.$key; |
160
|
|
|
} |
161
|
|
|
} else { |
162
|
|
|
$errorMessage = 'NO CONFIG FOR KEY: '.$key; |
163
|
|
|
} |
164
|
|
|
$this->doFireEvent(self::EVENT_CREATE_SERVICE_ERROR, [ |
165
|
|
|
self::HEADER_KEY => $key, |
166
|
|
|
self::HEADER_MESSAGE => $errorMessage, ] |
167
|
|
|
); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function getServiceConfig($key) |
171
|
|
|
{ |
172
|
|
|
if (!isset($this->ids[$key])) { |
173
|
|
|
return false; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $this->config[$this->ids[$key]]; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Builds a specific configuration-node. Calls the build-method which |
181
|
|
|
* is registered with the node-name. If none is registered does nothing. |
182
|
|
|
* |
183
|
|
|
* @param object $config configuration-node |
184
|
|
|
* |
185
|
|
|
* @return void |
186
|
|
|
*/ |
187
|
|
|
protected function buildNode($config) |
188
|
|
|
{ |
189
|
|
|
$nodeName = (string) $config['type']; |
190
|
|
|
|
191
|
|
|
$this->doFireEvent(self::EVENT_BEFORE_BUILD_NODE, [ |
192
|
|
|
self::HEADER_NODE_CONFIG => $config, |
193
|
|
|
self::HEADER_NODE_NAME => $nodeName, |
194
|
|
|
]); |
195
|
|
|
// call the builder method registered for the node. |
196
|
|
|
if (array_key_exists($nodeName, $this->nodeBuilders)) { |
197
|
|
|
$nodeInstance = call_user_func($this->nodeBuilders[$nodeName], $config); |
198
|
|
|
if (is_object($nodeInstance)) { |
199
|
|
|
$this->doFireEvent(self::EVENT_BUILD_NODE_SUCCESS, [ |
200
|
|
|
self::HEADER_NODE_CONFIG => $config, |
201
|
|
|
self::HEADER_NODE_NAME => $nodeName, |
202
|
|
|
self::HEADER_NODE => $nodeInstance, |
203
|
|
|
]); |
204
|
|
|
|
205
|
|
|
return $nodeInstance; |
206
|
|
|
} else { |
207
|
|
|
$errorMessage = 'BUILDER RETURNED NO OBJECT FOR NODE-TYPE: '.$nodeName; |
208
|
|
|
} |
209
|
|
|
} else { |
210
|
|
|
$errorMessage = 'NO BUILDER FOUND FOR NODE-TYPE: '.$nodeName; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
$this->doFireEvent(self::EVENT_BUILD_NODE_ERROR, [ |
214
|
|
|
self::HEADER_NODE_CONFIG => $config, |
215
|
|
|
self::HEADER_NODE_NAME => $nodeName, |
216
|
|
|
self::HEADER_MESSAGE => 'COULD NOT BUILD NODE: '.$errorMessage, |
217
|
|
|
]); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
protected function getIdFromConfig($config) |
221
|
|
|
{ |
222
|
|
|
$id = ''; |
223
|
|
|
if (isset($config[$this->idAttribute])) { |
224
|
|
|
$id = trim((string) ($config[$this->idAttribute])); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $id; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
protected function getCountConfig() |
231
|
|
|
{ |
232
|
|
|
return count($this->config); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
protected function doAddConfig($config) |
236
|
|
|
{ |
237
|
|
|
$countConfig = $this->getCountConfig(); |
238
|
|
|
$this->config[$countConfig] = $config; |
239
|
|
|
|
240
|
|
|
return $countConfig; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
protected function doRegisterConfig($config) |
244
|
|
|
{ |
245
|
|
|
$id = $this->getIdFromConfig($config); |
246
|
|
|
if ($id != '') { |
247
|
|
|
$this->ids[$id] = $this->getCountConfig() - 1; |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
return $id; |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.