Mombu the Php Forum sponsored links

Go Back   Mombu the Php Forum > Php > note 71297 added to function.ob-start
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 23rd December 12:13
php-general
External User
 
Posts: 1
Default note 71297 added to function.ob-start


In extension to the compress() function posted below, here's a nifty little class that improves the idea a bit. Basically, running that compress() function for all your CSS for every single page load is clearly far less than optimal, especially since the styles will change only infrequently at the very worst.

With this class you can simply specify an array of your CSS file names and call dump_style(). The contents of each file are saved in compress()'d form in a cache file that is only recreated when the corresponding source CSS changes.

It's intended for PHP5, but will work identically if you just un-OOP everything and possibly define file_put_contents.

Enjoy!

<?php

$CSS_FILES = array(
'_general.css'
);

$css_cache = new CSSCache($CSS_FILES);
$css_cache->dump_style();

//
// class CSSCache
//

class CSSCache {
private $filenames = array();
private $cwd;

public function __construct($i_filename_arr) {
if (!is_array($i_filename_arr))
$i_filename_arr = array($i_filename_arr);

$this->filenames = $i_filename_arr;
$this->cwd = getcwd() . DIRECTORY_SEPARATOR;

if ($this->style_changed())
$expire = -72000;
else
$expire = 3200;

header('Content-Type: text/css; charset: UTF-8');
header('Cache-Control: must-revalidate');
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expire) . ' GMT');
}

public function dump_style() {
ob_start('ob_gzhandler');

foreach ($this->filenames as $filename)
$this->dump_cache_contents($filename);

ob_end_flush();
}

private function get_cache_name($filename, $wildcard = FALSE) {
$stat = stat($filename);
return $this->cwd . '.' . $filename . '.' .
($wildcard ? '*' : ($stat['size'] . '-' . $stat['mtime'])) . '.cache';
}

private function style_changed() {
foreach ($this->filenames as $filename)
if (!is_file($this->get_cache_name($filename)))
return TRUE;
return FALSE;
}

private function compress($buffer) {
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' '), '', $buffer);
$buffer = str_replace('{ ', '{', $buffer);
$buffer = str_replace(' }', '}', $buffer);
$buffer = str_replace('; ', ';', $buffer);
$buffer = str_replace(', ', ',', $buffer);
$buffer = str_replace(' {', '{', $buffer);
$buffer = str_replace('} ', '}', $buffer);
$buffer = str_replace(': ', ':', $buffer);
$buffer = str_replace(' ,', ',', $buffer);
$buffer = str_replace(' ;', ';', $buffer);
return $buffer;
}

private function dump_cache_contents($filename) {
$current_cache = $this->get_cache_name($filename);

// the cache exists - just dump it
if (is_file($current_cache)) {
include($current_cache);
return;
}

// remove any old, lingering caches for this file
if ($dead_files = glob($this->get_cache_name($filename, TRUE), GLOB_NOESCAPE))
foreach ($dead_files as $dead_file)
unlink($dead_file);

$compressed = $this->compress(file_get_contents($filename));
file_put_contents($current_cache, $compressed);

echo $compressed;
}
}

?>
----
Server IP: 64.71.164.2
Probable Submitter: 208.247.75.49
----
Manual Page -- http://www.php.net/manual/en/function.ob-start.php
Edit -- https://master.php.net/note/edit/71297
Del: integrated -- https://master.php.net/note/delete/71297/integrated
Del: useless -- https://master.php.net/note/delete/71297/useless
Del: bad code -- https://master.php.net/note/delete/71297/bad+code
Del: spam -- https://master.php.net/note/delete/71297/spam
Del: non-english -- https://master.php.net/note/delete/71297/non-english
Del: in docs -- https://master.php.net/note/delete/71297/in+docs
Del: other reasons-- https://master.php.net/note/delete/71297
Reject -- https://master.php.net/note/reject/71297
Search -- https://master.php.net/manage/user-notes.php
  Reply With Quote


  sponsored links


2 23rd December 12:28
bobby
External User
 
Posts: 1
Default note 71297 deleted from function.ob-start by bobby


Note Submitter:

----

In extension to the compress() function posted below, here's a nifty little class that improves the idea a bit. Basically, running that compress() function for all your CSS for every single page load is clearly far less than optimal, especially since the styles will change only infrequently at the very worst.

With this class you can simply specify an array of your CSS file names and call dump_style(). The contents of each file are saved in compress()'d form in a cache file that is only recreated when the corresponding source CSS changes.

It's intended for PHP5, but will work identically if you just un-OOP everything and possibly define file_put_contents.

Enjoy!

<?php

$CSS_FILES = array(
'_general.css'
);

$css_cache = new CSSCache($CSS_FILES);
$css_cache->dump_style();

//
// class CSSCache
//

class CSSCache {
private $filenames = array();
private $cwd;

public function __construct($i_filename_arr) {
if (!is_array($i_filename_arr))
$i_filename_arr = array($i_filename_arr);

$this->filenames = $i_filename_arr;
$this->cwd = getcwd() . DIRECTORY_SEPARATOR;

if ($this->style_changed())
$expire = -72000;
else
$expire = 3200;

header('Content-Type: text/css; charset: UTF-8');
header('Cache-Control: must-revalidate');
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expire) . ' GMT');
}

public function dump_style() {
ob_start('ob_gzhandler');

foreach ($this->filenames as $filename)
$this->dump_cache_contents($filename);

ob_end_flush();
}

private function get_cache_name($filename, $wildcard = FALSE) {
$stat = stat($filename);
return $this->cwd . '.' . $filename . '.' .
($wildcard ? '*' : ($stat['size'] . '-' . $stat['mtime'])) . '.cache';
}

private function style_changed() {
foreach ($this->filenames as $filename)
if (!is_file($this->get_cache_name($filename)))
return TRUE;
return FALSE;
}

private function compress($buffer) {
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' '), '', $buffer);
$buffer = str_replace('{ ', '{', $buffer);
$buffer = str_replace(' }', '}', $buffer);
$buffer = str_replace('; ', ';', $buffer);
$buffer = str_replace(', ', ',', $buffer);
$buffer = str_replace(' {', '{', $buffer);
$buffer = str_replace('} ', '}', $buffer);
$buffer = str_replace(': ', ':', $buffer);
$buffer = str_replace(' ,', ',', $buffer);
$buffer = str_replace(' ;', ';', $buffer);
return $buffer;
}

private function dump_cache_contents($filename) {
$current_cache = $this->get_cache_name($filename);

// the cache exists - just dump it
if (is_file($current_cache)) {
include($current_cache);
return;
}

// remove any old, lingering caches for this file
if ($dead_files = glob($this->get_cache_name($filename, TRUE), GLOB_NOESCAPE))
foreach ($dead_files as $dead_file)
unlink($dead_file);

$compressed = $this->compress(file_get_contents($filename));
file_put_contents($current_cache, $compressed);

echo $compressed;
}
}

?>
  Reply With Quote


  sponsored links


Reply


Thread Tools
Display Modes




Copyright 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666