1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psr7Middlewares\Middleware; |
4
|
|
|
|
5
|
|
|
use Psr7Middlewares\Middleware; |
6
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Negotiation\EncodingNegotiator as Negotiator; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Middleware that returns the client preferred encoding. |
12
|
|
|
*/ |
13
|
|
|
class EncodingNegotiator |
14
|
|
|
{ |
15
|
|
|
const KEY = 'ENCODING'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array Available encodings |
19
|
|
|
*/ |
20
|
|
|
private $encodings = [ |
21
|
|
|
'gzip', |
22
|
|
|
'deflate', |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Returns the encoding. |
27
|
|
|
* |
28
|
|
|
* @param ServerRequestInterface $request |
29
|
|
|
* |
30
|
|
|
* @return string|null |
31
|
|
|
*/ |
32
|
|
|
public static function getEncoding(ServerRequestInterface $request) |
33
|
|
|
{ |
34
|
|
|
return Middleware::getAttribute($request, self::KEY); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor. Defines de available encodings. |
39
|
|
|
* |
40
|
|
|
* @param array $encodings |
41
|
|
|
*/ |
42
|
|
|
public function __construct(array $encodings = null) |
43
|
|
|
{ |
44
|
|
|
if ($encodings !== null) { |
45
|
|
|
$this->encodings($encodings); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Configure the available encodings. |
51
|
|
|
* |
52
|
|
|
* @param array $encodings |
53
|
|
|
* |
54
|
|
|
* @return self |
55
|
|
|
*/ |
56
|
|
|
public function encodings(array $encodings) |
57
|
|
|
{ |
58
|
|
|
$this->encodings = $encodings; |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Execute the middleware. |
65
|
|
|
* |
66
|
|
|
* @param ServerRequestInterface $request |
67
|
|
|
* @param ResponseInterface $response |
68
|
|
|
* @param callable $next |
69
|
|
|
* |
70
|
|
|
* @return ResponseInterface |
71
|
|
|
*/ |
72
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next) |
73
|
|
|
{ |
74
|
|
|
$request = Middleware::setAttribute($request, self::KEY, $this->getFromHeader($request)); |
75
|
|
|
|
76
|
|
|
return $next($request, $response); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Returns the encoding format using the Accept-Encoding header. |
81
|
|
|
* |
82
|
|
|
* @return null|string |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
private function getFromHeader(ServerRequestInterface $request) |
|
|
|
|
85
|
|
|
{ |
86
|
|
|
$accept = $request->getHeaderLine('Accept-Encoding'); |
87
|
|
|
|
88
|
|
|
if (empty($accept)) { |
89
|
|
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$encoding = (new Negotiator())->getBest($accept, $this->encodings); |
93
|
|
|
|
94
|
|
|
if ($encoding) { |
95
|
|
|
return $encoding->getValue(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.