|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Phiremock. |
|
4
|
|
|
* |
|
5
|
|
|
* Phiremock is free software: you can redistribute it and/or modify |
|
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
|
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
8
|
|
|
* (at your option) any later version. |
|
9
|
|
|
* |
|
10
|
|
|
* Phiremock is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* |
|
15
|
|
|
* You should have received a copy of the GNU General Public License |
|
16
|
|
|
* along with Phiremock. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Mcustiel\Phiremock\Server\Factory; |
|
20
|
|
|
|
|
21
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
22
|
|
|
use Mcustiel\Phiremock\Common\Utils\FileSystem; |
|
23
|
|
|
use Mcustiel\Phiremock\Factory as PhiremockFactory; |
|
24
|
|
|
use Mcustiel\Phiremock\Server\Actions\ActionLocator; |
|
25
|
|
|
use Mcustiel\Phiremock\Server\Actions\ActionsFactory; |
|
26
|
|
|
use Mcustiel\Phiremock\Server\Http\Implementation\FastRouterHandler; |
|
27
|
|
|
use Mcustiel\Phiremock\Server\Http\Implementation\ReactPhpServer; |
|
28
|
|
|
use Mcustiel\Phiremock\Server\Http\ServerInterface; |
|
29
|
|
|
use Mcustiel\Phiremock\Server\Model\ExpectationStorage; |
|
30
|
|
|
use Mcustiel\Phiremock\Server\Model\Implementation\ExpectationAutoStorage; |
|
31
|
|
|
use Mcustiel\Phiremock\Server\Model\Implementation\RequestAutoStorage; |
|
32
|
|
|
use Mcustiel\Phiremock\Server\Model\Implementation\ScenarioAutoStorage; |
|
33
|
|
|
use Mcustiel\Phiremock\Server\Model\RequestStorage; |
|
34
|
|
|
use Mcustiel\Phiremock\Server\Model\ScenarioStorage; |
|
35
|
|
|
use Mcustiel\Phiremock\Server\Utils\DataStructures\StringObjectArrayMap; |
|
36
|
|
|
use Mcustiel\Phiremock\Server\Utils\FileExpectationsLoader; |
|
37
|
|
|
use Mcustiel\Phiremock\Server\Utils\GuzzlePsr18Client; |
|
38
|
|
|
use Mcustiel\Phiremock\Server\Utils\HomePathService; |
|
39
|
|
|
use Mcustiel\Phiremock\Server\Utils\RequestExpectationComparator; |
|
40
|
|
|
use Mcustiel\Phiremock\Server\Utils\RequestToExpectationMapper; |
|
41
|
|
|
use Mcustiel\Phiremock\Server\Utils\ResponseStrategyLocator; |
|
42
|
|
|
use Mcustiel\Phiremock\Server\Utils\Strategies\HttpResponseStrategy; |
|
43
|
|
|
use Mcustiel\Phiremock\Server\Utils\Strategies\ProxyResponseStrategy; |
|
44
|
|
|
use Mcustiel\Phiremock\Server\Utils\Strategies\RegexResponseStrategy; |
|
45
|
|
|
use Monolog\Handler\StreamHandler; |
|
46
|
|
|
use Monolog\Logger; |
|
47
|
|
|
use Psr\Http\Client\ClientInterface; |
|
48
|
|
|
use Psr\Log\LoggerInterface; |
|
49
|
|
|
|
|
50
|
|
|
class Factory |
|
51
|
|
|
{ |
|
52
|
|
|
/** @var PhiremockFactory */ |
|
53
|
|
|
private $phiremockFactory; |
|
54
|
|
|
|
|
55
|
|
|
/** @var StringObjectArrayMap */ |
|
56
|
|
|
private $factoryCache; |
|
57
|
|
|
|
|
58
|
|
|
public function __construct(PhiremockFactory $factory) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->phiremockFactory = $factory; |
|
61
|
|
|
$this->factoryCache = new StringObjectArrayMap(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public static function createDefault(): self |
|
65
|
|
|
{ |
|
66
|
|
|
return new self(new PhiremockFactory()); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function createFileSystemService(): FileSystem |
|
70
|
|
|
{ |
|
71
|
|
|
if (!$this->factoryCache->has('fileSystem')) { |
|
72
|
|
|
$this->factoryCache->set('fileSystem', new FileSystem()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this->factoryCache->get('fileSystem'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function createLogger(): LoggerInterface |
|
79
|
|
|
{ |
|
80
|
|
|
if (!$this->factoryCache->has('logger')) { |
|
81
|
|
|
$logger = new Logger('stdoutLogger'); |
|
82
|
|
|
$logLevel = IS_DEBUG_MODE ? \Monolog\Logger::DEBUG : \Monolog\Logger::INFO; |
|
83
|
|
|
$logger->pushHandler(new StreamHandler(STDOUT, $logLevel)); |
|
84
|
|
|
$this->factoryCache->set('logger', $logger); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return $this->factoryCache->get('logger'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function createHttpResponseStrategy(): HttpResponseStrategy |
|
91
|
|
|
{ |
|
92
|
|
|
if (!$this->factoryCache->has('httpResponseStrategy')) { |
|
93
|
|
|
$this->factoryCache->set( |
|
94
|
|
|
'httpResponseStrategy', |
|
95
|
|
|
new HttpResponseStrategy( |
|
96
|
|
|
$this->createScenarioStorage(), |
|
97
|
|
|
$this->createLogger() |
|
98
|
|
|
) |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $this->factoryCache->get('httpResponseStrategy'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function createRegexResponseStrategy(): RegexResponseStrategy |
|
106
|
|
|
{ |
|
107
|
|
|
if (!$this->factoryCache->has('regexResponseStrategy')) { |
|
108
|
|
|
$this->factoryCache->set( |
|
109
|
|
|
'regexResponseStrategy', |
|
110
|
|
|
new RegexResponseStrategy( |
|
111
|
|
|
$this->createScenarioStorage(), |
|
112
|
|
|
$this->createLogger() |
|
113
|
|
|
) |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $this->factoryCache->get('regexResponseStrategy'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function createProxyResponseStrategy(): ProxyResponseStrategy |
|
121
|
|
|
{ |
|
122
|
|
|
if (!$this->factoryCache->has('proxyResponseStrategy')) { |
|
123
|
|
|
$this->factoryCache->set( |
|
124
|
|
|
'proxyResponseStrategy', |
|
125
|
|
|
new ProxyResponseStrategy( |
|
126
|
|
|
$this->createHttpClient(), |
|
127
|
|
|
$this->createScenarioStorage(), |
|
128
|
|
|
$this->createLogger() |
|
129
|
|
|
) |
|
130
|
|
|
); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
return $this->factoryCache->get('proxyResponseStrategy'); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function createResponseStrategyLocator(): ResponseStrategyLocator |
|
137
|
|
|
{ |
|
138
|
|
|
if (!$this->factoryCache->has('responseStrategyLocator')) { |
|
139
|
|
|
$this->factoryCache->set( |
|
140
|
|
|
'responseStrategyLocator', |
|
141
|
|
|
new ResponseStrategyLocator($this) |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return $this->factoryCache->get('responseStrategyLocator'); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function createRequestsRouter(): FastRouterHandler |
|
149
|
|
|
{ |
|
150
|
|
|
if (!$this->factoryCache->has('router')) { |
|
151
|
|
|
$this->factoryCache->set( |
|
152
|
|
|
'router', |
|
153
|
|
|
new FastRouterHandler($this->createActionLocator(), $this->createLogger()) |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $this->factoryCache->get('router'); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function createHomePathService(): HomePathService |
|
161
|
|
|
{ |
|
162
|
|
|
if (!$this->factoryCache->has('homePathService')) { |
|
163
|
|
|
$this->factoryCache->set( |
|
164
|
|
|
'homePathService', |
|
165
|
|
|
new HomePathService() |
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $this->factoryCache->get('homePathService'); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function createHttpServer(): ServerInterface |
|
173
|
|
|
{ |
|
174
|
|
|
if (!$this->factoryCache->has('httpServer')) { |
|
175
|
|
|
$this->factoryCache->set( |
|
176
|
|
|
'httpServer', |
|
177
|
|
|
new ReactPhpServer($this->createRequestsRouter(), $this->createLogger()) |
|
178
|
|
|
); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
return $this->factoryCache->get('httpServer'); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
public function createExpectationStorage(): ExpectationStorage |
|
185
|
|
|
{ |
|
186
|
|
|
if (!$this->factoryCache->has('expectationsStorage')) { |
|
187
|
|
|
$this->factoryCache->set( |
|
188
|
|
|
'expectationsStorage', |
|
189
|
|
|
new ExpectationAutoStorage() |
|
190
|
|
|
); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return $this->factoryCache->get('expectationsStorage'); |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
public function createExpectationBackup(): ExpectationStorage |
|
197
|
|
|
{ |
|
198
|
|
|
if (!$this->factoryCache->has('expectationsBackup')) { |
|
199
|
|
|
$this->factoryCache->set( |
|
200
|
|
|
'expectationsBackup', |
|
201
|
|
|
new ExpectationAutoStorage() |
|
202
|
|
|
); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return $this->factoryCache->get('expectationsBackup'); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
public function createRequestStorage(): RequestStorage |
|
209
|
|
|
{ |
|
210
|
|
|
if (!$this->factoryCache->has('requestsStorage')) { |
|
211
|
|
|
$this->factoryCache->set( |
|
212
|
|
|
'requestsStorage', |
|
213
|
|
|
new RequestAutoStorage() |
|
214
|
|
|
); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
return $this->factoryCache->get('requestsStorage'); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
public function createScenarioStorage(): ScenarioStorage |
|
221
|
|
|
{ |
|
222
|
|
|
if (!$this->factoryCache->has('scenariosStorage')) { |
|
223
|
|
|
$this->factoryCache->set( |
|
224
|
|
|
'scenariosStorage', |
|
225
|
|
|
new ScenarioAutoStorage() |
|
226
|
|
|
); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
return $this->factoryCache->get('scenariosStorage'); |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
public function createRequestExpectationComparator(): RequestExpectationComparator |
|
233
|
|
|
{ |
|
234
|
|
|
if (!$this->factoryCache->has('requestExpectationComparator')) { |
|
235
|
|
|
$this->factoryCache->set( |
|
236
|
|
|
'requestExpectationComparator', |
|
237
|
|
|
new RequestExpectationComparator( |
|
238
|
|
|
$this->createScenarioStorage(), |
|
239
|
|
|
$this->createLogger() |
|
240
|
|
|
) |
|
241
|
|
|
); |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
return $this->factoryCache->get('requestExpectationComparator'); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
public function createFileExpectationsLoader(): FileExpectationsLoader |
|
248
|
|
|
{ |
|
249
|
|
|
if (!$this->factoryCache->has('fileExpectationsLoader')) { |
|
250
|
|
|
$this->factoryCache->set( |
|
251
|
|
|
'fileExpectationsLoader', |
|
252
|
|
|
new FileExpectationsLoader( |
|
253
|
|
|
$this->phiremockFactory->createArrayToExpectationConverterLocator(), |
|
254
|
|
|
$this->createExpectationStorage(), |
|
255
|
|
|
$this->createExpectationBackup(), |
|
256
|
|
|
$this->createLogger() |
|
257
|
|
|
) |
|
258
|
|
|
); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
return $this->factoryCache->get('fileExpectationsLoader'); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
public function createActionLocator(): ActionLocator |
|
265
|
|
|
{ |
|
266
|
|
|
if (!$this->factoryCache->has('actionLocator')) { |
|
267
|
|
|
$this->factoryCache->set( |
|
268
|
|
|
'actionLocator', |
|
269
|
|
|
new ActionLocator($this->createActionFactory()) |
|
270
|
|
|
); |
|
271
|
|
|
} |
|
272
|
|
|
|
|
273
|
|
|
return $this->factoryCache->get('actionLocator'); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
public function createActionFactory(): ActionsFactory |
|
277
|
|
|
{ |
|
278
|
|
|
if (!$this->factoryCache->has('actionFactory')) { |
|
279
|
|
|
$this->factoryCache->set( |
|
280
|
|
|
'actionFactory', |
|
281
|
|
|
new ActionsFactory($this, $this->phiremockFactory) |
|
282
|
|
|
); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
return $this->factoryCache->get('actionFactory'); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
public function createRequestToExpectationMapper(): RequestToExpectationMapper |
|
289
|
|
|
{ |
|
290
|
|
|
if (!$this->factoryCache->has('requestToExpectationMapper')) { |
|
291
|
|
|
$this->factoryCache->set( |
|
292
|
|
|
'requestToExpectationMapper', |
|
293
|
|
|
new RequestToExpectationMapper( |
|
294
|
|
|
$this->phiremockFactory->createArrayToExpectationConverterLocator(), |
|
295
|
|
|
$this->createLogger() |
|
296
|
|
|
) |
|
297
|
|
|
); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
return $this->factoryCache->get('requestToExpectationMapper'); |
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
public function createHttpClient(): ClientInterface |
|
304
|
|
|
{ |
|
305
|
|
|
if (!class_exists(GuzzleClient::class, true)) { |
|
306
|
|
|
throw new \Exception('A default http client implementation is needed. Please extend the factory or install Guzzle Http Client v6'); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
return new GuzzlePsr18Client(); |
|
310
|
|
|
} |
|
311
|
|
|
} |
|
312
|
|
|
|