1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Hhxsv5\LaravelS\Swoole; |
5
|
|
|
|
6
|
|
|
use Swoole\Http\Response as SwooleResponse; |
7
|
|
|
use Symfony\Component\HttpFoundation\Response as SymfonyResponse; |
|
|
|
|
8
|
|
|
|
9
|
|
|
abstract class Response implements ResponseInterface |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
protected $chunkLimit = 2097152; // 2 * 1024 * 1024 |
12
|
|
|
|
13
|
|
|
protected $swooleResponse; |
14
|
|
|
|
15
|
|
|
protected $laravelResponse; |
16
|
|
|
|
17
|
|
|
public function __construct(SwooleResponse $swooleResponse, SymfonyResponse $laravelResponse) |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$this->swooleResponse = $swooleResponse; |
20
|
|
|
$this->laravelResponse = $laravelResponse; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function setChunkLimit($chunkLimit) |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$this->chunkLimit = $chunkLimit; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function sendStatusCode() |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
$this->swooleResponse->status($this->laravelResponse->getStatusCode()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private function getHeaders() |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
if (method_exists($this->laravelResponse->headers, 'allPreserveCaseWithoutCookies')) { |
36
|
|
|
return $this->laravelResponse->headers->allPreserveCaseWithoutCookies(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $this->laravelResponse->headers->allPreserveCase(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function sendHeaders() |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$headers = $this->getHeaders(); |
45
|
|
|
$trailers = isset($headers['trailer']) ? $headers['trailer'] : []; |
46
|
|
|
|
47
|
|
|
foreach ($headers as $name => $values) { |
48
|
|
|
if (in_array($name, $trailers, true)) { |
49
|
|
|
continue; |
50
|
|
|
} |
51
|
|
|
if (version_compare(SWOOLE_VERSION, '4.6.0', '>=')) { |
52
|
|
|
$this->swooleResponse->header($name, $values); |
53
|
|
|
} else { |
54
|
|
|
foreach ($values as $value) { |
55
|
|
|
$this->swooleResponse->header($name, $value); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function sendTrailers() |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$headers = $this->getHeaders(); |
64
|
|
|
$trailers = isset($headers['trailer']) ? $headers['trailer'] : []; |
65
|
|
|
|
66
|
|
|
foreach ($headers as $name => $values) { |
67
|
|
|
if (!in_array($name, $trailers, true)) { |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
foreach ($values as $value) { |
72
|
|
|
$this->swooleResponse->trailer($name, $value); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function sendCookies() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$hasIsRaw = null; |
80
|
|
|
/**@var \Symfony\Component\HttpFoundation\Cookie[] $cookies */ |
|
|
|
|
81
|
|
|
$cookies = $this->laravelResponse->headers->getCookies(); |
82
|
|
|
foreach ($cookies as $cookie) { |
83
|
|
|
if ($hasIsRaw === null) { |
84
|
|
|
$hasIsRaw = method_exists($cookie, 'isRaw'); |
85
|
|
|
} |
86
|
|
|
$setCookie = $hasIsRaw && $cookie->isRaw() ? 'rawcookie' : 'cookie'; |
87
|
|
|
$this->swooleResponse->$setCookie( |
88
|
|
|
$cookie->getName(), |
89
|
|
|
(string)$cookie->getValue(), |
90
|
|
|
$cookie->getExpiresTime(), |
91
|
|
|
$cookie->getPath(), |
92
|
|
|
(string)$cookie->getDomain(), |
93
|
|
|
$cookie->isSecure(), |
94
|
|
|
$cookie->isHttpOnly() |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function send($gzip = false) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
$this->sendStatusCode(); |
102
|
|
|
$this->sendHeaders(); |
103
|
|
|
$this->sendCookies(); |
104
|
|
|
$this->sendTrailers(); |
105
|
|
|
if ($gzip) { |
106
|
|
|
$this->gzip(); |
107
|
|
|
} |
108
|
|
|
$this->sendContent(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|