1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-13 01:24:51 +02:00

Moved file caching to Minify_Cache_File.

Removed Cache_Lite.
Deprecated UseServerCache() in favor of setCache().
Added Groups controller to ab tests.
This commit is contained in:
Steve Clay
2008-06-22 15:08:30 +00:00
parent a6d7115a6c
commit 4f615c12f3
15 changed files with 201 additions and 1058 deletions

98
lib/Minify/Cache/File.php Normal file
View File

@@ -0,0 +1,98 @@
<?php
/**
* Class Minify_Cache_File
* @package Minify
*/
class Minify_Cache_File {
public function __construct($path = '')
{
if (! $path) {
require_once 'Solar/Dir.php';
$path = rtrim(Solar_Dir::tmp(), DIRECTORY_SEPARATOR);
}
$this->_path = $path;
}
/**
* Write data to cache.
*
* @param string $id cache id (e.g. a filename)
*
* @param string $data
*
* @return bool success
*/
public function store($id, $data)
{
return self::_verifiedWrite($this->_path . '/' . $id, $data);
}
/**
* Get the size of a cache entry
*
* @param string $id cache id (e.g. a filename)
*
* @return int size in bytes
*/
public function getSize($id)
{
return filesize($this->_path . '/' . $id);
}
/**
* Does a valid cache entry exist?
*
* @param string $id cache id (e.g. a filename)
*
* @param int $srcMtime mtime of the original source file(s)
*
* @return bool exists
*/
public function isValid($id, $srcMtime)
{
$file = $this->_path . '/' . $id;
return (file_exists($file) && (filemtime($file) >= $srcMtime));
}
/**
* Send the cached content to output
*
* @param string $id cache id (e.g. a filename)
*/
public function display($id)
{
readfile($this->_path . '/' . $id);
}
/**
* Fetch the cached content
*
* @param string $id cache id (e.g. a filename)
*
* @return string
*/
public function fetch($id)
{
return file_get_contents($this->_path . '/' . $id);
}
private $_path = null;
/**
* Write data to file and verify its contents
*
* @param string $file path
*
* @param string $data
*
* @return bool success
*/
private static function _verifiedWrite($file, $data)
{
return (file_put_contents($file, $data, LOCK_EX)
&& (md5($data) === md5_file($file))
);
}
}

View File

@@ -43,7 +43,7 @@ abstract class Minify_Controller_Base {
,'encodeOutput' => true
,'encodeMethod' => null // determine later
,'encodeLevel' => 9
,'perType' => array() // no per-type minifier options
,'minifierOptions' => array() // no minifier options
,'contentTypeCharset' => 'UTF-8'
,'setExpires' => null // use conditional GET
,'quiet' => false // serve() will send headers and output

View File

@@ -46,6 +46,10 @@ class Minify_Controller_Groups extends Minify_Controller_Base {
$groups = $options['groups'];
unset($options['groups']);
if (! isset($_SERVER['PATH_INFO'])) {
// no PATH_INFO
return $options;
}
$pi = substr($_SERVER['PATH_INFO'], 1);
if (! isset($groups[$pi])) {
// not a valid group

View File

@@ -31,7 +31,7 @@ class Minify_Controller_Version1 extends Minify_Controller_Base {
$cacheDir = defined('MINIFY_CACHE_DIR')
? MINIFY_CACHE_DIR
: null;
Minify::useServerCache($cacheDir);
Minify::setCache($cacheDir);
}
$options['badRequestHeader'] = 'HTTP/1.0 404 Not Found';
$options['contentTypeCharset'] = MINIFY_ENCODING;