-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathResponse.php
More file actions
372 lines (324 loc) · 8.88 KB
/
Response.php
File metadata and controls
372 lines (324 loc) · 8.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
<?php
namespace Dingo\Api\Http;
use ArrayObject;
use UnexpectedValueException;
use Illuminate\Http\JsonResponse;
use Dingo\Api\Transformer\Binding;
use Dingo\Api\Event\ResponseIsMorphing;
use Dingo\Api\Event\ResponseWasMorphed;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Response as IlluminateResponse;
use Illuminate\Events\Dispatcher as EventDispatcher;
use Dingo\Api\Transformer\Factory as TransformerFactory;
use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
class Response extends IlluminateResponse
{
/**
* The exception that triggered the error response.
*
* @var \Exception
*/
public $exception;
/**
* Transformer binding instance.
*
* @var \Dingo\Api\Transformer\Binding
*/
protected $binding;
/**
* Array of registered formatters.
*
* @var array
*/
protected static $formatters = [];
/**
* Transformer factory instance.
*
* @var \Dingo\Api\Transformer\TransformerFactory
*/
protected static $transformer;
/**
* Event dispatcher instance.
*
* @var \Illuminate\Events\Dispatcher
*/
protected static $events;
/**
* Create a new response instance.
*
* @param mixed $content
* @param int $status
* @param array $headers
* @param \Dingo\Api\Transformer\Binding $binding
*
* @return void
*/
public function __construct($content, $status = 200, $headers = [], Binding $binding = null)
{
$this->binding = $binding;
parent::__construct($content, $status, $headers);
}
/**
* Make an API response from an existing Illuminate response.
*
* @param \Illuminate\Http\Response $old
*
* @return \Dingo\Api\Http\Response
*/
public static function makeFromExisting(IlluminateResponse $old)
{
$new = static::create($old->getOriginalContent(), $old->getStatusCode());
$new->headers = $old->headers;
return $new;
}
/**
* Make an API response from an existing JSON response.
*
* @param \Illuminate\Http\JsonResponse $json
*
* @return \Dingo\Api\Http\Response
*/
public static function makeFromJson(JsonResponse $json)
{
$new = static::create(json_decode($json->getContent(), true), $json->getStatusCode());
$new->headers = $json->headers;
return $new;
}
/**
* Morph the API response to the appropriate format.
*
* @param string $format
*
* @return \Dingo\Api\Http\Response
*/
public function morph($format = 'json')
{
$this->content = $this->getOriginalContent();
$this->fireMorphingEvent();
if (isset(static::$transformer) && static::$transformer->transformableResponse($this->content)) {
$this->content = static::$transformer->transform($this->content);
}
$formatter = static::getFormatter($format);
$defaultContentType = $this->headers->get('Content-Type');
$this->headers->set('Content-Type', $formatter->getContentType());
$this->fireMorphedEvent();
if ($this->content instanceof EloquentModel) {
$this->content = $formatter->formatEloquentModel($this->content);
} elseif ($this->content instanceof EloquentCollection) {
$this->content = $formatter->formatEloquentCollection($this->content);
} elseif (is_array($this->content) || $this->content instanceof ArrayObject || $this->content instanceof Arrayable) {
$this->content = $formatter->formatArray($this->content);
} else {
$this->headers->set('Content-Type', $defaultContentType);
}
return $this;
}
/**
* Fire the morphed event.
*
* @return void
*/
protected function fireMorphedEvent()
{
if (! static::$events) {
return;
}
static::$events->fire(new ResponseWasMorphed($this, $this->content));
}
/**
* Fire the morphing event.
*
* @return void
*/
protected function fireMorphingEvent()
{
if (! static::$events) {
return;
}
static::$events->fire(new ResponseIsMorphing($this, $this->content));
}
/**
* {@inheritdoc}
*/
public function setContent($content)
{
// Attempt to set the content string, if we encounter an unexpected value
// then we most likely have an object that cannot be type cast. In that
// case we'll simply leave the content as null and set the original
// content value and continue.
try {
if ($this->binding) {
$this->original = $this->content = $content;
return $this;
}
return parent::setContent($content);
} catch (UnexpectedValueException $exception) {
$this->original = $content;
return $this;
}
}
/**
* Set the event dispatcher instance.
*
* @param \Illuminate\Events\Dispatcher $events
*
* @return void
*/
public static function setEventDispatcher(EventDispatcher $events)
{
static::$events = $events;
}
/**
* Get the formatter based on the requested format type.
*
* @param string $format
*
* @throws \RuntimeException
*
* @return \Dingo\Api\Http\Response\Format\Format
*/
public static function getFormatter($format)
{
if (! static::hasFormatter($format)) {
throw new NotAcceptableHttpException('Unable to format response according to Accept header.');
}
return static::$formatters[$format];
}
/**
* Determine if a response formatter has been registered.
*
* @param string $format
*
* @return bool
*/
public static function hasFormatter($format)
{
return isset(static::$formatters[$format]);
}
/**
* Set the response formatters.
*
* @param array $formatters
*
* @return void
*/
public static function setFormatters(array $formatters)
{
static::$formatters = $formatters;
}
/**
* Add a response formatter.
*
* @param string $key
* @param \Dingo\Api\Http\Response\Format\Format $formatter
*
* @return void
*/
public static function addFormatter($key, $formatter)
{
static::$formatters[$key] = $formatter;
}
/**
* Set the transformer factory instance.
*
* @param \Dingo\Api\Transformer\Factory $transformer
*
* @return void
*/
public static function setTransformer(TransformerFactory $transformer)
{
static::$transformer = $transformer;
}
/**
* Get the transformer instance.
*
* @return \Dingo\Api\Transformer\Factory
*/
public static function getTransformer()
{
return static::$transformer;
}
/**
* Add a meta key and value pair.
*
* @param string $key
* @param mixed $value
*
* @return \Dingo\Api\Http\Response
*/
public function addMeta($key, $value)
{
$this->binding->addMeta($key, $value);
return $this;
}
/**
* Add a meta key and value pair.
*
* @param string $key
* @param mixed $value
*
* @return \Dingo\Api\Http\Response
*/
public function meta($key, $value)
{
return $this->addMeta($key, $value);
}
/**
* Set the meta data for the response.
*
* @param array $meta
*
* @return \Dingo\Api\Http\Response
*/
public function setMeta(array $meta)
{
$this->binding->setMeta($meta);
return $this;
}
/**
* Get the meta data for the response.
*
* @return array
*/
public function getMeta()
{
return $this->binding->getMeta();
}
/**
* Add a cookie to the response.
*
* @param \Symfony\Component\HttpFoundation\Cookie|mixed $cookie
*
* @return \Dingo\Api\Http\Response
*/
public function cookie($cookie)
{
return $this->withCookie($cookie);
}
/**
* Add a header to the response.
*
* @param string $key
* @param string $value
* @param bool $replace
*
* @return \Dingo\Api\Http\Response
*/
public function withHeader($key, $value, $replace = true)
{
return $this->header($key, $value, $replace);
}
/**
* Set the response status code.
*
* @param int $statusCode
*
* @return \Dingo\Api\Http\Response
*/
public function statusCode($statusCode)
{
return $this->setStatusCode($statusCode);
}
}