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\Arrayable; |
13
|
|
|
use Illuminate\Contracts\Support\Renderable; |
14
|
|
|
use Illuminate\Contracts\Support\Responsable; |
15
|
|
|
use Illuminate\Http\Response as BaseResponse; |
16
|
|
|
use Illuminate\Support\Arr; |
17
|
|
|
use Illuminate\Support\Str; |
18
|
|
|
use Modules\Core\Contracts\Support\Boolable; |
19
|
|
|
use Modules\Core\Enums\StatusCodeEnum; |
20
|
|
|
use Modules\Core\Traits\Supports\ResponseHandleTrait; |
21
|
|
|
use SoapBox\Formatter\Formatter; |
22
|
|
|
|
23
|
|
|
class Response implements Responsable, Arrayable, Renderable, Boolable |
24
|
|
|
{ |
25
|
|
|
public $original; |
26
|
|
|
protected $response; |
27
|
|
|
protected $statusCode; |
28
|
|
|
|
29
|
|
|
use ResponseHandleTrait; |
30
|
|
|
|
31
|
|
|
public function __construct(array $response, $original = null) |
32
|
|
|
{ |
33
|
|
|
$this->original = $original; |
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
|
|
|
|
64
|
|
|
return $response; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* s |
69
|
|
|
* 允许跨域请求 |
70
|
|
|
* |
71
|
|
|
* @param \Illuminate\Http\Response $response |
72
|
|
|
* |
73
|
|
|
* @return \Illuminate\Http\Response |
74
|
|
|
*/ |
75
|
|
|
private function cors(BaseResponse $response): BaseResponse |
76
|
|
|
{ |
77
|
|
|
if (config('core.api.cors_enabled')) { |
78
|
|
|
/** @var CorsService $cors */ |
79
|
|
|
$cors = app(CorsService::class); |
80
|
|
|
$request = request(); |
81
|
|
|
|
82
|
|
|
if ($cors->isCorsRequest(request())) { |
83
|
|
|
if (!$response->headers->has('Access-Control-Allow-Origin')) { |
84
|
|
|
$response = $cors->addActualRequestHeaders($response, $request); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $response; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Create an HTTP response that represents the object. |
94
|
|
|
* |
95
|
|
|
* @param \Illuminate\Http\Request $request |
96
|
|
|
* |
97
|
|
|
* @return \Illuminate\Http\Response |
98
|
|
|
*/ |
99
|
|
|
public function toResponse($request): BaseResponse |
100
|
|
|
{ |
101
|
|
|
return $this->render(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get the instance as an array. |
106
|
|
|
* |
107
|
|
|
* @return array |
108
|
|
|
*/ |
109
|
|
|
public function toArray(): array |
110
|
|
|
{ |
111
|
|
|
return (array) Arr::get($this->response, 'data'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get the evaluated contents of the object. |
116
|
|
|
* |
117
|
|
|
* @return \Illuminate\Http\Response |
118
|
|
|
*/ |
119
|
|
|
public function render(): BaseResponse |
120
|
|
|
{ |
121
|
|
|
return $this->cors($this->format()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the true and false of the instance. |
126
|
|
|
* |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
public function toBool(): bool |
130
|
|
|
{ |
131
|
|
|
return Str::startsWith(Arr::get($this->response, 'meta.status_code'), 2); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get the instance to continue. |
136
|
|
|
* |
137
|
|
|
* @return bool |
138
|
|
|
*/ |
139
|
|
|
public function isContinue(): bool |
140
|
|
|
{ |
141
|
|
|
return Str::startsWith(Arr::get($this->response, 'meta.status_code'), [1, 2]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Return an response. |
146
|
|
|
* |
147
|
|
|
* @param array $response |
148
|
|
|
* @param $original |
149
|
|
|
* |
150
|
|
|
* @return Response |
151
|
|
|
*/ |
152
|
|
|
private static function call(array $response, $original): self |
153
|
|
|
{ |
154
|
|
|
return new self($response, $original); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param string $param |
159
|
|
|
* |
160
|
|
|
* @return mixed|void |
161
|
|
|
*/ |
162
|
|
|
public static function param(string $param) |
163
|
|
|
{ |
164
|
|
|
$request = app('Illuminate\Http\Request'); |
165
|
|
|
|
166
|
|
|
if ($request->has($param)) { |
167
|
|
|
return $request->get($param); |
168
|
|
|
} else { |
169
|
|
|
$header_param = Str::title(Str::kebab(Str::studly($param))); |
170
|
|
|
if ($request->hasHeader($header_param)) { |
171
|
|
|
return $request->header($header_param); |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|