From a2c47304d8230f1937c2a4f019aba89fa74203bc Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 29 Aug 2018 13:58:50 +0200 Subject: Added generic error pages --- src/Http/Response.php | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'src/Http/Response.php') diff --git a/src/Http/Response.php b/src/Http/Response.php index 9db6fa83..d79ab98b 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -2,6 +2,7 @@ namespace Engelsystem\Http; +use Engelsystem\Renderer\Renderer; use Psr\Http\Message\ResponseInterface; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; @@ -9,6 +10,25 @@ class Response extends SymfonyResponse implements ResponseInterface { use MessageTrait; + /** @var Renderer */ + protected $view; + + /** + * @param string $content + * @param int $status + * @param array $headers + * @param Renderer $view + */ + public function __construct( + $content = '', + int $status = 200, + array $headers = array(), + Renderer $view = null + ) { + $this->view = $view; + parent::__construct($content, $status, $headers); + } + /** * Return an instance with the specified status code and, optionally, reason phrase. * @@ -72,4 +92,25 @@ class Response extends SymfonyResponse implements ResponseInterface return $new; } + + /** + * Return an instance with the rendered content. + * + * THis method retains the immutability of the message and returns + * an instance with the updated status and headers + * + * @param string $view + * @param array $data + * @param int $status + * @param string[]|string[][] $headers + * @return Response + */ + public function withView($view, $data = [], $status = 200, $headers = []) + { + if (!$this->view instanceof Renderer) { + throw new \InvalidArgumentException('Renderer not defined'); + } + + return $this->create($this->view->render($view, $data), $status, $headers); + } } -- cgit v1.2.3-54-g00ecf