1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Server\Configuration\LoggerXmlConfiguration |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Johann Zelger <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link https://github.com/appserver-io/server |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Server\Configuration; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Server\Interfaces\LoggerConfigurationInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class LoggerXmlConfiguration |
27
|
|
|
* |
28
|
|
|
* @author Johann Zelger <[email protected]> |
29
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
31
|
|
|
* @link https://github.com/appserver-io/server |
32
|
|
|
* @link http://www.appserver.io |
33
|
|
|
*/ |
34
|
|
|
class LoggerXmlConfiguration implements LoggerConfigurationInterface |
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Holds the name of the logger |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $name; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Holds the type of the logger |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
protected $type; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* 'Holds the loggers channel name |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $channel; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Holds all handlers defined for logger |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $handlers; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Holds all processors defined for logger |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected $processors; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Constructs config |
69
|
|
|
* |
70
|
|
|
* @param \SimpleXMLElement $node The simple xml element used to build config |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function __construct(\SimpleXMLElement $node) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
// prepare properties |
75
|
|
|
$this->name = (string)$node->attributes()->name; |
76
|
|
|
$this->type = (string)$node->attributes()->type; |
77
|
|
|
if (isset($node->attributes()->channel)) { |
78
|
|
|
$this->channel = (string)$node->attributes()->channel; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// prepare handlers |
82
|
|
|
$this->handlers = $this->prepareHandlers($node); |
83
|
|
|
// prepare processors |
84
|
|
|
$this->processors = $this->prepareProcessors($node); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns name |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getName() |
93
|
|
|
{ |
94
|
|
|
return $this->name; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns type |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function getType() |
103
|
|
|
{ |
104
|
|
|
return $this->type; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns channel |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getChannel() |
113
|
|
|
{ |
114
|
|
|
return $this->channel; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns defined handlers for logger |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function getHandlers() |
123
|
|
|
{ |
124
|
|
|
return $this->handlers; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Returns defined processors for logger |
129
|
|
|
* |
130
|
|
|
* @return array |
131
|
|
|
*/ |
132
|
|
|
public function getProcessors() |
133
|
|
|
{ |
134
|
|
|
return $this->processors; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Prepares handlers array for config |
139
|
|
|
* |
140
|
|
|
* @param \SimpleXMLElement $node The xml node to prepare for |
141
|
|
|
* |
142
|
|
|
* @return array |
143
|
|
|
*/ |
144
|
|
|
public function prepareHandlers($node) |
145
|
|
|
{ |
146
|
|
|
$handlers = array(); |
147
|
|
|
if ($node->handlers) { |
148
|
|
|
foreach ($node->handlers->handler as $handlerNode) { |
149
|
|
|
// build up params |
150
|
|
|
$params = array(); |
151
|
|
|
|
152
|
|
|
foreach ($handlerNode->params->param as $paramNode) { |
153
|
|
|
$paramName = (string) $paramNode->attributes()->name; |
154
|
|
|
$paramNodes = $handlerNode->xpath(".//param[@name='$paramName']"); |
155
|
|
|
$params[$paramName] = (string) array_shift($paramNodes); |
156
|
|
|
} |
157
|
|
|
// build up formatter infos if exists |
158
|
|
|
if (isset($handlerNode->formatter)) { |
159
|
|
|
$formatterType = (string)$handlerNode->formatter->attributes()->type; |
160
|
|
|
$formatterParams = array(); |
161
|
|
|
foreach ($handlerNode->formatter->params->param as $paramNode) { |
162
|
|
|
$paramName = (string)$paramNode->attributes()->name; |
163
|
|
|
$paramsNameArray = $handlerNode->xpath(".//param[@name='$paramName']"); |
164
|
|
|
$formatterParams[$paramName] = (string)array_shift($paramsNameArray); |
165
|
|
|
} |
166
|
|
|
// setup formatter info |
167
|
|
|
$handlers[(string)$handlerNode->attributes()->type]['formatter'] = array( |
168
|
|
|
'type' => $formatterType, |
169
|
|
|
'params' => $formatterParams |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
// set up handler infos |
173
|
|
|
$handlers[(string)$handlerNode->attributes()->type]['params'] = $params; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
return $handlers; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Prepares processors array for config |
181
|
|
|
* |
182
|
|
|
* @param \SimpleXMLElement $node The xml node to prepare for |
183
|
|
|
* |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
|
|
public function prepareProcessors($node) |
187
|
|
|
{ |
188
|
|
|
$processors = array(); |
189
|
|
|
if ($node->processors) { |
190
|
|
|
foreach ($node->processors->processor as $processorNode) { |
191
|
|
|
$processors[(string)$processorNode->attributes()->type] = (string)$processorNode->attributes()->type; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
return $processors; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.