Chris@0: hasHeader('Content-Length')) { Chris@0: // PSR-7 indicates int OR null for the stream size; for null values, Chris@0: // we will not auto-inject the Content-Length. Chris@0: if (null !== $response->getBody()->getSize()) { Chris@0: return $response->withHeader('Content-Length', (string) $response->getBody()->getSize()); Chris@0: } Chris@0: } Chris@0: Chris@0: return $response; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Emit the status line. Chris@0: * Chris@0: * Emits the status line using the protocol version and status code from Chris@0: * the response; if a reason phrase is available, it, too, is emitted. Chris@0: * Chris@0: * @param ResponseInterface $response Chris@0: */ Chris@0: private function emitStatusLine(ResponseInterface $response) Chris@0: { Chris@0: $reasonPhrase = $response->getReasonPhrase(); Chris@0: header(sprintf( Chris@0: 'HTTP/%s %d%s', Chris@0: $response->getProtocolVersion(), Chris@0: $response->getStatusCode(), Chris@0: ($reasonPhrase ? ' ' . $reasonPhrase : '') Chris@0: )); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Emit response headers. Chris@0: * Chris@0: * Loops through each header, emitting each; if the header value Chris@0: * is an array with multiple values, ensures that each is sent Chris@0: * in such a way as to create aggregate headers (instead of replace Chris@0: * the previous). Chris@0: * Chris@0: * @param ResponseInterface $response Chris@0: */ Chris@0: private function emitHeaders(ResponseInterface $response) Chris@0: { Chris@0: foreach ($response->getHeaders() as $header => $values) { Chris@0: $name = $this->filterHeader($header); Chris@0: $first = $name === 'Set-Cookie' ? false : true; Chris@0: foreach ($values as $value) { Chris@0: header(sprintf( Chris@0: '%s: %s', Chris@0: $name, Chris@0: $value Chris@0: ), $first); Chris@0: $first = false; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Loops through the output buffer, flushing each, before emitting Chris@0: * the response. Chris@0: * Chris@0: * @param int|null $maxBufferLevel Flush up to this buffer level. Chris@0: */ Chris@0: private function flush($maxBufferLevel = null) Chris@0: { Chris@0: if (null === $maxBufferLevel) { Chris@0: $maxBufferLevel = ob_get_level(); Chris@0: } Chris@0: Chris@0: while (ob_get_level() > $maxBufferLevel) { Chris@0: ob_end_flush(); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Filter a header name to wordcase Chris@0: * Chris@0: * @param string $header Chris@0: * @return string Chris@0: */ Chris@0: private function filterHeader($header) Chris@0: { Chris@0: $filtered = str_replace('-', ' ', $header); Chris@0: $filtered = ucwords($filtered); Chris@0: return str_replace(' ', '-', $filtered); Chris@0: } Chris@0: }