Skip to content

Instantly share code, notes, and snippets.

@jeserkin
Created December 19, 2012 22:16
Show Gist options
  • Select an option

  • Save jeserkin/4341055 to your computer and use it in GitHub Desktop.

Select an option

Save jeserkin/4341055 to your computer and use it in GitHub Desktop.
/**
* @param bool $throw
* @throws RuntimeException
* @return bool
*/
private function canSendHeaders( $throw = false )
{
$result = headers_sent( $inFile, $atLine );
if ( $result && $throw )
{
throw new RuntimeException( "Headers already sent in file: {$inFile} at line {$atLine}" );
}
return ! $result;
}
/**
* @return void
*/
public function sendResponse()
{
$this->sendHeaders();
$this->sendContent();
}
/**
* @return void
*/
public function sendHeaders()
{
if ( ! $this->canSendHeaders( true ) )
{
return;
}
header( 'HTTP/1.0 ' . $this->statusCode . ' ' . $this->statusText );
if ( ! in_array( 'Content-Type', $this->httpHeaders ) )
{
header( 'Content-Type: ' . $this->contentType . ( empty( $this->charset ) ? '' : '; charset=' . $this->charset ) );
}
foreach ( $this->httpHeaders as $headerName => $headerValue )
{
header( $headerName . ': ' . $headerValue );
}
}
/**
* @return void
*/
public function sendContent()
{
echo $this->content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment