Added a class for handling hard caching and minification of [dynamic] content.
This commit is contained in:
parent
29f3275988
commit
66088b7613
3 changed files with 249 additions and 6 deletions
|
@ -59,16 +59,20 @@ class Display_PHP extends Display_Common
|
|||
{
|
||||
// Starts up the buffer
|
||||
ob_start();
|
||||
|
||||
// Puts the class variables in local scope for the template
|
||||
$form_class = (isset($this->config->pickles['classes']['Form']) ? $this->config->pickles['classes']['Form'] : 'Form');
|
||||
|
||||
|
||||
// Puts the class variables in local scope of the template
|
||||
$__config = $this->config;
|
||||
$__meta = $this->meta_data;
|
||||
$__module = $this->module_return;
|
||||
$__css_class = $this->css_class;
|
||||
$__js_file = $this->js_basename;
|
||||
$__form = new $form_class();
|
||||
|
||||
// Creates (possibly overwritten) objects
|
||||
$form_class = (isset($this->config->pickles['classes']['Form']) ? $this->config->pickles['classes']['Form'] : 'Form');
|
||||
$dynamic_class = (isset($this->config->pickles['classes']['Dynamic']) ? $this->config->pickles['classes']['Dynamic'] : 'Dynamic');
|
||||
|
||||
$__form = new $form_class();
|
||||
$__dynamic = new $dynamic_class();
|
||||
|
||||
// Loads the template
|
||||
if ($this->parent_template != null)
|
||||
|
|
236
classes/Dynamic.php
Normal file
236
classes/Dynamic.php
Normal file
|
@ -0,0 +1,236 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Dynamic Content Class File for PICKLES
|
||||
*
|
||||
* PHP version 5
|
||||
*
|
||||
* Licensed under The MIT License
|
||||
* Redistribution of these files must retain the above copyright notice.
|
||||
*
|
||||
* @author Josh Sherman <josh@gravityblvd.com>
|
||||
* @copyright Copyright 2007-2010, Gravity Boulevard, LLC
|
||||
* @license http://www.opensource.org/licenses/mit-license.html
|
||||
* @package PICKLES
|
||||
* @link http://p.ickl.es
|
||||
*/
|
||||
|
||||
/**
|
||||
* Dynamic Class
|
||||
*
|
||||
* Handles generating links to static content that are a timestamp injected as
|
||||
* to avoid hard caching. Also minifies content where applicable.
|
||||
*/
|
||||
class Dynamic extends Object
|
||||
{
|
||||
/**
|
||||
* Generate Reference
|
||||
*
|
||||
* Appends a dynamic piece of information to the passed reference in the
|
||||
* form of a UNIX timestamp added to the query string.
|
||||
*
|
||||
* @param string $reference URI reference of the file
|
||||
* @return string URI reference reference with dynamic content
|
||||
*/
|
||||
public function ref($reference)
|
||||
{
|
||||
// Checks if the URI reference is absolute, and not relative
|
||||
if (substr($reference, 0, 1) == '/')
|
||||
{
|
||||
$query_string = '';
|
||||
|
||||
// Checks for ? and extracts query string
|
||||
if (strstr($reference, '?'))
|
||||
{
|
||||
list($reference, $query_string) = explode('?', $reference);
|
||||
}
|
||||
|
||||
// Adds the dot so the file functions can find the file
|
||||
$file = '.' . $reference;
|
||||
|
||||
if (file_exists($file))
|
||||
{
|
||||
// Replaces the extension with time().extension
|
||||
$parts = explode('.', $reference);
|
||||
|
||||
if (count($parts) == 1)
|
||||
{
|
||||
throw new Exception('Filename must have an extension (e.g. /path/to/file.png)');
|
||||
}
|
||||
else
|
||||
{
|
||||
end($parts);
|
||||
$parts[key($parts)] = filemtime($file) . '.' . current($parts);
|
||||
$reference = implode('.', $parts);
|
||||
}
|
||||
|
||||
// Adds the query string back
|
||||
if ($query_string != '')
|
||||
{
|
||||
$reference .= '?' . $query_string;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception('Supplied reference does not exist');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception('Reference value must be absolute (e.g. /path/to/file.png)');
|
||||
}
|
||||
|
||||
return $reference;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Javascript Reference
|
||||
*
|
||||
* Attempts to minify the source with Google's Closure compiler, and then
|
||||
* returns the reference URI for the file, minified or not.
|
||||
*
|
||||
* @link http://code.google.com/closure/compiler/
|
||||
* @param string $reference URI reference of the Javascript file
|
||||
* @return string URI reference reference with dynamic content
|
||||
*/
|
||||
public function js($original_reference, $level = 'simple')
|
||||
{
|
||||
$level = strtoupper($level);
|
||||
|
||||
switch ($level)
|
||||
{
|
||||
CASE 'WHITESPACE':
|
||||
CASE 'SIMPLE':
|
||||
CASE 'ADVANCED':
|
||||
// Injects .min into the filename
|
||||
$parts = explode('.', $original_reference);
|
||||
|
||||
if (count($parts) == 1)
|
||||
{
|
||||
throw new Exception('Filename must have an extension (e.g. /path/to/file.js)');
|
||||
}
|
||||
else
|
||||
{
|
||||
end($parts);
|
||||
$parts[key($parts)] = 'min.' . current($parts);
|
||||
$minified_reference = implode('.', $parts);
|
||||
}
|
||||
|
||||
$original_filename = '.' . $original_reference;
|
||||
$minified_filename = '.' . $minified_reference;
|
||||
|
||||
$path = dirname($original_filename);
|
||||
|
||||
if (file_exists($original_filename))
|
||||
{
|
||||
$reference = $original_reference;
|
||||
|
||||
if (is_writable($path) && (!file_exists($minified_filename) || filemtime($original_filename) > filemtime($minified_filename)) && extension_loaded('curl'))
|
||||
{
|
||||
// Sets up the options list
|
||||
$options = array(
|
||||
CURLOPT_URL => 'http://closure-compiler.appspot.com/compile',
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded; charset=utf-8'),
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_POSTFIELDS => 'js_code=' . urlencode(file_get_contents($original_filename)) . '&compilation_level=' . ($level . '_' . ($level == 'WHITESPACE' ? 'ONLY' : 'OPTIMIZATIONS')) . '&output_format=text&output_info=compiled_code'
|
||||
);
|
||||
|
||||
try
|
||||
{
|
||||
// Executes the request
|
||||
$curl = curl_init();
|
||||
curl_setopt_array($curl, $options);
|
||||
file_put_contents($minified_filename, curl_exec($curl));
|
||||
curl_close($curl);
|
||||
|
||||
$reference = $minified_reference;
|
||||
}
|
||||
catch (Exception $exception)
|
||||
{
|
||||
$reference = $original_reference;
|
||||
}
|
||||
}
|
||||
elseif (file_exists($minified_filename))
|
||||
{
|
||||
$reference = $minified_reference;
|
||||
}
|
||||
|
||||
$reference = $this->ref($reference);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception('Supplied reference does not exist');
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new Exception('The level "' . $level . '" is invalid. Valid levels include "whitespace", "simple" and "advanced"');
|
||||
break;
|
||||
}
|
||||
|
||||
return $reference;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate Stylesheet Reference
|
||||
*
|
||||
* Attempts to minify the stylesheet and then returns the reference URI
|
||||
* for the file, minified or not.
|
||||
*
|
||||
* @param string $reference URI reference of the Stylesheet
|
||||
* @return string URI reference reference with dynamic content
|
||||
*/
|
||||
public function css($original_reference)
|
||||
{
|
||||
// Injects .min into the filename
|
||||
$parts = explode('.', $original_reference);
|
||||
|
||||
if (count($parts) == 1)
|
||||
{
|
||||
throw new Exception('Filename must have an extension (e.g. /path/to/file.css)');
|
||||
}
|
||||
else
|
||||
{
|
||||
end($parts);
|
||||
$parts[key($parts)] = 'min.' . current($parts);
|
||||
$minified_reference = implode('.', $parts);
|
||||
}
|
||||
|
||||
$original_filename = '.' . $original_reference;
|
||||
$minified_filename = '.' . $minified_reference;
|
||||
|
||||
$path = dirname($original_filename);
|
||||
|
||||
if (file_exists($original_filename))
|
||||
{
|
||||
$reference = $original_reference;
|
||||
|
||||
if (is_writable($path) && (!file_exists($minified_filename) || filemtime($original_filename) > filemtime($minified_filename)))
|
||||
{
|
||||
// Minifies CSS with a few basic character replacements.
|
||||
$stylesheet = file_get_contents($original_filename);
|
||||
$stylesheet = str_replace(array("\t", "\n", ', ', ' {', ': ', ';}'), array('', '', ',', '{', ':', '}'), $stylesheet);
|
||||
$stylesheet = preg_replace('/\/\*.+?\*\//', '', $stylesheet);
|
||||
file_put_contents($minified_filename, $stylesheet);
|
||||
|
||||
$reference = $minified_reference;
|
||||
}
|
||||
elseif (file_exists($minified_filename))
|
||||
{
|
||||
$reference = $minified_reference;
|
||||
}
|
||||
|
||||
$reference = $this->ref($reference);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception('Supplied reference does not exist');
|
||||
}
|
||||
|
||||
return $reference;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -12,11 +12,14 @@ RewriteBase /
|
|||
# Strips the trailing slash
|
||||
RewriteRule ^(.+)/$ $1 [R]
|
||||
|
||||
# Makes sure to skip rewriting actual files and directories
|
||||
# Makes sure to skip rewriting files and directories that really exist
|
||||
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
|
||||
RewriteCond %{REQUEST_FILENAME} -d [NC]
|
||||
RewriteRule .* - [L]
|
||||
|
||||
# Rewrites "dynamic" content filenames with dynamic timestamps data
|
||||
RewriteRule ^(.+)\.([\d]+)\.(css|js|gif|png|jpg|jpeg)$ /$1.$3 [NC,QSA]
|
||||
|
||||
# One rewrite to rule them all
|
||||
RewriteRule ^(.+)$ index.php?request=$1 [NC,QSA]
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue