diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2019-11-27 19:11:37 +0100 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2019-12-08 02:12:56 +0100 |
commit | 89742ecd5542c740f3625af76c843a7471dbe98a (patch) | |
tree | 71c0c78ceb6a3a24b63c51c94a655cf5b33dc044 /src | |
parent | be39c63f46562eea173747d80cd91ac81e0b8e09 (diff) |
Response: Added with and withInput methods and back/redirect functions
Diffstat (limited to 'src')
-rw-r--r-- | src/Http/RedirectServiceProvider.php | 13 | ||||
-rw-r--r-- | src/Http/Redirector.php | 55 | ||||
-rw-r--r-- | src/Http/Response.php | 60 | ||||
-rw-r--r-- | src/Middleware/ErrorHandler.php | 25 | ||||
-rw-r--r-- | src/helpers.php | 40 |
5 files changed, 165 insertions, 28 deletions
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 @@ +<?php + +namespace Engelsystem\Http; + +use Engelsystem\Container\ServiceProvider; + +class RedirectServiceProvider extends ServiceProvider +{ + public function register() + { + $this->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 @@ +<?php + +namespace Engelsystem\Http; + +class Redirector +{ + /** @var Request */ + protected $request; + + /** @var Response */ + protected $response; + + /** + * @param Request $request + * @param Response $response + */ + public function __construct(Request $request, Response $response) + { + $this->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; + } } diff --git a/src/Middleware/ErrorHandler.php b/src/Middleware/ErrorHandler.php index 46e6e5a8..85f315d4 100644 --- a/src/Middleware/ErrorHandler.php +++ b/src/Middleware/ErrorHandler.php @@ -63,17 +63,11 @@ class ErrorHandler implements MiddlewareInterface } catch (HttpException $e) { $response = $this->createResponse($e->getMessage(), $e->getStatusCode(), $e->getHeaders()); } catch (ValidationException $e) { - $response = $this->createResponse('', 302, ['Location' => $this->getPreviousUrl($request)]); + $response = $this->redirectBack(); + $response->with('errors', ['validation' => $e->getValidator()->getErrors()]); if ($request instanceof Request) { - $session = $request->getSession(); - $errors = array_merge_recursive( - $session->get('errors', []), - ['validation' => $e->getValidator()->getErrors()] - ); - $session->set('errors', $errors); - - $session->set('form-data', Arr::except($request->request->all(), $this->formIgnore)); + $response->withInput(Arr::except($request->request->all(), $this->formIgnore)); } } @@ -140,15 +134,12 @@ class ErrorHandler implements MiddlewareInterface } /** - * @param ServerRequestInterface $request - * @return string + * Create a redirect back response + * + * @return Response */ - protected function getPreviousUrl(ServerRequestInterface $request) + protected function redirectBack() { - if ($header = $request->getHeader('referer')) { - return array_pop($header); - } - - return '/'; + return back(); } } diff --git a/src/helpers.php b/src/helpers.php index de140c4e..7cb17ea9 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -4,6 +4,7 @@ use Engelsystem\Application; use Engelsystem\Config\Config; use Engelsystem\Helpers\Authenticator; use Engelsystem\Helpers\Translation\Translator; +use Engelsystem\Http\Redirector; use Engelsystem\Http\Request; use Engelsystem\Http\Response; use Engelsystem\Http\UrlGeneratorInterface; @@ -28,7 +29,7 @@ function app($id = null) /** * @return Authenticator */ -function auth() +function auth(): Authenticator { return app('authenticator'); } @@ -37,12 +38,25 @@ function auth() * @param string $path * @return string */ -function base_path($path = '') +function base_path($path = ''): string { return app('path') . (empty($path) ? '' : DIRECTORY_SEPARATOR . $path); } /** + * @param int $status + * @param array $headers + * @return Response + */ +function back($status = 302, $headers = []): Response +{ + /** @var Redirector $redirect */ + $redirect = app('redirect'); + + return $redirect->back($status, $headers); +} + +/** * Get or set config values * * @param string|array $key @@ -70,12 +84,26 @@ function config($key = null, $default = null) * @param string $path * @return string */ -function config_path($path = '') +function config_path($path = ''): string { return app('path.config') . (empty($path) ? '' : DIRECTORY_SEPARATOR . $path); } /** + * @param string $path + * @param int $status + * @param array $headers + * @return Response + */ +function redirect(string $path, $status = 302, $headers = []): Response +{ + /** @var Redirector $redirect */ + $redirect = app('redirect'); + + return $redirect->to($path, $status, $headers); +} + +/** * @param string $key * @param mixed $default * @return Request|mixed @@ -97,7 +125,7 @@ function request($key = null, $default = null) * @param array $headers * @return Response */ -function response($content = '', $status = 200, $headers = []) +function response($content = '', $status = 200, $headers = []): Response { /** @var Response $response */ $response = app('psr7.response'); @@ -155,7 +183,7 @@ function trans($key = null, $replace = []) * @param array $replace * @return string */ -function __($key, $replace = []) +function __($key, $replace = []): string { /** @var Translator $translator */ $translator = app('translator'); @@ -172,7 +200,7 @@ function __($key, $replace = []) * @param array $replace * @return string */ -function _e($key, $keyPlural, $number, $replace = []) +function _e($key, $keyPlural, $number, $replace = []): string { /** @var Translator $translator */ $translator = app('translator'); |