Chris@0: 0 && ob_get_length() > 0) { Chris@12: throw new RuntimeException('Output has been emitted previously; cannot emit response'); Chris@12: } 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@12: * It is important to mention that this method should be called after Chris@12: * `emitHeaders()` in order to prevent PHP from changing the status code of Chris@12: * the emitted response. Chris@12: * Chris@0: * @param ResponseInterface $response Chris@12: * Chris@12: * @see \Zend\Diactoros\Response\SapiEmitterTrait::emitHeaders() Chris@0: */ Chris@0: private function emitStatusLine(ResponseInterface $response) Chris@0: { Chris@0: $reasonPhrase = $response->getReasonPhrase(); Chris@12: $statusCode = $response->getStatusCode(); Chris@12: Chris@0: header(sprintf( Chris@0: 'HTTP/%s %d%s', Chris@0: $response->getProtocolVersion(), Chris@12: $statusCode, Chris@0: ($reasonPhrase ? ' ' . $reasonPhrase : '') Chris@12: ), true, $statusCode); 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@12: $statusCode = $response->getStatusCode(); Chris@12: 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@12: ), $first, $statusCode); Chris@0: $first = false; Chris@0: } 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: }