1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* thanks to https://github.com/limingxinleo. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Canvas\Http; |
8
|
|
|
|
9
|
|
|
use Exception; |
10
|
|
|
use Phalcon\Http\Cookie; |
|
|
|
|
11
|
|
|
use Phalcon\Http\Response as PhResponse; |
|
|
|
|
12
|
|
|
use swoole_http_response; |
|
|
|
|
13
|
|
|
use Throwable; |
14
|
|
|
|
15
|
|
|
class SwooleResponse extends Response |
16
|
|
|
{ |
17
|
|
|
protected $response; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Set the swoole response object. |
21
|
|
|
* |
22
|
|
|
* @param swoole_http_response $response |
23
|
|
|
* |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
public function init(swoole_http_response $response) : void |
27
|
|
|
{ |
28
|
|
|
$this->response = $response; |
29
|
|
|
$this->_sent = false; |
|
|
|
|
30
|
|
|
$this->_content = null; |
|
|
|
|
31
|
|
|
$this->setStatusCode(200); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Send the response. |
36
|
|
|
* |
37
|
|
|
* @return PhResponse |
38
|
|
|
*/ |
39
|
|
|
public function send() : PhResponse |
40
|
|
|
{ |
41
|
|
|
if ($this->_sent) { |
42
|
|
|
throw new Exception('Response was already sent'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->_sent = true; |
|
|
|
|
46
|
|
|
// get phalcon headers |
47
|
|
|
$headers = $this->getHeaders(); |
48
|
|
|
|
49
|
|
|
foreach ($headers->toArray() as $key => $val) { |
50
|
|
|
//if the key has spaces this breaks postman, so we remove this headers |
51
|
|
|
//example: HTTP/1.1 200 OK || HTTP/1.1 401 Unauthorized |
52
|
|
|
if (!preg_match('/\s/', $key)) { |
53
|
|
|
$this->response->header($key, $val); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** @var Cookies $cookies */ |
58
|
|
|
$cookies = $this->getCookies(); |
59
|
|
|
if ($cookies) { |
|
|
|
|
60
|
|
|
/** @var Cookie $cookie */ |
61
|
|
|
foreach ($cookies->getCookies() as $cookie) { |
62
|
|
|
$this->response->cookie( |
63
|
|
|
$cookie->getName(), |
64
|
|
|
$cookie->getValue(), |
65
|
|
|
$cookie->getExpiration(), |
66
|
|
|
$cookie->getPath(), |
67
|
|
|
$cookie->getDomain(), |
68
|
|
|
$cookie->getSecure(), |
69
|
|
|
$cookie->getHttpOnly() |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
//set swoole response |
75
|
|
|
$this->response->status($this->getStatusCode()); |
76
|
|
|
$this->response->end($this->_content); |
77
|
|
|
|
78
|
|
|
//reset di |
79
|
|
|
$this->resetDi(); |
80
|
|
|
|
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Handle the exception we throw from our api. |
86
|
|
|
* |
87
|
|
|
* @param Throwable $e |
88
|
|
|
* |
89
|
|
|
* @return Response |
90
|
|
|
*/ |
91
|
|
|
public function handleException(Throwable $e) : Response |
92
|
|
|
{ |
93
|
|
|
//reset di |
94
|
|
|
$response = parent::handleException($e); |
95
|
|
|
$this->resetDi(); |
96
|
|
|
|
97
|
|
|
return $response; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Given Swoole behavior we need to reset the DI and Close the DB connection |
102
|
|
|
* What happens if you don't do this? You will cache the DI request and always get the same info ;). |
103
|
|
|
* |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
|
|
protected function resetDi() : void |
107
|
|
|
{ |
108
|
|
|
$this->_sent = false; |
|
|
|
|
109
|
|
|
$this->getDi()->get('db')->close(); |
110
|
|
|
$this->getDi()->reset(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
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