1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Phprest; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use League\BooBoo\BooBoo; |
7
|
|
|
use League\Container\Container; |
8
|
|
|
use League\Container\ContainerInterface; |
9
|
|
|
use League\Event\Emitter as EventEmitter; |
10
|
|
|
use League\Event\EmitterInterface as EventEmitterInterface; |
11
|
|
|
use League\Route\Strategy\StrategyInterface; |
12
|
|
|
use Phprest\ErrorHandler\Formatter\JsonXml as JsonXmlFormatter; |
13
|
|
|
use Phprest\ErrorHandler\Handler\Log as LogHandler; |
14
|
|
|
use Phprest\Router\RouteCollection; |
15
|
|
|
use Phprest\Router\Strategy as RouterStrategy; |
16
|
|
|
use Phprest\Service\Hateoas\Config as HateoasConfig; |
17
|
|
|
use Phprest\Service\Hateoas\Service as HateoasService; |
18
|
|
|
use Phprest\Service\Logger\Config as LoggerConfig; |
19
|
|
|
use Phprest\Service\Logger\Service as LoggerService; |
20
|
|
|
|
21
|
|
|
class Config |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $vendor; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $apiVersion; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
protected $debug = false; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ContainerInterface |
40
|
|
|
*/ |
41
|
|
|
protected $container; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var RouteCollection |
45
|
|
|
*/ |
46
|
|
|
protected $router; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var EventEmitterInterface |
50
|
|
|
*/ |
51
|
|
|
protected $eventEmitter; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var BooBoo |
55
|
|
|
*/ |
56
|
|
|
protected $errorHandler; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var HateoasConfig |
60
|
|
|
*/ |
61
|
|
|
protected $hateoasConfig; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var HateoasService |
65
|
|
|
*/ |
66
|
|
|
protected $hateoasService; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var LoggerConfig |
70
|
|
|
*/ |
71
|
|
|
protected $loggerConfig; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var LoggerService |
75
|
|
|
*/ |
76
|
|
|
protected $loggerService; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var LogHandler |
80
|
|
|
*/ |
81
|
|
|
protected $logHandler; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $vendor |
85
|
|
|
* @param string $apiVersion |
86
|
|
|
* @param bool $debug |
87
|
|
|
*/ |
88
|
84 |
|
public function __construct($vendor, $apiVersion, $debug = false) |
89
|
|
|
{ |
90
|
84 |
|
if (! preg_match('#^' . Application::API_VERSION_REG_EXP . '$#', (string) $apiVersion)) { |
91
|
11 |
|
throw new InvalidArgumentException('Api version is not valid'); |
92
|
|
|
} |
93
|
|
|
|
94
|
73 |
|
$this->vendor = $vendor; |
95
|
73 |
|
$this->apiVersion = $apiVersion; |
96
|
73 |
|
$this->debug = $debug; |
97
|
|
|
|
98
|
73 |
|
$this->setContainer(new Container()); |
99
|
73 |
|
$this->setRouter(new RouteCollection($this->getContainer())); |
100
|
73 |
|
$this->setEventEmitter(new EventEmitter()); |
101
|
73 |
|
$this->setHateoasConfig(new HateoasConfig($debug)); |
102
|
73 |
|
$this->setHateoasService(new HateoasService()); |
103
|
73 |
|
$this->setLoggerConfig(new LoggerConfig('phprest')); |
104
|
73 |
|
$this->setLoggerService(new LoggerService()); |
105
|
73 |
|
$this->setLogHandler(new LogHandler()); |
106
|
73 |
|
$this->setRouterStrategy(new RouterStrategy($this->getContainer())); |
107
|
|
|
|
108
|
73 |
|
$errorHandler = new BooBoo([new JsonXmlFormatter($this)]); |
109
|
73 |
|
$errorHandler->silenceAllErrors(false); |
110
|
73 |
|
$errorHandler->treatErrorsAsExceptions(true); |
111
|
|
|
|
112
|
73 |
|
$this->setErrorHandler($errorHandler); |
113
|
73 |
|
} |
114
|
|
|
|
115
|
12 |
|
public function getVendor(): string |
116
|
|
|
{ |
117
|
12 |
|
return $this->vendor; |
118
|
|
|
} |
119
|
|
|
|
120
|
72 |
|
public function getApiVersion(): string |
121
|
|
|
{ |
122
|
72 |
|
return $this->apiVersion; |
123
|
|
|
} |
124
|
|
|
|
125
|
12 |
|
public function isDebug(): bool |
126
|
|
|
{ |
127
|
12 |
|
return $this->debug; |
128
|
|
|
} |
129
|
|
|
|
130
|
73 |
|
public function setContainer(ContainerInterface $container): self |
131
|
|
|
{ |
132
|
73 |
|
$this->container = $container; |
133
|
|
|
|
134
|
73 |
|
return $this; |
135
|
|
|
} |
136
|
|
|
|
137
|
74 |
|
public function getContainer(): ContainerInterface |
138
|
|
|
{ |
139
|
74 |
|
return $this->container; |
140
|
|
|
} |
141
|
|
|
|
142
|
73 |
|
public function setRouter(RouteCollection $router): self |
143
|
|
|
{ |
144
|
73 |
|
$this->router = $router; |
145
|
|
|
|
146
|
73 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
73 |
|
public function setRouterStrategy(StrategyInterface $strategy): self |
150
|
|
|
{ |
151
|
73 |
|
$this->router->setStrategy($strategy); |
152
|
|
|
|
153
|
73 |
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
9 |
|
public function getRouter(): RouteCollection |
157
|
|
|
{ |
158
|
9 |
|
return $this->router; |
159
|
|
|
} |
160
|
|
|
|
161
|
73 |
|
public function setEventEmitter(EventEmitterInterface $eventEmitter): self |
162
|
|
|
{ |
163
|
73 |
|
$this->eventEmitter = $eventEmitter; |
164
|
|
|
|
165
|
73 |
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
9 |
|
public function getEventEmitter(): EventEmitterInterface |
169
|
|
|
{ |
170
|
9 |
|
return $this->eventEmitter; |
171
|
|
|
} |
172
|
|
|
|
173
|
73 |
|
public function setErrorHandler(BooBoo $errorHandler): self |
174
|
|
|
{ |
175
|
73 |
|
$this->errorHandler = $errorHandler; |
176
|
|
|
|
177
|
73 |
|
return $this; |
178
|
|
|
} |
179
|
|
|
|
180
|
9 |
|
public function getErrorHandler(): BooBoo |
181
|
|
|
{ |
182
|
9 |
|
return $this->errorHandler; |
183
|
|
|
} |
184
|
|
|
|
185
|
73 |
|
public function setHateoasConfig(HateoasConfig $config): self |
186
|
|
|
{ |
187
|
73 |
|
$this->hateoasConfig = $config; |
188
|
|
|
|
189
|
73 |
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
12 |
|
public function getHateoasConfig(): HateoasConfig |
193
|
|
|
{ |
194
|
12 |
|
return $this->hateoasConfig; |
195
|
|
|
} |
196
|
|
|
|
197
|
73 |
|
public function setHateoasService(HateoasService $service): self |
198
|
|
|
{ |
199
|
73 |
|
$this->hateoasService = $service; |
200
|
|
|
|
201
|
73 |
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
12 |
|
public function getHateoasService(): HateoasService |
205
|
|
|
{ |
206
|
12 |
|
return $this->hateoasService; |
207
|
|
|
} |
208
|
|
|
|
209
|
73 |
|
public function setLoggerConfig(LoggerConfig $config): self |
210
|
|
|
{ |
211
|
73 |
|
$this->loggerConfig = $config; |
212
|
|
|
|
213
|
73 |
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
9 |
|
public function getLoggerConfig(): LoggerConfig |
217
|
|
|
{ |
218
|
9 |
|
return $this->loggerConfig; |
219
|
|
|
} |
220
|
|
|
|
221
|
73 |
|
public function setLoggerService(LoggerService $service): self |
222
|
|
|
{ |
223
|
73 |
|
$this->loggerService = $service; |
224
|
|
|
|
225
|
73 |
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
9 |
|
public function getLoggerService(): LoggerService |
229
|
|
|
{ |
230
|
9 |
|
return $this->loggerService; |
231
|
|
|
} |
232
|
|
|
|
233
|
73 |
|
public function setLogHandler(LogHandler $logHandler): self |
234
|
|
|
{ |
235
|
73 |
|
$this->logHandler = $logHandler; |
236
|
|
|
|
237
|
73 |
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
9 |
|
public function getLogHandler(): LogHandler |
241
|
|
|
{ |
242
|
9 |
|
return $this->logHandler; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|