diff options
author | msquare <msquare@notrademark.de> | 2018-09-04 18:24:11 +0200 |
---|---|---|
committer | msquare <msquare@notrademark.de> | 2018-09-04 18:24:11 +0200 |
commit | b320fc779063ee80b8f0ba505cb323287ccccbf5 (patch) | |
tree | 1e420597ae72c979361bf29b66ae7e27c73cf431 /src/Middleware/SendResponseHandler.php | |
parent | 9f1ee0c6c6497d43fb275491ec53fda420f64b81 (diff) | |
parent | 36dafdb68acbde2fe42ce36ef50f497c8c06411f (diff) |
Merge branch 'MyIgel-rebuild-psr7'
Diffstat (limited to 'src/Middleware/SendResponseHandler.php')
-rw-r--r-- | src/Middleware/SendResponseHandler.php | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/src/Middleware/SendResponseHandler.php b/src/Middleware/SendResponseHandler.php new file mode 100644 index 00000000..34e70a87 --- /dev/null +++ b/src/Middleware/SendResponseHandler.php @@ -0,0 +1,69 @@ +<?php + +namespace Engelsystem\Middleware; + +use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\ServerRequestInterface; +use Psr\Http\Server\MiddlewareInterface; +use Psr\Http\Server\RequestHandlerInterface; + +class SendResponseHandler implements MiddlewareInterface +{ + /** + * Send the server response to the client + * + * This should be the first middleware + * + * @param ServerRequestInterface $request + * @param RequestHandlerInterface $handler + * @return ResponseInterface + */ + public function process( + ServerRequestInterface $request, + RequestHandlerInterface $handler + ): ResponseInterface { + $response = $handler->handle($request); + + if (!$this->headersSent()) { + $this->sendHeader(sprintf( + 'HTTP/%s %s %s', + $response->getProtocolVersion(), + $response->getStatusCode(), + $response->getReasonPhrase() + ), true, $response->getStatusCode()); + + foreach ($response->getHeaders() as $name => $values) { + foreach ($values as $value) { + $this->sendHeader($name . ': ' . $value, false); + } + } + } + + echo $response->getBody(); + return $response; + } + + /** + * Checks if headers have been sent + * + * @return bool + * @codeCoverageIgnore + */ + protected function headersSent() + { + return headers_sent(); + } + + /** + * Send a raw HTTP header + * + * @param string $content + * @param bool $replace + * @param int $code + * @codeCoverageIgnore + */ + protected function sendHeader($content, $replace = true, $code = null) + { + header($content, $replace, $code); + } +} |