|
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)) { |
|
49
|
|
|
continue; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
foreach ($values as $value) { |
|
53
|
|
|
$this->swooleResponse->header($name, $value); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function sendTrailers() |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
$headers = $this->getHeaders(); |
|
61
|
|
|
$trailers = isset($headers["trailer"]) ? $headers["trailer"] : []; |
|
62
|
|
|
|
|
63
|
|
|
foreach ($headers as $name => $values) { |
|
64
|
|
|
if (!in_array($name, $trailers)) { |
|
65
|
|
|
continue; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
foreach ($values as $value) { |
|
69
|
|
|
$this->swooleResponse->trailer($name, $value); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function sendCookies() |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
$hasIsRaw = null; |
|
77
|
|
|
/**@var \Symfony\Component\HttpFoundation\Cookie[] $cookies */ |
|
|
|
|
|
|
78
|
|
|
$cookies = $this->laravelResponse->headers->getCookies(); |
|
79
|
|
|
foreach ($cookies as $cookie) { |
|
80
|
|
|
if ($hasIsRaw === null) { |
|
81
|
|
|
$hasIsRaw = method_exists($cookie, 'isRaw'); |
|
82
|
|
|
} |
|
83
|
|
|
$setCookie = $hasIsRaw && $cookie->isRaw() ? 'rawcookie' : 'cookie'; |
|
84
|
|
|
$this->swooleResponse->$setCookie( |
|
85
|
|
|
$cookie->getName(), |
|
86
|
|
|
$cookie->getValue(), |
|
87
|
|
|
$cookie->getExpiresTime(), |
|
88
|
|
|
$cookie->getPath(), |
|
89
|
|
|
$cookie->getDomain(), |
|
90
|
|
|
$cookie->isSecure(), |
|
91
|
|
|
$cookie->isHttpOnly() |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function send($gzip = false) |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
$this->sendStatusCode(); |
|
99
|
|
|
$this->sendHeaders(); |
|
100
|
|
|
$this->sendCookies(); |
|
101
|
|
|
$this->sendTrailers(); |
|
102
|
|
|
if ($gzip) { |
|
103
|
|
|
$this->gzip(); |
|
104
|
|
|
} |
|
105
|
|
|
$this->sendContent(); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|