1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pachico\SlimSwoole\Bridge; |
4
|
|
|
|
5
|
|
|
use Slim\App; |
6
|
|
|
use Psr\Http\Message\ResponseInterface; |
7
|
|
|
use swoole_http_response; |
8
|
|
|
use Dflydev\FigCookies\SetCookies; |
9
|
|
|
|
10
|
|
|
class ResponseMerger implements ResponseMergerInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var App |
14
|
|
|
*/ |
15
|
|
|
private $app; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param App $app |
19
|
18 |
|
*/ |
20
|
|
|
public function __construct(App $app) |
21
|
18 |
|
{ |
22
|
18 |
|
$this->app = $app; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Response $response |
27
|
|
|
* @param swoole_http_response $swooleResponse |
28
|
|
|
* |
29
|
|
|
* @return swoole_http_response |
30
|
18 |
|
*/ |
31
|
|
|
public function mergeToSwoole( |
32
|
|
|
ResponseInterface $response, |
33
|
|
|
swoole_http_response $swooleResponse |
34
|
18 |
|
): swoole_http_response { |
35
|
|
|
$container = $this->app->getContainer(); |
36
|
18 |
|
|
37
|
18 |
|
$settings = $container->get('settings'); |
38
|
3 |
|
if (isset($settings['addContentLengthHeader']) && $settings['addContentLengthHeader'] == true) { |
39
|
3 |
|
$size = $response->getBody()->getSize(); |
40
|
3 |
|
if ($size !== null) { |
41
|
|
|
$swooleResponse->header('Content-Length', (string) $size); |
42
|
|
|
} |
43
|
|
|
} |
44
|
18 |
|
|
45
|
3 |
|
if (!empty($response->getHeaders())) { |
46
|
3 |
|
$this->setCookies($swooleResponse, $response); |
47
|
|
|
|
48
|
|
|
$response = $response->withoutHeader('Set-Cookie'); |
49
|
|
|
|
50
|
18 |
|
foreach ($response->getHeaders() as $key => $headerArray) { |
51
|
|
|
$swooleResponse->header($key, implode('; ', $headerArray)); |
52
|
18 |
|
} |
53
|
9 |
|
} |
54
|
3 |
|
|
55
|
|
|
$swooleResponse->status($response->getStatusCode(), $response->getReasonPhrase()); |
|
|
|
|
56
|
|
|
|
57
|
9 |
|
if ($response->getBody()->getSize() > 0) { |
58
|
|
|
if ($response->getBody()->isSeekable()) { |
59
|
|
|
$response->getBody()->rewind(); |
60
|
18 |
|
} |
61
|
|
|
|
62
|
|
|
$swooleResponse->write($response->getBody()->getContents()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $swooleResponse; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function setCookies($swooleResponse, $response) |
69
|
|
|
{ |
70
|
|
|
if (!$response->hasHeader('Set-Cookie')) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$setCookies = SetCookies::fromSetCookieStrings($response->getHeader('Set-Cookie')); |
75
|
|
|
foreach ($setCookies->getAll() as $setCookie) { |
76
|
|
|
$swooleResponse->cookie( |
77
|
|
|
$setCookie->getName(), |
78
|
|
|
$setCookie->getValue(), |
79
|
|
|
$setCookie->getExpires(), |
80
|
|
|
$setCookie->getPath(), |
81
|
|
|
$setCookie->getDomain(), |
82
|
|
|
$setCookie->getSecure(), |
83
|
|
|
$setCookie->getHttpOnly() |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.