1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* \AppserverIo\Server\Contexts\ServerContext |
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\Contexts; |
22
|
|
|
|
23
|
|
|
use AppserverIo\Server\Exceptions\ServerException; |
24
|
|
|
use AppserverIo\Server\Interfaces\ServerConfigurationInterface; |
25
|
|
|
use AppserverIo\Server\Interfaces\ServerContextInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Class ServerContext |
29
|
|
|
* |
30
|
|
|
* @author Johann Zelger <[email protected]> |
31
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
32
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
33
|
|
|
* @link https://github.com/appserver-io/server |
34
|
|
|
* @link http://www.appserver.io |
35
|
|
|
*/ |
36
|
|
|
class ServerContext implements ServerContextInterface |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* Optionally Holds an container implementation of third party environment. |
40
|
|
|
* So every mod depending on his environment can use this as a container to transfer environment specific stuff. |
41
|
|
|
* |
42
|
|
|
* @var mixed |
43
|
|
|
*/ |
44
|
|
|
protected $container; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* All logger instances will be hold here. |
48
|
|
|
* Every logger instance has to be a PSR compatible |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
protected $loggers; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Holds upstream instances |
56
|
|
|
* |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $upstreams; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Holds the config instance |
63
|
|
|
* |
64
|
|
|
* @var \AppserverIo\Server\Interfaces\ServerConfigurationInterface $serverConfig |
65
|
|
|
*/ |
66
|
|
|
protected $serverConfig; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Initialises the server context |
70
|
|
|
* |
71
|
|
|
* @param \AppserverIo\Server\Interfaces\ServerConfigurationInterface $serverConfig The servers configuration |
72
|
|
|
* |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function init(ServerConfigurationInterface $serverConfig) |
76
|
|
|
{ |
77
|
|
|
// set configuration |
78
|
|
|
$this->serverConfig = $serverConfig; |
79
|
|
|
// init loggers array |
80
|
|
|
$this->loggers = array(); |
81
|
|
|
// init upstreams array |
82
|
|
|
$this->upstreams = array(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns the server config instance |
87
|
|
|
* |
88
|
|
|
* @return \AppserverIo\Server\Interfaces\ServerConfigurationInterface The server config instance |
89
|
|
|
*/ |
90
|
|
|
public function getServerConfig() |
91
|
|
|
{ |
92
|
|
|
return $this->serverConfig; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Injects the stream context object for the server socket to be bound with. |
97
|
|
|
* |
98
|
|
|
* @param resource $streamContext The stream context instance to inject |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
|
|
public function injectStreamContext($streamContext) |
103
|
|
|
{ |
104
|
|
|
$this->streamContext = $streamContext; |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns the corresponding stream context for server socket to be bound with. |
109
|
|
|
* |
110
|
|
|
* @return resource |
111
|
|
|
*/ |
112
|
|
|
public function getStreamContext() |
113
|
|
|
{ |
114
|
|
|
return $this->streamContext; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Injects the container for further use in specific server mods etc... |
119
|
|
|
* |
120
|
|
|
* @param mixed $container An container instance for third party environment |
121
|
|
|
* |
122
|
|
|
* @return void |
123
|
|
|
*/ |
124
|
|
|
public function injectContainer($container) |
125
|
|
|
{ |
126
|
|
|
$this->container = $container; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns the container instance |
131
|
|
|
* |
132
|
|
|
* @return mixed The container instance for third party environment |
133
|
|
|
*/ |
134
|
|
|
public function getContainer() |
135
|
|
|
{ |
136
|
|
|
return $this->container; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Injects upstream instances |
141
|
|
|
* |
142
|
|
|
* @param array $upstreams The array of upstream instances |
143
|
|
|
* |
144
|
|
|
* @return void |
145
|
|
|
*/ |
146
|
|
|
public function injectUpstreams(array $upstreams) |
147
|
|
|
{ |
148
|
|
|
// iterate loggers to collection hashmap |
149
|
|
|
foreach ($upstreams as $upstreamName => $upstreamInstance) { |
150
|
|
|
$this->upstreams[$upstreamName] = $upstreamInstance; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns specific upstream by given name from upstream collection |
156
|
|
|
* |
157
|
|
|
* @param string $upstreamName The upstreams name to find |
158
|
|
|
* |
159
|
|
|
* @throws ServerException |
160
|
|
|
* @return \AppserverIo\Server\Interfaces\UpstreamInterface |
161
|
|
|
*/ |
162
|
|
|
public function getUpstream($upstreamName) |
163
|
|
|
{ |
164
|
|
|
if (!isset($this->upstreams[$upstreamName])) { |
165
|
|
|
// throw exception |
166
|
|
|
throw new ServerException("Upstream '$upstreamName' does not exist.", 500); |
167
|
|
|
} |
168
|
|
|
return $this->upstreams[$upstreamName]; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Injects compatible logger instances |
173
|
|
|
* |
174
|
|
|
* @param array<\Psr\Log\LoggerInterface> $loggers The array of logger instances |
175
|
|
|
* |
176
|
|
|
* @return void |
177
|
|
|
*/ |
178
|
|
|
public function injectLoggers(array $loggers) |
179
|
|
|
{ |
180
|
|
|
// iterate loggers to collection hashmap |
181
|
|
|
foreach ($loggers as $loggerName => $loggerInstance) { |
182
|
|
|
$this->loggers[$loggerName] = $loggerInstance; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Queries if the requested logger type is registered or not. |
188
|
|
|
* |
189
|
|
|
* @param string $loggerType The logger type we want to query |
190
|
|
|
* |
191
|
|
|
* @return boolean TRUE if the logger is registered, else FALSE |
192
|
|
|
*/ |
193
|
|
|
public function hasLogger($loggerType) |
194
|
|
|
{ |
195
|
|
|
return isset($this->loggers[$loggerType]); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Returns the logger instance |
200
|
|
|
* |
201
|
|
|
* @param string $loggerType the logger's type to get |
202
|
|
|
* |
203
|
|
|
* @return \Psr\Log\LoggerInterface|null The logger instance |
204
|
|
|
* @throws \AppserverIo\Server\Exceptions\ServerException |
205
|
|
|
*/ |
206
|
|
|
public function getLogger($loggerType = self::DEFAULT_LOGGER_TYPE) |
207
|
|
|
{ |
208
|
|
|
// check if logger is set |
209
|
|
|
if ($this->hasLogger($loggerType)) { |
210
|
|
|
// return logger |
211
|
|
|
return $this->loggers[$loggerType]; |
212
|
|
|
} |
213
|
|
|
// throw exception |
214
|
|
|
throw new ServerException("Logger name '$loggerType' does not exist.", 500); |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
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: