<?php namespace Illuminate\Http; use ArrayObject; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; use Illuminate\Contracts\Support\Renderable; use Illuminate\Support\Traits\Macroable; use InvalidArgumentException; use JsonSerializable; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; use Symfony\Component\HttpFoundation\ResponseHeaderBag; class Response extends SymfonyResponse { use ResponseTrait, Macroable { Macroable::__call as macroCall; } /** * Create a new HTTP response. * * @param mixed $content * @param int $status * @param array $headers * @return void * * @throws \InvalidArgumentException */ public function __construct($content = '', $status = 200, array $headers = []) { $this->headers = new ResponseHeaderBag($headers); $this->setContent($content); $this->setStatusCode($status); $this->setProtocolVersion('1.0'); } /** * Set the content on the response. * * @param mixed $content * @return $this * * @throws \InvalidArgumentException */ public function setContent(mixed $content): static { $this->original = $content; // If the content is "JSONable" we will set the appropriate header and convert // the content to JSON. This is useful when returning something like models // from routes that will be automatically transformed to their JSON form. if ($this->shouldBeJson($content)) { $this->header('Content-Type', 'application/json'); $content = $this->morphToJson($content); if ($content === false) { throw new InvalidArgumentException(json_last_error_msg()); } } // If this content implements the "Renderable" interface then we will call the // render method on the object so we will avoid any "__toString" exceptions // that might be thrown and have their errors obscured by PHP's handling. elseif ($content instanceof Renderable) { $content = $content->render(); } parent::setContent($content); return $this; } /** * Determine if the given content should be turned into JSON. * * @param mixed $content * @return bool */ protected function shouldBeJson($content) { return $content instanceof Arrayable || $content instanceof Jsonable || $content instanceof ArrayObject || $content instanceof JsonSerializable || is_array($content); } /** * Morph the given content into JSON. * * @param mixed $content * @return string */ protected function morphToJson($content) { if ($content instanceof Jsonable) { return $content->toJson(); } elseif ($content instanceof Arrayable) { return json_encode($content->toArray()); } return json_encode($content); } }
Name | Type | Size | Permission | Actions |
---|---|---|---|---|
Client | Folder | 0755 |
|
|
Concerns | Folder | 0755 |
|
|
Exceptions | Folder | 0755 |
|
|
Middleware | Folder | 0755 |
|
|
Resources | Folder | 0755 |
|
|
Testing | Folder | 0755 |
|
|
File.php | File | 155 B | 0644 |
|
FileHelpers.php | File | 1001 B | 0644 |
|
JsonResponse.php | File | 3.44 KB | 0644 |
|
LICENSE.md | File | 1.05 KB | 0644 |
|
RedirectResponse.php | File | 5.81 KB | 0644 |
|
Request.php | File | 17.72 KB | 0644 |
|
Response.php | File | 3.03 KB | 0644 |
|
ResponseTrait.php | File | 3.75 KB | 0644 |
|
UploadedFile.php | File | 4.01 KB | 0644 |
|
composer.json | File | 1.27 KB | 0644 |
|