Added Output class, fixed some formatting issues

This commit is contained in:
Timothy Warren 2011-07-26 11:58:23 -04:00
parent c52724e325
commit d043477bc3
3 changed files with 502 additions and 284 deletions

View File

@ -0,0 +1,2 @@
Codeigniter Page Builder is a simple system for putting together content into web pages, using Codeigniter's views, and making it simpler to set expiration headers for javascript and CSS.

View File

@ -0,0 +1,222 @@
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
define('MIN_SAFE', 1);
define('MIN_EXTREME', 2);
define('MIN_EXTREME_COMMENTS', 4);
// ------------------------------------------------------------------------
/**
* Output Class
*
* Responsible for sending final output to browser
*
* @package CodeIgniter
* @subpackage Libraries
* @category Output
* @author ExpressionEngine Dev Team
* @link http://codeigniter.com/user_guide/libraries/output.html
*/
class MY_Output extends CI_Output
{
var $final_output;
var $cache_expiration = 0;
var $headers = array();
var $enable_profiler = FALSE;
var $min = 0;
// --------------------------------------------------------------------
/**
* Display Output
*
* All "view" data is automatically put into this variable by the controller class:
*
* $this->final_output
*
* This function sends the finalized output data to the browser along
* with any server headers and profile data. It also stops the
* benchmark timer so the page rendering speed and memory usage can be shown.
*
* @access public
* @return mixed
*/
function _display($output = '')
{
// Note: We use globals because we can't use $CI =& get_instance()
// since this function is sometimes called by the caching mechanism,
// which happens before the CI super object is available.
global $BM, $CFG;
//$this->min = 0;
// --------------------------------------------------------------------
// Set the output data
if ($output == '')
{
$output =& $this->final_output;
}
// --------------------------------------------------------------------
// Do we need to write a cache file?
if ($this->cache_expiration > 0)
{
$this->_write_cache($output);
}
// --------------------------------------------------------------------
// Parse out the elapsed time and memory usage,
// then swap the pseudo-variables with the data
$elapsed = $BM->elapsed_time('total_execution_time_start', 'total_execution_time_end');
$output = str_replace('{elapsed_time}', $elapsed, $output);
$memory = (!function_exists('memory_get_usage')) ? '0' : round(memory_get_usage() / 1024 / 1024, 2) . 'MB';
$output = str_replace('{memory_usage}', $memory, $output);
// --------------------------------------------------------------------
// Is compression requested?
if ($CFG->item('compress_output') === TRUE)
{
if (extension_loaded('zlib'))
{
if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
{
ob_start('ob_gzhandler');
}
}
}
// --------------------------------------------------------------------
// Are there any server headers to send?
if (count($this->headers) > 0)
{
foreach ($this->headers as $header)
{
header($header[0], $header[1]);
}
}
// --------------------------------------------------------------------
// Does the get_instance() function exist?
// If not we know we are dealing with a cache file so we'll
// simply echo out the data and exit.
if (!function_exists('get_instance'))
{
echo $output;
log_message('debug', "Final output sent to browser");
log_message('debug', "Total execution time: " . $elapsed);
return TRUE;
}
// --------------------------------------------------------------------
// Grab the super object. We'll need it in a moment…
$CI =& get_instance();
// Do we need to generate profile data?
// If so, load the Profile class and run it.
if ($this->enable_profiler == TRUE)
{
$CI->load->library('profiler');
// If the output data contains closing </body> and </html> tags
// we will remove them and add them back after we insert the profile data
if (preg_match("|</body>.*?</html>|is", $output))
{
$output = preg_replace("|</body>.*?</html>|is", '', $output);
$output .= $CI->profiler->run();
$output .= '</body></html>';
}
else
{
$output .= $CI->profiler->run();
}
}
//Let's minify!
switch ((int) $this->min)
{
case 0:
//Don't minify
break;
case 1: //Safe Minify
$output = preg_replace("`>\s+<`", "> <", $output);
break;
case 2: //Extreme Minify
$output = preg_replace('/<!--[^\[](.*)-->/Uis', '', $output);
$output = preg_replace("`\s+`", " ", $output);
$output = preg_replace("`> <`", "><", $output);
$output = str_replace("</a><a", "</a> <a", $output);
$output = preg_replace("`(<img(.*?)/>)`", " <img$2/> ", $output);
break;
case 4: //Extreme minify, save comments
$output = preg_replace("`\s+`", " ", $output);
$output = preg_replace("`> <`", "><", $output);
$output = str_replace("</a><a", "</a> <a", $output);
$output = preg_replace("`(<img(.*?)/>)`", " <img$2/> ", $output);
break;
default:
//Don't minify
break;
}
//Replace common entities with more compatible versions
$replace = array(
'"&"' => '"&amp;"',
'{NL}' => " ",
'&nbsp;' => '&#160;',
'&copy;' => '&#169;',
'&acirc;' => '&#226;',
'&cent;' => '&#162;',
'&raquo;' => '&#187;',
'&laquo;' => '&#171;'
);
$output = strtr($output, $replace);
// --------------------------------------------------------------------
// Does the controller contain a function named _output()?
// If so send the output there. Otherwise, echo it.
if (method_exists($CI, '_output'))
{
$CI->_output($output);
}
else
{
echo $output; // Send it to the browser!
}
log_message('debug', "Final output sent to browser");
log_message('debug', "Total execution time: " . $elapsed);
}
/**
* Enable/disable Minified HTML
*
* @access public
* @param int
* @return void
*/
function enable_min($val = 0)
{
$this->min = $val;
}
}
// END Output Class
/* End of file Output.php */
/* Location: ./system/libraries/Output.php */

View File

@ -1,58 +1,58 @@
<?php (defined('BASEPATH')) OR exit('No direct script access allowed'); <?php
(defined('BASEPATH')) OR exit('No direct script access allowed');
/** /**
* Class for building pages * Class for building pages
* *
* All methods are chainable, with the exception of the constructor, * All methods are chainable, with the exception of the constructor,
* build_header(), build_footer(), build_page() and _headers() methods. * build_header(), build_footer(), build_page() and _headers() methods.
*/ */
class Page { class Page
{
private static $meta, $head_js, $foot_js, $css, $title, private static $meta, $head_js, $foot_js, $css, $title, $head_tags, $body_class, $body_id, $base;
$head_tags, $body_class, $body_id, $base;
private $CI; private $CI;
public function __construct() public function __construct()
{ {
$this->meta = ""; $this->meta = "";
$this->head_js = ""; $this->head_js = "";
$this->foot_js = ""; $this->foot_js = "";
$this->css = ""; $this->css = "";
$this->title = ""; $this->title = "";
$this->head_tags = ""; $this->head_tags = "";
$this->body_class = ""; $this->body_class = "";
$this->body_id = ""; $this->body_id = "";
$this->base = ""; $this->base = "";
$this->CI =& get_instance(); $this->CI =& get_instance();
//Define some constants for formatting //Define some constants for formatting
define('NL', "\n"); define('NL', "\n");
define('T1', "\t"); define('T1', "\t");
define('T2', T1.T1); define('T2', T1 . T1);
define('T3', T2.T1); define('T3', T2 . T1);
define('T4', T2.T2); define('T4', T2 . T2);
define('T5', T3.T2); define('T5', T3 . T2);
define('T6', T3.T3); define('T6', T3 . T3);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Sets server headers and doctype * Sets server headers and doctype
* *
* Also sets page mime type, based on if sent as * Also sets page mime type, based on if sent as
* html or xhtml, and what the target browser * html or xhtml, and what the target browser
* supports * supports
* *
* @param bool $xhtml * @param bool $xhtml
* @param bool $html5 * @param bool $html5
* @return Page * @return Page
*/ */
private function _headers($xhtml, $html5) private function _headers($xhtml, $html5)
{ {
$this->CI->output->set_header("Cache-Control: must-revalidate, public"); $this->CI->output->set_header("Cache-Control: must-revalidate, public");
$this->CI->output->set_header("Vary: Accept"); $this->CI->output->set_header("Vary: Accept");
$mime = ""; $mime = "";
//Variable for accept keyword //Variable for accept keyword
$accept = (!empty($_SERVER['HTTP_ACCEPT'])) ? $_SERVER['HTTP_ACCEPT'] : ""; $accept = (!empty($_SERVER['HTTP_ACCEPT'])) ? $_SERVER['HTTP_ACCEPT'] : "";
@ -63,21 +63,21 @@ class Page {
//Predefine charset //Predefine charset
$charset = "UTF-8"; $charset = "UTF-8";
//If xhtml flag is false, set html4 header //If xhtml flag is false, set html4 header
if($xhtml == TRUE) if ($xhtml == TRUE)
{ {
//Check that the user agent accepts application/xhtml+xml, or if it's the W3C Validator //Check that the user agent accepts application/xhtml+xml, or if it's the W3C Validator
if(stristr($accept,"application/xhtml+xml") || stristr($_SERVER["HTTP_USER_AGENT"],"W3C_Validator")) if (stristr($accept, "application/xhtml+xml") || stristr($_SERVER["HTTP_USER_AGENT"], "W3C_Validator"))
{ {
$mime = "application/xhtml+xml"; $mime = "application/xhtml+xml";
} }
//Or if it supports application/xml //Or if it supports application/xml
else if(stristr($accept,"application/xml")) else if (stristr($accept, "application/xml"))
{ {
$mime = "application/xml"; $mime = "application/xml";
} }
//Or if it supports text/xml //Or if it supports text/xml
else if(stristr($accept,"text/xml")) else if (stristr($accept, "text/xml"))
{ {
$mime = "text/xml"; $mime = "text/xml";
} }
@ -85,7 +85,7 @@ class Page {
{ {
$mime = "text/html"; $mime = "text/html";
if($html5 == FALSE) //If it's not HTML5, it's HTML4 if ($html5 == FALSE) //If it's not HTML5, it's HTML4
{ {
$doctype_string = doctype('html4-strict'); $doctype_string = doctype('html4-strict');
} }
@ -95,40 +95,40 @@ class Page {
{ {
$mime = "text/html"; $mime = "text/html";
if($html5 == FALSE) if ($html5 == FALSE)
{ {
$doctype_string = doctype('html4-strict'); $doctype_string = doctype('html4-strict');
} }
} }
// set the doctype according to the mime type which was determined // set the doctype according to the mime type which was determined
if($mime == "application/xhtml+xml" || $mime == "text/xml" || $mime == "application/xml") if ($mime == "application/xhtml+xml" || $mime == "text/xml" || $mime == "application/xml")
{ {
if($html5 == TRUE) if ($html5 == TRUE)
{ {
$doctype_string = ''; $doctype_string = '';
} }
$doctype_string = "<?xml version='1.0' encoding='$charset' ?>\n" . $doctype_string = "<?xml version='1.0' encoding='$charset' ?>\n"
$doctype_string . "\n<html xmlns='http://www.w3.org/1999/xhtml'" . . $doctype_string
" xml:lang='en'>"; . "\n<html xmlns='http://www.w3.org/1999/xhtml'" . " xml:lang='en'>";
} }
else else
{ {
$doctype_string .= "\n<html lang='en'>"; $doctype_string .= "\n<html lang='en'>";
} }
// finally, output the mime type and prolog type // finally, output the mime type and prolog type
$this->CI->output->set_header("Content-Type: $mime;charset=$charset"); $this->CI->output->set_header("Content-Type: $mime;charset=$charset");
$this->CI->output->set_header("X-UA-Compatible: chrome=1, IE=edge"); $this->CI->output->set_header("X-UA-Compatible: chrome=1, IE=edge");
$this->CI->output->set_output($doctype_string); $this->CI->output->set_output($doctype_string);
return $this; return $this;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Set Meta * Set Meta
* *
* Sets meta tags, with codeigniter native meta tag helper * Sets meta tags, with codeigniter native meta tag helper
@ -136,172 +136,171 @@ class Page {
* @param array $meta * @param array $meta
* @return Page * @return Page
*/ */
public function set_meta($meta) public function set_meta($meta)
{ {
$this->meta .= T1.meta($meta).NL; $this->meta .= T1 . meta($meta) . NL;
return $this; return $this;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Sets minified javascript group in header * Sets minified javascript group in header
* @param string $group * @param string $group
* @param bool $debug * @param bool $debug
* @return Page * @return Page
*/ */
public function set_head_js_group($group, $debug=FALSE) public function set_head_js_group($group, $debug = FALSE)
{ {
if($group === FALSE) if ($group === FALSE)
{ {
return $this; return $this;
} }
$file = $this->CI->config->item('group_js_path') . $group; $file = $this->CI->config->item('group_js_path') . $group;
$file .= ($debug == TRUE) ? "/debug/1" : ""; $file .= ($debug == TRUE) ? "/debug/1" : "";
$this->head_js .= $this->script_tag($file, FALSE); $this->head_js .= $this->script_tag($file, FALSE);
return $this; return $this;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Set an individual js file in header * Set an individual js file in header
* @param string $js * @param string $js
* @param bool $domain * @param bool $domain
* @return Page * @return Page
*/ */
public function set_head_js($js, $domain=TRUE) public function set_head_js($js, $domain = TRUE)
{ {
$this->head_js .= $this->script_tag($js, $domain); $this->head_js .= $this->script_tag($js, $domain);
return $this; return $this;
} }
// --------------------------------------------------------------------------
/**
* Sets a minified css group
* @param string $group
* @return Page
*/
public function set_css_group($group)
{
$link = array(
'href' => $this->CI->config->item('group_style_path') . $group,
'rel' => 'stylesheet',
'type' => 'text/css',
);
$this->css .= T1.link_tag($link).NL;
return $this;
}
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Sets a minified javascript group for the page footer * Sets a minified css group
* @param string $group * @param string $group
* @return Page * @return Page
*/ */
public function set_foot_js_group($group, $debug=FALSE) public function set_css_group($group)
{ {
$file = $this->CI->config->item('group_js_path') . $group; $link = array(
$file .= ($debug == TRUE) ? "?debug=1" : ""; 'href' => $this->CI->config->item('group_style_path') . $group,
$this->foot_js .= $this->script_tag($file, FALSE); 'rel' => 'stylesheet',
return $this; 'type' => 'text/css'
} );
$this->css .= T1 . link_tag($link) . NL;
// --------------------------------------------------------------------------
/**
* Sets js in footer; multiple files are combined and minified.
* @param array $args
* @return Page
*/
public function set_foot_js($js, $domain)
{
$this->foot_js .= $this->script_tag($js, $domain);
return $this;
}
// --------------------------------------------------------------------------
/**
* Sets html title string
* @param string $title
* @return Page
*/
public function set_title($title="")
{
$title = ($title == "") ?
$this->CI->config->item('default_title') : $title;
$this->title = $title;
return $this;
}
// --------------------------------------------------------------------------
/**
* Sets custom body class
* @param string $class
* @return Page
*/
public function set_body_class($class="")
{
$this->body_class = $class;
return $this;
}
// --------------------------------------------------------------------------
/**
* Sets custom body id
* @param string $id
* @return Page
*/
public function set_body_id($id="")
{
$this->body_id = $id;
return $this;
}
// --------------------------------------------------------------------------
/**
* Sets custom base href
* @param string href
* @return Page
*/
public function set_base($href)
{
$this->base = $href;
return $this;
}
// --------------------------------------------------------------------------
/**
* Sets custom css tags
* @param string $name
* @param string $media
* @return Page
*/
public function set_css_tag($name, $domain=TRUE, $media="all")
{
$path = $this->CI->config->item('content_domain');
$css_file = $path . "/css/" . $name . ".css";
if ($domain == FALSE)
$css_file = $name;
$this->css_tags .= T1.link_tag($name, "stylesheet", "text/css", "", $media).NL;
return $this; return $this;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/**
* Sets a minified javascript group for the page footer
* @param string $group
* @return Page
*/
public function set_foot_js_group($group, $debug = FALSE)
{
$file = $this->CI->config->item('group_js_path') . $group;
$file .= ($debug == TRUE) ? "?debug=1" : "";
$this->foot_js .= $this->script_tag($file, FALSE);
return $this;
}
// --------------------------------------------------------------------------
/**
* Sets js in footer; multiple files are combined and minified.
* @param array $args
* @return Page
*/
public function set_foot_js($js, $domain)
{
$this->foot_js .= $this->script_tag($js, $domain);
return $this;
}
// --------------------------------------------------------------------------
/**
* Sets html title string
* @param string $title
* @return Page
*/
public function set_title($title = "")
{
$title = ($title == "") ? $this->CI->config->item('default_title') : $title;
$this->title = $title;
return $this;
}
// --------------------------------------------------------------------------
/**
* Sets custom body class
* @param string $class
* @return Page
*/
public function set_body_class($class = "")
{
$this->body_class = $class;
return $this;
}
// --------------------------------------------------------------------------
/**
* Sets custom body id
* @param string $id
* @return Page
*/
public function set_body_id($id = "")
{
$this->body_id = $id;
return $this;
}
// --------------------------------------------------------------------------
/**
* Sets custom base href
* @param string href
* @return Page
*/
public function set_base($href)
{
$this->base = $href;
return $this;
}
// --------------------------------------------------------------------------
/**
* Sets custom css tags
* @param string $name
* @param string $media
* @return Page
*/
public function set_css_tag($name, $domain = TRUE, $media = "all")
{
$path = $this->CI->config->item('content_domain');
$css_file = $path . "/css/" . $name . ".css";
if ($domain == FALSE)
$css_file = $name;
$this->css_tags .= T1 . link_tag($name, "stylesheet", "text/css", "", $media) . NL;
return $this;
}
// --------------------------------------------------------------------------
/** /**
* Sets uncompressed js file in footer * Sets uncompressed js file in footer
@ -309,9 +308,9 @@ class Page {
* @param bool $domain * @param bool $domain
* @return Page * @return Page
*/ */
public function set_foot_js_tag($name, $domain=TRUE) public function set_foot_js_tag($name, $domain = TRUE)
{ {
$this->foot_js .= $this->script_tag($name, $domain); $this->foot_js .= $this->script_tag($name, $domain);
return $this; return $this;
} }
@ -322,13 +321,13 @@ class Page {
* @param string $tag * @param string $tag
* @return Page * @return Page
*/ */
public function set_head_tag($tag) public function set_head_tag($tag)
{ {
$this->head_tags .= $tag . "\n"; $this->head_tags .= $tag . "\n";
return $this; return $this;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Sets custom page header * Sets custom page header
@ -337,14 +336,12 @@ class Page {
* @param bool $fbml * @param bool $fbml
* @return $this * @return $this
*/ */
public function build_header($xhtml = FALSE, $html5 = TRUE) public function build_header($xhtml = FALSE, $html5 = TRUE)
{ {
$data = array(); $data = array();
//Set Meta Tags //Set Meta Tags
$this->meta = ($html5 == TRUE) ? $this->meta = ($html5 == TRUE) ? T1 . '<meta charset="utf-8" />' . NL . $this->meta : T1 . meta('content-type', 'text/html; charset=utf-8', 'equiv') . NL . $this->meta;
T1.'<meta charset="utf-8" />'.NL. $this->meta :
T1.meta('content-type', 'text/html; charset=utf-8', 'equiv').NL.$this->meta;
$data['meta'] = $this->meta; $data['meta'] = $this->meta;
//Set CSS //Set CSS
@ -360,57 +357,56 @@ class Page {
} }
//Set head javascript //Set head javascript
if($this->head_js != "") if ($this->head_js != "")
{ {
$data['head_js'] = $this->head_js; $data['head_js'] = $this->head_js;
} }
else else
{ {
$this->set_head_js_group($this->CI->config->item('default_head_js_group')); $this->set_head_js_group($this->CI->config->item('default_head_js_group'));
$data['head_js'] = $this->head_js; $data['head_js'] = $this->head_js;
} }
//Set Page Title //Set Page Title
$data['title'] = ($this->title != '') ? $this->title : $this->CI->config->item('default_title'); $data['title'] = ($this->title != '') ? $this->title : $this->CI->config->item('default_title');
//Set Body Class //Set Body Class
$data['body_class'] = $this->body_class; $data['body_class'] = $this->body_class;
//Set Body Id //Set Body Id
$data['body_id'] = $this->body_id; $data['body_id'] = $this->body_id;
//Set Base HREF //Set Base HREF
$data['base'] = $this->base; $data['base'] = $this->base;
//Set individual head tags //Set individual head tags
$data['head_tags'] = $this->head_tags; $data['head_tags'] = $this->head_tags;
//Set Server Headers and Doctype //Set Server Headers and Doctype
$this->_headers($xhtml, $html5); $this->_headers($xhtml, $html5);
//Output Header //Output Header
$this->CI->load->view('header', $data); $this->CI->load->view('header', $data);
flush(); flush();
return $this; return $this;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/** /**
* Builds common footer with any additional js * Builds common footer with any additional js
*/ */
public function build_footer() public function build_footer()
{ {
$data = array(); $data = array();
$data['foot_js'] = ($this->foot_js != "") ? $data['foot_js'] = ($this->foot_js != "") ? $this->foot_js : '';
$this->foot_js : '';
$this->CI->load->view('footer', $data); $this->CI->load->view('footer', $data);
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -423,17 +419,15 @@ class Page {
* @param bool $domain * @param bool $domain
* @return string * @return string
*/ */
private function script_tag($js, $domain=TRUE) private function script_tag($js, $domain = TRUE)
{ {
$path = $this->CI->config->item('content_domain'); $path = $this->CI->config->item('content_domain');
$js_file = $path . "/js/" . $js . ".js"; $js_file = $path . "/js/" . $js . ".js";
if ($domain == FALSE) if ($domain == FALSE)
$js_file = $js; $js_file = $js;
$tag = T1.'<script src="' . $tag = T1 . '<script src="' . $js_file . '"></script>' . NL;
$js_file .
'"></script>'.NL;
return $tag; return $tag;
} }
@ -449,7 +443,7 @@ class Page {
* @param bool $xhtml * @param bool $xhtml
* @param bool $html5 * @param bool $html5
*/ */
public function quick_build($view, $data, $xhtml=TRUE, $html5=TRUE) public function quick_build($view, $data, $xhtml = TRUE, $html5 = TRUE)
{ {
//Set up header //Set up header
if ($title != '') if ($title != '')
@ -492,7 +486,7 @@ class Page {
*/ */
public function num_queries() public function num_queries()
{ {
return (isset($this->CI->db)) ? count($this->CI->db->queries) : 0; return (isset($this->CI->db)) ? count($this->CI->db->queries) : 0;
} }
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
@ -508,7 +502,7 @@ class Page {
public function set_message($type, $message) public function set_message($type, $message)
{ {
$data['stat_class'] = $type; $data['stat_class'] = $type;
$data['message'] = $message; $data['message'] = $message;
$this->CI->load->view('message', $data); $this->CI->load->view('message', $data);
} }