| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Http;
|
|
|
4 |
|
|
|
5 |
use ArrayObject;
|
|
|
6 |
use Illuminate\Contracts\Support\Arrayable;
|
|
|
7 |
use Illuminate\Contracts\Support\Jsonable;
|
|
|
8 |
use Illuminate\Contracts\Support\Renderable;
|
|
|
9 |
use Illuminate\Support\Traits\Macroable;
|
|
|
10 |
use InvalidArgumentException;
|
|
|
11 |
use JsonSerializable;
|
|
|
12 |
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
|
|
|
13 |
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
|
|
|
14 |
|
|
|
15 |
class Response extends SymfonyResponse
|
|
|
16 |
{
|
|
|
17 |
use ResponseTrait, Macroable {
|
|
|
18 |
Macroable::__call as macroCall;
|
|
|
19 |
}
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Create a new HTTP response.
|
|
|
23 |
*
|
|
|
24 |
* @param mixed $content
|
|
|
25 |
* @param int $status
|
|
|
26 |
* @param array $headers
|
|
|
27 |
* @return void
|
|
|
28 |
*
|
|
|
29 |
* @throws \InvalidArgumentException
|
|
|
30 |
*/
|
|
|
31 |
public function __construct($content = '', $status = 200, array $headers = [])
|
|
|
32 |
{
|
|
|
33 |
$this->headers = new ResponseHeaderBag($headers);
|
|
|
34 |
|
|
|
35 |
$this->setContent($content);
|
|
|
36 |
$this->setStatusCode($status);
|
|
|
37 |
$this->setProtocolVersion('1.0');
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Set the content on the response.
|
|
|
42 |
*
|
|
|
43 |
* @param mixed $content
|
|
|
44 |
* @return $this
|
|
|
45 |
*
|
|
|
46 |
* @throws \InvalidArgumentException
|
|
|
47 |
*/
|
|
|
48 |
public function setContent(mixed $content): static
|
|
|
49 |
{
|
|
|
50 |
$this->original = $content;
|
|
|
51 |
|
|
|
52 |
// If the content is "JSONable" we will set the appropriate header and convert
|
|
|
53 |
// the content to JSON. This is useful when returning something like models
|
|
|
54 |
// from routes that will be automatically transformed to their JSON form.
|
|
|
55 |
if ($this->shouldBeJson($content)) {
|
|
|
56 |
$this->header('Content-Type', 'application/json');
|
|
|
57 |
|
|
|
58 |
$content = $this->morphToJson($content);
|
|
|
59 |
|
|
|
60 |
if ($content === false) {
|
|
|
61 |
throw new InvalidArgumentException(json_last_error_msg());
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
// If this content implements the "Renderable" interface then we will call the
|
|
|
66 |
// render method on the object so we will avoid any "__toString" exceptions
|
|
|
67 |
// that might be thrown and have their errors obscured by PHP's handling.
|
|
|
68 |
elseif ($content instanceof Renderable) {
|
|
|
69 |
$content = $content->render();
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
parent::setContent($content);
|
|
|
73 |
|
|
|
74 |
return $this;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Determine if the given content should be turned into JSON.
|
|
|
79 |
*
|
|
|
80 |
* @param mixed $content
|
|
|
81 |
* @return bool
|
|
|
82 |
*/
|
|
|
83 |
protected function shouldBeJson($content)
|
|
|
84 |
{
|
|
|
85 |
return $content instanceof Arrayable ||
|
|
|
86 |
$content instanceof Jsonable ||
|
|
|
87 |
$content instanceof ArrayObject ||
|
|
|
88 |
$content instanceof JsonSerializable ||
|
|
|
89 |
is_array($content);
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* Morph the given content into JSON.
|
|
|
94 |
*
|
|
|
95 |
* @param mixed $content
|
|
|
96 |
* @return string
|
|
|
97 |
*/
|
|
|
98 |
protected function morphToJson($content)
|
|
|
99 |
{
|
|
|
100 |
if ($content instanceof Jsonable) {
|
|
|
101 |
return $content->toJson();
|
|
|
102 |
} elseif ($content instanceof Arrayable) {
|
|
|
103 |
return json_encode($content->toArray());
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
return json_encode($content);
|
|
|
107 |
}
|
|
|
108 |
}
|