|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Http\Middleware; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
7
|
|
|
use React\Http\Io\HttpBodyStream; |
|
8
|
|
|
use React\Promise\PromiseInterface; |
|
9
|
|
|
use function React\Promise\resolve; |
|
10
|
|
|
use function RingCentral\Psr7\stream_for; |
|
11
|
|
|
use WyriHaximus\HtmlCompress\Factory; |
|
12
|
|
|
use WyriHaximus\HtmlCompress\Parser; |
|
13
|
|
|
|
|
14
|
|
|
final class HtmlCompressMiddleware |
|
15
|
|
|
{ |
|
16
|
|
|
const MIME_TYPES = [ |
|
17
|
|
|
'text/html', |
|
18
|
|
|
'text/xhtml', |
|
19
|
|
|
]; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var Parser |
|
23
|
|
|
*/ |
|
24
|
|
|
private $compressor; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param Parser $compressor |
|
28
|
|
|
*/ |
|
29
|
5 |
|
public function __construct(Parser $compressor = null) |
|
30
|
|
|
{ |
|
31
|
5 |
|
if ($compressor === null) { |
|
32
|
5 |
|
$compressor = Factory::constructFastest(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
5 |
|
$this->compressor = $compressor; |
|
|
|
|
|
|
36
|
5 |
|
} |
|
37
|
|
|
|
|
38
|
5 |
|
public function __invoke(ServerRequestInterface $request, callable $next) |
|
39
|
|
|
{ |
|
40
|
5 |
|
$response = $next($request); |
|
41
|
|
|
|
|
42
|
5 |
|
if (!($response instanceof PromiseInterface)) { |
|
43
|
2 |
|
return resolve($this->handleResponse($response)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return $response->then(function (ResponseInterface $response) { |
|
47
|
3 |
|
return $this->handleResponse($response); |
|
48
|
3 |
|
}); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
5 |
|
private function handleResponse(ResponseInterface $response) |
|
52
|
|
|
{ |
|
53
|
5 |
|
if ($response->getBody() instanceof HttpBodyStream) { |
|
54
|
1 |
|
return $response; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
4 |
|
if (!$response->hasHeader('content-type')) { |
|
58
|
1 |
|
return $response; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
3 |
|
list($contentType) = \explode(';', $response->getHeaderLine('content-type')); |
|
62
|
3 |
|
if (!\in_array($contentType, self::MIME_TYPES, true)) { |
|
63
|
1 |
|
return $response; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
2 |
|
$body = (string)$response->getBody(); |
|
67
|
2 |
|
$compressedBody = $this->compressor->compress($body); |
|
68
|
|
|
|
|
69
|
2 |
|
return $response->withBody(stream_for($compressedBody))->withHeader('Content-Length', \strlen($compressedBody)); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.