miniMVC/sys/core/output.php

151 lines
2.4 KiB
PHP
Raw Normal View History

2012-01-13 11:58:06 -05:00
<?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
*
* @package miniMVC
* @subpackage System
2012-01-13 11:58:06 -05:00
*/
2012-04-27 16:28:25 -04:00
class MM_Output extends MM {
2012-01-13 11:58:06 -05:00
/**
* Content for outputting
*
* @var string
*/
private $buffer;
/**
* HTTP headers to send
*
* @var array
*/
private $headers;
2012-01-13 11:58:06 -05:00
/**
* Initialize the output class
*
* @return void
*/
public function __construct()
2012-01-13 11:58:06 -05:00
{
$this->buffer = "";
2012-05-14 14:35:57 -04:00
$this->headers = [];
2012-01-13 11:58:06 -05:00
}
/**
* PHP magic method called when ending the script
* Used for outputing HTML
*
* @return void
2012-01-13 11:58:06 -05:00
*/
public function __destruct()
2012-01-13 11:58:06 -05:00
{
2012-05-03 13:26:09 -04:00
if ( ! empty($this->headers))
2012-01-13 11:58:06 -05:00
{
// Set headers
foreach($this->headers as $key => $val)
{
2012-05-03 13:26:09 -04:00
if ( ! isset($val))
2012-01-13 11:58:06 -05:00
{
@header($key);
}
else
{
@header("$key: $val");
}
}
}
2012-05-03 13:26:09 -04:00
if ( ! empty($this->buffer))
2012-04-27 16:28:25 -04:00
{
2012-05-03 13:26:09 -04:00
if (is_null(error_get_last()))
2012-01-13 11:58:06 -05:00
{
2012-04-27 16:28:25 -04:00
// Compression is good!
ob_start("ob_gzhandler");
2012-01-13 11:58:06 -05:00
}
else
{
ob_start();
}
echo $this->buffer;
ob_end_flush();
}
}
2012-05-03 07:56:14 -04:00
// --------------------------------------------------------------------------
2012-01-13 11:58:06 -05:00
/**
* Sets a header for later output
*
2012-01-13 11:58:06 -05:00
* @param string $key
* @param string $val
*/
public function set_header($key, $val)
2012-01-13 11:58:06 -05:00
{
$this->headers[$key] = $val;
}
2012-05-03 07:56:14 -04:00
// --------------------------------------------------------------------------
2012-01-13 11:58:06 -05:00
/**
* Adds text to the output buffer
*
* @param string $string
*/
public function append_output($string)
2012-01-13 11:58:06 -05:00
{
$this->buffer .= $string;
}
2012-05-03 07:56:14 -04:00
// --------------------------------------------------------------------------
2012-01-13 11:58:06 -05:00
/**
* Sets the output buffer
*
* @param string $string
*/
public function set_output($string)
2012-01-13 11:58:06 -05:00
{
$this->buffer = $string;
}
2012-05-03 07:56:14 -04:00
// --------------------------------------------------------------------------
2012-01-13 11:58:06 -05:00
/**
* Sends headers and then removes them
*/
public function flush_headers()
2012-01-13 11:58:06 -05:00
{
// Set headers
2012-05-14 14:35:57 -04:00
foreach($this->headers as $key => &$val)
2012-01-13 11:58:06 -05:00
{
2012-05-03 13:26:09 -04:00
if ( ! isset($val))
2012-01-13 11:58:06 -05:00
{
@header($key);
}
else
{
2012-05-01 16:39:14 -04:00
@header("{$key}: {$val}");
2012-01-13 11:58:06 -05:00
}
}
// Empty headers
2012-05-14 14:35:57 -04:00
$this->headers = [];
2012-01-13 11:58:06 -05:00
}
}
2012-05-17 16:22:39 -04:00
2012-01-13 11:58:06 -05:00
// End of Output.php