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