diff options
author | Igor Scheller <igor.scheller@igorshp.de> | 2018-08-07 03:18:22 +0200 |
---|---|---|
committer | Igor Scheller <igor.scheller@igorshp.de> | 2018-08-14 00:17:19 +0200 |
commit | 20c03a155d2017101a098cefa602116a4a331d71 (patch) | |
tree | 7d91761680d8183bdeb4f9299800c47256086db9 /src/helpers.php | |
parent | 92c26718fd0799660515d64feabbbc1cd1d71a35 (diff) |
Implemented PSR-15 middleware handler
Diffstat (limited to 'src/helpers.php')
-rw-r--r-- | src/helpers.php | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/src/helpers.php b/src/helpers.php index c3c727ec..2a90dcde 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -6,13 +6,15 @@ use Engelsystem\Config\Config; use Engelsystem\Http\Request; use Engelsystem\Renderer\Renderer; use Engelsystem\Routing\UrlGenerator; +use Psr\Http\Message\ResponseInterface; use Symfony\Component\HttpFoundation\Session\SessionInterface; +use Zend\Diactoros\Stream; /** * Get the global app instance * * @param string $id - * @return mixed + * @return mixed|Application */ function app($id = null) { @@ -81,6 +83,32 @@ function request($key = null, $default = null) } /** + * @param string $content + * @param int $status + * @param array $headers + * @return ResponseInterface + */ +function response($content = '', $status = 200, $headers = []) +{ + /** @var ResponseInterface $response */ + $response = app('psr7.response'); + + /** @var Stream $stream */ + $stream = app()->make(Stream::class, ['stream' => 'php://memory', 'mode' => 'wb+']); + $stream->write($content); + $stream->rewind(); + + $response = $response + ->withBody($stream) + ->withStatus($status); + foreach ($headers as $key => $value) { + $response = $response->withAddedHeader($key, $value); + } + + return $response; +} + +/** * @param string $key * @param mixed $default * @return SessionInterface|mixed |