1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: frowhy |
5
|
|
|
* Date: 2017/8/1 |
6
|
|
|
* Time: 下午3:25 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Modules\Core\Supports; |
10
|
|
|
|
11
|
|
|
use Asm89\Stack\CorsService; |
12
|
|
|
use Illuminate\Contracts\Support\{ |
13
|
|
|
Arrayable, Renderable, Responsable |
14
|
|
|
}; |
15
|
|
|
use Illuminate\Http\Response as BaseResponse; |
16
|
|
|
use Illuminate\Support\{ |
17
|
|
|
Arr, |
18
|
|
|
Str |
19
|
|
|
}; |
20
|
|
|
use Modules\Core\Contracts\Support\Boolable; |
21
|
|
|
use Modules\Core\Enums\StatusCodeEnum; |
22
|
|
|
use Modules\Core\Traits\Supports\ResponseHandleTrait; |
23
|
|
|
use SoapBox\Formatter\Formatter; |
24
|
|
|
|
25
|
|
|
class Response implements Responsable, Arrayable, Renderable, Boolable |
26
|
|
|
{ |
27
|
|
|
protected $response; |
28
|
|
|
protected $statusCode; |
29
|
|
|
|
30
|
|
|
use ResponseHandleTrait; |
31
|
|
|
|
32
|
|
|
public function __construct(array $response) |
33
|
|
|
{ |
34
|
|
|
$this->response = $response; |
35
|
|
|
$this->statusCode = $response['meta']['status_code'] ?? StatusCodeEnum::HTTP_OK; |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* 格式化响应 |
42
|
|
|
* |
43
|
|
|
* @return \Illuminate\Http\Response |
44
|
|
|
*/ |
45
|
|
|
private function format(): BaseResponse |
46
|
|
|
{ |
47
|
|
|
list($response, $statusCode) = [$this->response, $this->statusCode]; |
48
|
|
|
$formatter = Formatter::make($response, Formatter::ARR); |
49
|
|
|
$format = self::param('output_format') ?? (config('core.api.output_format')); |
50
|
|
|
$statusCode = |
51
|
|
|
(self::param('status_sync') ?? config('core.api.status_sync')) ? $statusCode : StatusCodeEnum::HTTP_OK; |
52
|
|
|
if (in_array($format, ['application/xml', 'xml'])) { |
53
|
|
|
$response = response($formatter->toXml(), $statusCode, ['Content-Type' => 'application/xml']); |
54
|
|
|
} elseif (in_array($format, ['application/x-yaml', 'yaml'])) { |
55
|
|
|
$response = response($formatter->toYaml(), $statusCode, ['Content-Type' => 'application/x-yaml']); |
56
|
|
|
} elseif (in_array($format, ['text/csv', 'csv'])) { |
57
|
|
|
$response = response($formatter->toCsv(), $statusCode, ['Content-Type' => 'text/csv']); |
58
|
|
|
} elseif (in_array($format, ['application/json', 'json'])) { |
59
|
|
|
$response = response($formatter->toJson(), $statusCode, ['Content-Type' => 'application/json']); |
60
|
|
|
} else { |
61
|
|
|
$response = response($response, $statusCode); |
62
|
|
|
} |
63
|
|
|
return $response; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* s |
68
|
|
|
* 允许跨域请求 |
69
|
|
|
* |
70
|
|
|
* @param \Illuminate\Http\Response $response |
71
|
|
|
* @return \Illuminate\Http\Response |
72
|
|
|
*/ |
73
|
|
|
private function cors(BaseResponse $response): BaseResponse |
74
|
|
|
{ |
75
|
|
|
if (config('core.api.cors_enabled')) { |
76
|
|
|
/** @var CorsService $cors */ |
77
|
|
|
$cors = app(CorsService::class); |
78
|
|
|
$request = request(); |
79
|
|
|
|
80
|
|
|
if ($cors->isCorsRequest(request())) { |
81
|
|
|
if (!$response->headers->has('Access-Control-Allow-Origin')) { |
82
|
|
|
$response = $cors->addActualRequestHeaders($response, $request); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $response; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Create an HTTP response that represents the object. |
92
|
|
|
* |
93
|
|
|
* @param \Illuminate\Http\Request $request |
94
|
|
|
* |
95
|
|
|
* @return \Illuminate\Http\Response |
96
|
|
|
*/ |
97
|
|
|
public function toResponse($request): BaseResponse |
98
|
|
|
{ |
99
|
|
|
return $this->render(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get the instance as an array. |
104
|
|
|
* |
105
|
|
|
* @return array |
106
|
|
|
*/ |
107
|
|
|
public function toArray(): array |
108
|
|
|
{ |
109
|
|
|
return (array) Arr::get($this->response, 'data'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get the evaluated contents of the object. |
114
|
|
|
* |
115
|
|
|
* @return \Illuminate\Http\Response |
116
|
|
|
*/ |
117
|
|
|
public function render(): BaseResponse |
118
|
|
|
{ |
119
|
|
|
return $this->cors($this->format()); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get the true and false of the instance. |
124
|
|
|
* |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
public function toBool(): bool |
128
|
|
|
{ |
129
|
|
|
return Str::startsWith(Arr::get($this->response, 'meta.status_code'), 2); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Return an response. |
134
|
|
|
* |
135
|
|
|
* @param array $response |
136
|
|
|
* |
137
|
|
|
* @return Response |
138
|
|
|
*/ |
139
|
|
|
private static function call(array $response): Response |
140
|
|
|
{ |
141
|
|
|
return new self($response); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public static function param(string $param) |
145
|
|
|
{ |
146
|
|
|
$request = app('Illuminate\Http\Request'); |
147
|
|
|
if ($request->has($param)) { |
148
|
|
|
return $request->get($param); |
149
|
|
|
} else { |
150
|
|
|
$header_param = Str::title(Str::kebab(Str::studly($param))); |
151
|
|
|
if ($request->hasHeader($header_param)) { |
152
|
|
|
return $request->header($header_param); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return null; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|