1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Smile\HTTPlugRecordAndReplayPlugin; |
6
|
|
|
|
7
|
|
|
use Http\Client\Common\Plugin; |
8
|
|
|
use Http\Client\Common\Plugin\Cache\Generator\CacheKeyGenerator; |
9
|
|
|
use Http\Client\Common\Plugin\Exception\RewindStreamException; |
10
|
|
|
use Http\Promise\FulfilledPromise; |
11
|
|
|
use Http\Promise\Promise; |
12
|
|
|
use Http\Promise\RejectedPromise; |
13
|
|
|
use Psr\Http\Message\RequestInterface; |
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
15
|
|
|
use Psr\Http\Message\StreamFactoryInterface; |
16
|
|
|
use Psr\SimpleCache\CacheInterface; |
17
|
|
|
|
18
|
|
|
class RecordAndReplayPlugin implements Plugin |
19
|
|
|
{ |
20
|
|
|
/** @var CacheInterface */ |
21
|
|
|
private $cachePool; |
22
|
|
|
|
23
|
|
|
/** @var CacheKeyGenerator */ |
24
|
|
|
private $cacheKeyGenerator; |
25
|
|
|
|
26
|
|
|
/** @var StreamFactoryInterface */ |
27
|
|
|
private $streamFactory; |
28
|
|
|
|
29
|
|
|
/** @var bool */ |
30
|
|
|
private $isRecording; |
31
|
|
|
|
32
|
3 |
|
public function __construct( |
33
|
|
|
CacheInterface $cachePool, |
34
|
|
|
CacheKeyGenerator $cacheKeyGenerator, |
35
|
|
|
StreamFactoryInterface $streamFactory, |
36
|
|
|
bool $isRecording = false |
37
|
|
|
) { |
38
|
3 |
|
$this->cachePool = $cachePool; |
39
|
3 |
|
$this->cacheKeyGenerator = $cacheKeyGenerator; |
40
|
3 |
|
$this->streamFactory = $streamFactory; |
41
|
3 |
|
$this->isRecording = $isRecording; |
42
|
3 |
|
} |
43
|
|
|
|
44
|
3 |
|
public function handleRequest(RequestInterface $request, callable $next, callable $first): Promise |
45
|
|
|
{ |
46
|
3 |
|
$recordKey = hash('sha1', $this->cacheKeyGenerator->generate($request)); |
47
|
3 |
|
if (!$this->isRecording) { |
48
|
3 |
|
$cachedRecord = $this->cachePool->get($recordKey); |
49
|
3 |
|
if ($cachedRecord === null) { |
50
|
1 |
|
return new RejectedPromise(new NoRecordException($recordKey, $request)); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
return new FulfilledPromise($this->createResponseFromRecord($cachedRecord)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $next($request)->then(function (ResponseInterface $response) use ($recordKey) { |
57
|
2 |
|
$bodyStream = $response->getBody(); |
58
|
2 |
|
$body = $bodyStream->__toString(); |
59
|
2 |
|
if ($bodyStream->isSeekable()) { |
60
|
2 |
|
$bodyStream->rewind(); |
61
|
|
|
} else { |
62
|
|
|
$response = $response->withBody($this->streamFactory->createStream($body)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$record = [ |
66
|
2 |
|
'response' => $response, |
67
|
2 |
|
'body' => $body, |
68
|
|
|
]; |
69
|
2 |
|
$this->cachePool->set($recordKey, $record); |
70
|
|
|
|
71
|
2 |
|
return $response; |
72
|
2 |
|
}); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param array $record |
77
|
|
|
* |
78
|
|
|
* @return ResponseInterface |
79
|
|
|
*/ |
80
|
2 |
|
private function createResponseFromRecord(array $record): ResponseInterface |
81
|
|
|
{ |
82
|
|
|
/** @var ResponseInterface $response */ |
83
|
2 |
|
$response = $record['response']; |
84
|
2 |
|
$stream = $this->streamFactory->createStream($record['body']); |
85
|
|
|
|
86
|
|
|
try { |
87
|
2 |
|
$stream->rewind(); |
88
|
|
|
} catch (\Exception $e) { |
89
|
|
|
throw new RewindStreamException('Cannot rewind stream.', 0, $e); |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
$response = $response->withBody($stream); |
93
|
|
|
|
94
|
2 |
|
return $response; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|