miniMVC/sys/output.php

116 lines
1.8 KiB
PHP

<?php
/**
* MiniMVC
*
* Convention-based micro-framework for PHP
*
* @author Timothy J. Warren
* @copyright Copyright (c) 2011 - 2012
* @link https://github.com/timw4mail/miniMVC
* @license http://philsturgeon.co.uk/code/dbad-license
*/
// --------------------------------------------------------------------------
/**
* Class for displaying output and setting http headers
*
* @extends miniMVC
*/
class Output extends miniMVC {
private $buffer, $headers;
function __construct()
{
// Compression is good!
ob_start("ob_gzhandler");
$this->buffer = "";
$this->headers = array();
}
/**
* PHP magic method called when ending the script
* Used for outputing HTML
*/
function __destruct()
{
if( ! empty($this->headers))
{
// Set headers
foreach($this->headers as $key => $val)
{
if( ! isset($val))
{
@header($key);
}
else
{
@header("$key: $val");
}
}
}
if( ! empty($this->buffer))
{
echo $this->buffer;
ob_end_flush();
}
}
/**
* Sets a header for later output
* @param string $key
* @param string $val
*/
function set_header($key, $val)
{
$this->headers[$key] = $val;
}
/**
* Adds text to the output buffer
*
* @param string $string
*/
function append_output($string)
{
$this->buffer .= $string;
}
/**
* Sets the output buffer
*
* @param string $string
*/
function set_output($string)
{
$this->buffer = $string;
}
/**
* Sends headers and then removes them
*/
function flush_headers()
{
// Set headers
foreach($this->headers as $key => $val)
{
if( ! isset($val))
{
@header($key);
}
else
{
@header("$key: $val");
}
}
// Empty headers
$this->headers = array();
}
}
// End of Output.php