From 89742ecd5542c740f3625af76c843a7471dbe98a Mon Sep 17 00:00:00 2001 From: Igor Scheller Date: Wed, 27 Nov 2019 19:11:37 +0100 Subject: Response: Added with and withInput methods and back/redirect functions --- src/Http/RedirectServiceProvider.php | 13 ++++++++ src/Http/Redirector.php | 55 +++++++++++++++++++++++++++++++++ src/Http/Response.php | 60 +++++++++++++++++++++++++++++++++--- 3 files changed, 123 insertions(+), 5 deletions(-) create mode 100644 src/Http/RedirectServiceProvider.php create mode 100644 src/Http/Redirector.php (limited to 'src/Http') diff --git a/src/Http/RedirectServiceProvider.php b/src/Http/RedirectServiceProvider.php new file mode 100644 index 00000000..70238d91 --- /dev/null +++ b/src/Http/RedirectServiceProvider.php @@ -0,0 +1,13 @@ +app->bind('redirect', Redirector::class); + } +} diff --git a/src/Http/Redirector.php b/src/Http/Redirector.php new file mode 100644 index 00000000..4149a8ec --- /dev/null +++ b/src/Http/Redirector.php @@ -0,0 +1,55 @@ +request = $request; + $this->response = $response; + } + + /** + * @param string $path + * @param int $status + * @param array $headers + * @return Response + */ + public function to(string $path, int $status = 302, array $headers = []): Response + { + return $this->response->redirectTo($path, $status, $headers); + } + + /** + * @param int $status + * @param array $headers + * @return Response + */ + public function back(int $status = 302, array $headers = []): Response + { + return $this->to($this->getPreviousUrl(), $status, $headers); + } + + /** + * @return string + */ + protected function getPreviousUrl(): string + { + if ($header = $this->request->getHeader('referer')) { + return array_pop($header); + } + + return '/'; + } +} diff --git a/src/Http/Response.php b/src/Http/Response.php index a6b4ab74..43bcb5d8 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -6,27 +6,37 @@ use Engelsystem\Renderer\Renderer; use InvalidArgumentException; use Psr\Http\Message\ResponseInterface; use Symfony\Component\HttpFoundation\Response as SymfonyResponse; +use Symfony\Component\HttpFoundation\Session\SessionInterface; class Response extends SymfonyResponse implements ResponseInterface { use MessageTrait; + /** + * @var SessionInterface + */ + protected $session; + /** @var Renderer */ protected $renderer; /** - * @param string $content - * @param int $status - * @param array $headers - * @param Renderer $renderer + * @param string $content + * @param int $status + * @param array $headers + * @param Renderer $renderer + * @param SessionInterface $session */ public function __construct( $content = '', int $status = 200, array $headers = [], - Renderer $renderer = null + Renderer $renderer = null, + SessionInterface $session = null ) { $this->renderer = $renderer; + $this->session = $session; + parent::__construct($content, $status, $headers); } @@ -155,4 +165,44 @@ class Response extends SymfonyResponse implements ResponseInterface { $this->renderer = $renderer; } + + /** + * Sets a session attribute (which is mutable) + * + * @param string $key + * @param mixed|mixed[] $value + * @return Response + */ + public function with(string $key, $value) + { + if (!$this->session instanceof SessionInterface) { + throw new InvalidArgumentException('Session not defined'); + } + + $data = $this->session->get($key); + if (is_array($data) && is_array($value)) { + $value = array_merge_recursive($data, $value); + } + + $this->session->set($key, $value); + + return $this; + } + + /** + * Sets form data to the mutable session + * + * @param array $input + * @return Response + */ + public function withInput(array $input) + { + if (!$this->session instanceof SessionInterface) { + throw new InvalidArgumentException('Session not defined'); + } + + $this->session->set('form-data', $input); + + return $this; + } } -- cgit v1.2.3-70-g09d2