1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SwooleTW\Http\Transformers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Response as IlluminateResponse; |
6
|
|
|
use Swoole\Http\Response as SwooleResponse; |
7
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response as SymfonyResponse; |
9
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
10
|
|
|
|
11
|
|
|
class Response |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var \Swoole\Http\Response |
15
|
|
|
*/ |
16
|
|
|
protected $swooleResponse; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var \Illuminate\Http\Response |
20
|
|
|
*/ |
21
|
|
|
protected $illuminateResponse; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Make a response. |
25
|
|
|
* |
26
|
|
|
* @param $illuminateResponse |
27
|
|
|
* @param \Swoole\Http\Response $swooleResponse |
28
|
|
|
* @return \SwooleTW\Http\Server\Response |
|
|
|
|
29
|
|
|
*/ |
30
|
|
|
public static function make($illuminateResponse, SwooleResponse $swooleResponse) |
31
|
|
|
{ |
32
|
|
|
return new static($illuminateResponse, $swooleResponse); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Response constructor. |
37
|
|
|
* |
38
|
|
|
* @param mixed $illuminateResponse |
39
|
|
|
* @param \Swoole\Http\Response $swooleResponse |
40
|
|
|
*/ |
41
|
|
|
public function __construct($illuminateResponse, SwooleResponse $swooleResponse) |
42
|
|
|
{ |
43
|
|
|
$this->setIlluminateResponse($illuminateResponse); |
44
|
|
|
$this->setSwooleResponse($swooleResponse); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Sends HTTP headers and content. |
49
|
|
|
* |
50
|
|
|
* @throws \InvalidArgumentException |
51
|
|
|
*/ |
52
|
|
|
public function send() |
53
|
|
|
{ |
54
|
|
|
$this->sendHeaders(); |
55
|
|
|
$this->sendContent(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Sends HTTP headers. |
60
|
|
|
* |
61
|
|
|
* @throws \InvalidArgumentException |
62
|
|
|
*/ |
63
|
|
|
protected function sendHeaders() |
64
|
|
|
{ |
65
|
|
|
$illuminateResponse = $this->getIlluminateResponse(); |
66
|
|
|
|
67
|
|
|
/* RFC2616 - 14.18 says all Responses need to have a Date */ |
68
|
|
|
if (! $illuminateResponse->headers->has('Date')) { |
69
|
|
|
$illuminateResponse->setDate(\DateTime::createFromFormat('U', time())); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// headers |
73
|
|
|
// allPreserveCaseWithoutCookies() doesn't exist before Laravel 5.3 |
74
|
|
|
$headers = $illuminateResponse->headers->allPreserveCase(); |
75
|
|
|
if (isset($headers['Set-Cookie'])) { |
76
|
|
|
unset($headers['Set-Cookie']); |
77
|
|
|
} |
78
|
|
|
foreach ($headers as $name => $values) { |
79
|
|
|
foreach ($values as $value) { |
80
|
|
|
$this->swooleResponse->header($name, $value); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// status |
85
|
|
|
$this->swooleResponse->status($illuminateResponse->getStatusCode()); |
86
|
|
|
|
87
|
|
|
// cookies |
88
|
|
|
foreach ($illuminateResponse->headers->getCookies() as $cookie) { |
89
|
|
|
// may need to consider rawcookie |
90
|
|
|
$this->swooleResponse->cookie( |
91
|
|
|
$cookie->getName(), $cookie->getValue(), |
92
|
|
|
$cookie->getExpiresTime(), $cookie->getPath(), |
93
|
|
|
$cookie->getDomain(), $cookie->isSecure(), |
94
|
|
|
$cookie->isHttpOnly() |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Sends HTTP content. |
101
|
|
|
*/ |
102
|
|
|
protected function sendContent() |
103
|
|
|
{ |
104
|
|
|
$illuminateResponse = $this->getIlluminateResponse(); |
105
|
|
|
|
106
|
|
|
if ($illuminateResponse instanceof StreamedResponse && |
|
|
|
|
107
|
|
|
property_exists($illuminateResponse, 'output') |
108
|
|
|
) { |
109
|
|
|
$this->swooleResponse->end($illuminateResponse->output); |
110
|
|
|
} elseif ($illuminateResponse instanceof BinaryFileResponse) { |
|
|
|
|
111
|
|
|
$this->swooleResponse->sendfile($illuminateResponse->getFile()->getPathname()); |
112
|
|
|
} else { |
113
|
|
|
$this->swooleResponse->end($illuminateResponse->getContent()); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param \Swoole\Http\Response $swooleResponse |
119
|
|
|
* @return \SwooleTW\Http\Server\Response |
120
|
|
|
*/ |
121
|
|
|
protected function setSwooleResponse(SwooleResponse $swooleResponse) |
122
|
|
|
{ |
123
|
|
|
$this->swooleResponse = $swooleResponse; |
124
|
|
|
|
125
|
|
|
return $this; |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return \Swoole\Http\Response |
130
|
|
|
*/ |
131
|
|
|
public function getSwooleResponse() |
132
|
|
|
{ |
133
|
|
|
return $this->swooleResponse; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param mixed illuminateResponse |
138
|
|
|
* @return \SwooleTW\Http\Server\Response |
139
|
|
|
*/ |
140
|
|
|
protected function setIlluminateResponse($illuminateResponse) |
141
|
|
|
{ |
142
|
|
|
if (! $illuminateResponse instanceof SymfonyResponse) { |
143
|
|
|
$content = (string) $illuminateResponse; |
144
|
|
|
$illuminateResponse = new IlluminateResponse($content); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$this->illuminateResponse = $illuminateResponse; |
148
|
|
|
|
149
|
|
|
return $this; |
|
|
|
|
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return \Illuminate\Http\Response |
154
|
|
|
*/ |
155
|
|
|
public function getIlluminateResponse() |
156
|
|
|
{ |
157
|
|
|
return $this->illuminateResponse; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths