1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Server\Configuration\UpstreamXmlConfiguration |
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\UpstreamConfigurationInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Class UpstreamXmlConfiguration |
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 UpstreamXmlConfiguration implements UpstreamConfigurationInterface |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* Holds the name of the upstream node |
38
|
|
|
* |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
public $name; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Holds the type of the upstream node |
45
|
|
|
* |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
public $type; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Holds the servers for the upstream node |
52
|
|
|
* |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
public $servers; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Constructs config |
59
|
|
|
* |
60
|
|
|
* @param \SimpleXMLElement $node The simple xml element used to build config |
61
|
|
|
*/ |
62
|
|
|
public function __construct(\SimpleXMLElement $node) |
63
|
|
|
{ |
64
|
|
|
// prepare properties |
65
|
|
|
$this->name = (string)$node->attributes()->name; |
66
|
|
|
$this->type = (string)$node->attributes()->type; |
67
|
|
|
|
68
|
|
|
if (isset($node->attributes()->channel)) { |
69
|
|
|
$this->channel = (string)$node->attributes()->channel; |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// prepare handlers |
73
|
|
|
$this->servers = $this->prepareServers($node); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Prepares server nodes configured for upstream |
78
|
|
|
* |
79
|
|
|
* @param \SimpleXMLElement $node The xml node for servers to prepare |
80
|
|
|
* |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
protected function prepareServers(\SimpleXMLElement $node) |
84
|
|
|
{ |
85
|
|
|
$servers = array(); |
86
|
|
|
foreach ($node->servers->server as $serverNode) { |
87
|
|
|
$name = (string)$serverNode->attributes()->name; |
88
|
|
|
$type = (string)$serverNode->attributes()->type; |
89
|
|
|
$params = array(); |
90
|
|
View Code Duplication |
foreach ($serverNode->params->param as $paramNode) { |
|
|
|
|
91
|
|
|
$paramName = (string)$paramNode->attributes()->name; |
92
|
|
|
$paramType = (string)$paramNode->attributes()->type; |
93
|
|
|
$paramValue = (string)$paramNode; |
94
|
|
|
// check if type boolen and transform true and false strings to int |
95
|
|
|
if ($paramType === 'boolean') { |
96
|
|
|
$paramValue = str_replace(array('true', 'false', '1', '0'), array(1,0,1,0 ), $paramValue); |
97
|
|
|
} |
98
|
|
|
// set correct value type |
99
|
|
|
settype($paramValue, $paramType); |
100
|
|
|
$params[$paramName] = $paramValue; |
101
|
|
|
} |
102
|
|
|
$servers[$name] = array( |
103
|
|
|
'name' => $name, |
104
|
|
|
'type' => $type, |
105
|
|
|
'params' => $params |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
return $servers; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Returns name |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function getName() |
117
|
|
|
{ |
118
|
|
|
return $this->name; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Returns type |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function getType() |
127
|
|
|
{ |
128
|
|
|
return $this->type; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns servers |
133
|
|
|
* |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function getServers() |
137
|
|
|
{ |
138
|
|
|
return $this->servers; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: