1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Response parser |
4
|
|
|
* User: moyo |
5
|
|
|
* Date: 2018/5/30 |
6
|
|
|
* Time: 9:58 AM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Carno\Web\Chips\Dispatcher; |
10
|
|
|
|
11
|
|
|
use Carno\HTTP\Standard\Response; |
12
|
|
|
use Carno\HTTP\Standard\ServerRequest; |
13
|
|
|
use Carno\HTTP\Standard\Streams\Body; |
14
|
|
|
use Carno\Web\Exception\RespondingCodecException; |
15
|
|
|
use Google\Protobuf\Internal\Message; |
|
|
|
|
16
|
|
|
use ArrayObject; |
17
|
|
|
|
18
|
|
|
trait Responsive |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @param ServerRequest $request |
22
|
|
|
* @param Response $response |
23
|
|
|
* @param mixed $result |
24
|
|
|
* @return Response |
25
|
|
|
*/ |
26
|
|
|
protected function responding(ServerRequest $request, Response $response, $result) : Response |
27
|
|
|
{ |
28
|
|
|
switch (gettype($result)) { |
29
|
|
|
case 'array': |
30
|
|
|
if (($cc = $this->codec($request)) === 'json') { |
|
|
|
|
31
|
|
|
return $this->ccJson( |
32
|
|
|
$response, |
33
|
|
|
json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
throw new RespondingCodecException('Unknown responding codec'); |
37
|
|
|
case 'string': |
38
|
|
|
return $response->withBody(new Body($result)); |
39
|
|
|
case 'object': |
40
|
|
|
if ($result instanceof Response) { |
41
|
|
|
return $result; |
42
|
|
|
} elseif ($result instanceof Message) { |
43
|
|
|
return $this->ccJson($response, $result->serializeToJsonString()); |
44
|
|
|
} elseif ($result instanceof ArrayObject) { |
45
|
|
|
return $this->ccJson( |
46
|
|
|
$response, |
47
|
|
|
json_encode((array)$result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) |
48
|
|
|
); |
49
|
|
|
} elseif (method_exists($result, '__toString')) { |
50
|
|
|
return $response->withBody(new Body((string)$result)); |
51
|
|
|
} |
52
|
|
|
throw new RespondingCodecException('Unacceptable object'); |
53
|
|
|
case 'NULL': |
54
|
|
|
return $response; |
55
|
|
|
default: |
56
|
|
|
return $response->withBody(new Body((string)$result)); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param ServerRequest $request |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
private function codec(ServerRequest $request) : string |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
// TODO currently forced to "json" |
67
|
|
|
return 'json'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Response $response |
72
|
|
|
* @param string $data |
73
|
|
|
* @return Response |
74
|
|
|
*/ |
75
|
|
|
private function ccJson(Response $response, string $data) : Response |
76
|
|
|
{ |
77
|
|
|
$response = $response->withBody(new Body($data)); |
78
|
|
|
$response->withHeader('Content-Type', 'application/json'); |
79
|
|
|
return $response; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
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