1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-12 09:05:08 +02:00

Issue 167, Issue 186, integrated Solar_Dir::tmp into file cache

This commit is contained in:
Steve Clay
2010-07-25 05:11:41 +00:00
parent 5e0a7b1bf9
commit 5d9d81f875
11 changed files with 258 additions and 213 deletions

View File

@@ -200,7 +200,7 @@ var MUB = {
* Runs on DOMready
*/
,init : function () {
$('#jsDidntLoad').hide();
$('#jsDidntLoad').remove();
$('#app').show();
$('#sources').html('');
$('#add button').click(MUB.addButtonClick);

View File

@@ -33,7 +33,7 @@ $setIncludeSuccess = set_include_path(dirname(__FILE__) . '/../lib' . PATH_SEPAR
// we do it this way because we want the builder to work after the user corrects
// include_path. (set_include_path returning FALSE is OK).
try {
require_once 'Solar/Dir.php';
require_once 'Minify/Cache/File.php';
} catch (Exception $e) {
if (! $setIncludeSuccess) {
echo "Minify: set_include_path() failed. You may need to set your include_path "
@@ -46,8 +46,8 @@ try {
require 'Minify.php';
$cachePathCode = '';
if (! isset($min_cachePath)) {
$detectedTmp = rtrim(Solar_Dir::tmp(), DIRECTORY_SEPARATOR);
if (! isset($min_cachePath) && ! function_exists('sys_get_temp_dir')) {
$detectedTmp = Minify_Cache_File::tmp();
$cachePathCode = "\$min_cachePath = " . var_export($detectedTmp, 1) . ';';
}
@@ -73,6 +73,7 @@ b {color:#c00}
.topNote {background: #ff9; display:inline-block; padding:.5em .6em; margin:0 0 1em;}
.topWarning {background:#c00; color:#fff; padding:.5em .6em; margin:0 0 1em;}
.topWarning a {color:#fff;}
#jsDidntLoad {display:none;}
</style>
<body>
<?php if ($symlinkOption): ?>
@@ -179,11 +180,10 @@ by Minify. E.g. <code>@import "<span class=minRoot>/min/?</span>g=css2";</code><
<script>
$(function () {
// give Minify a few seconds to serve _index.js before showing scary red warning
$('#jsDidntLoad').hide();
setTimeout(function () {
if (! window.MUB) {
// Minify didn't load
$('#jsDidntLoad').show();
$('#jsDidntLoad').css({display:'block'});
}
}, 3000);

View File

@@ -49,6 +49,12 @@ $min_enableBuilder = true;
//$min_cachePath = 'c:\\WINDOWS\\Temp';
//$min_cachePath = '/tmp';
//$min_cachePath = preg_replace('/^\\d+;/', '', session_save_path());
/**
* To use APC/Memcache/ZendPlatform for cache storage, require the class and
* set $min_cachePath to an instance. Example below:
*/
//require dirname(__FILE__) . '/lib/Minify/Cache/APC.php';
//$min_cachePath = new Minify_Cache_APC();
/**

View File

@@ -211,7 +211,9 @@ class HTTP_ConditionalGet {
{
$headers = $this->_headers;
if (array_key_exists('_responseCode', $headers)) {
header($headers['_responseCode']);
// FastCGI environments require 3rd arg to header() to be set
list(, $code) = explode(' ', $headers['_responseCode'], 3);
header($headers['_responseCode'], true, $code);
unset($headers['_responseCode']);
}
foreach ($headers as $name => $val) {

View File

@@ -423,7 +423,9 @@ class Minify {
$url = htmlspecialchars($url, ENT_QUOTES);
list(,$h1) = explode(' ', $header, 2);
$h1 = htmlspecialchars($h1);
header($header);
// FastCGI environments require 3rd arg to header() to be set
list(, $code) = explode(' ', $header, 3);
header($header, true, $code);
header('Content-Type: text/html; charset=utf-8');
echo "<h1>$h1</h1>";
echo "<p>Please see <a href='$url'>$url</a>.</p>";

View File

@@ -9,8 +9,7 @@ class Minify_Cache_File {
public function __construct($path = '', $fileLocking = false)
{
if (! $path) {
require_once 'Solar/Dir.php';
$path = rtrim(Solar_Dir::tmp(), DIRECTORY_SEPARATOR);
$path = self::tmp();
}
$this->_locking = $fileLocking;
$this->_path = $path;
@@ -123,6 +122,67 @@ class Minify_Cache_File {
return $this->_path;
}
/**
* Get a usable temp directory
*
* Adapted from Solar/Dir.php
* @author Paul M. Jones <pmjones@solarphp.com>
* @license http://opensource.org/licenses/bsd-license.php BSD
* @link http://solarphp.com/trac/core/browser/trunk/Solar/Dir.php
*
* @return string
*/
public static function tmp()
{
static $tmp = null;
if (! $tmp) {
$tmp = function_exists('sys_get_temp_dir')
? sys_get_temp_dir()
: self::_tmp();
$tmp = rtrim($tmp, DIRECTORY_SEPARATOR);
}
return $tmp;
}
/**
* Returns the OS-specific directory for temporary files
*
* @author Paul M. Jones <pmjones@solarphp.com>
* @license http://opensource.org/licenses/bsd-license.php BSD
* @link http://solarphp.com/trac/core/browser/trunk/Solar/Dir.php
*
* @return string
*/
protected static function _tmp()
{
// non-Windows system?
if (strtolower(substr(PHP_OS, 0, 3)) != 'win') {
$tmp = empty($_ENV['TMPDIR']) ? getenv('TMPDIR') : $_ENV['TMPDIR'];
if ($tmp) {
return $tmp;
} else {
return '/tmp';
}
}
// Windows 'TEMP'
$tmp = empty($_ENV['TEMP']) ? getenv('TEMP') : $_ENV['TEMP'];
if ($tmp) {
return $tmp;
}
// Windows 'TMP'
$tmp = empty($_ENV['TMP']) ? getenv('TMP') : $_ENV['TMP'];
if ($tmp) {
return $tmp;
}
// Windows 'windir'
$tmp = empty($_ENV['windir']) ? getenv('windir') : $_ENV['windir'];
if ($tmp) {
return $tmp;
}
// final fallback for Windows
return getenv('SystemRoot') . '\\temp';
}
/**
* Send message to the Minify logger
* @param string $msg

View File

@@ -0,0 +1,142 @@
<?php
/**
* Class Minify_Cache_ZendPlatform
* @package Minify
*/
/**
* ZendPlatform-based cache class for Minify
*
* Based on Minify_Cache_APC, uses output_cache_get/put (currently deprecated)
*
* <code>
* Minify::setCache(new Minify_Cache_ZendPlatform());
* </code>
*
* @package Minify
* @author Patrick van Dissel
*/
class Minify_Cache_ZendPlatform {
/**
* Create a Minify_Cache_ZendPlatform object, to be passed to
* Minify::setCache().
*
* @param int $expire seconds until expiration (default = 0
* meaning the item will not get an expiration date)
*
* @return null
*/
public function __construct($expire = 0)
{
$this->_exp = $expire;
}
/**
* Write data to cache.
*
* @param string $id cache id
*
* @param string $data
*
* @return bool success
*/
public function store($id, $data)
{
return output_cache_put($id, "{$_SERVER['REQUEST_TIME']}|{$data}");
}
/**
* Get the size of a cache entry
*
* @param string $id cache id
*
* @return int size in bytes
*/
public function getSize($id)
{
return $this->_fetch($id)
? strlen($this->_data)
: false;
}
/**
* Does a valid cache entry exist?
*
* @param string $id cache id
*
* @param int $srcMtime mtime of the original source file(s)
*
* @return bool exists
*/
public function isValid($id, $srcMtime)
{
$ret = ($this->_fetch($id) && ($this->_lm >= $srcMtime));
return $ret;
}
/**
* Send the cached content to output
*
* @param string $id cache id
*/
public function display($id)
{
echo $this->_fetch($id)
? $this->_data
: '';
}
/**
* Fetch the cached content
*
* @param string $id cache id
*
* @return string
*/
public function fetch($id)
{
return $this->_fetch($id)
? $this->_data
: '';
}
private $_exp = null;
// cache of most recently fetched id
private $_lm = null;
private $_data = null;
private $_id = null;
/**
* Fetch data and timestamp from ZendPlatform, store in instance
*
* @param string $id
*
* @return bool success
*/
private function _fetch($id)
{
if ($this->_id === $id) {
return true;
}
$ret = output_cache_get($id, $this->_exp);
if (false === $ret) {
$this->_id = null;
return false;
}
list($this->_lm, $this->_data) = explode('|', $ret, 2);
$this->_id = $id;
return true;
}
}

View File

@@ -1,199 +0,0 @@
<?php
/**
*
* Utility class for static directory methods.
*
* @category Solar
*
* @package Solar
*
* @author Paul M. Jones <pmjones@solarphp.com>
*
* @license http://opensource.org/licenses/bsd-license.php BSD
*
* @version $Id: Dir.php 2926 2007-11-09 16:25:44Z pmjones $
*
*/
class Solar_Dir {
/**
*
* The OS-specific temporary directory location.
*
* @var string
*
*/
protected static $_tmp;
/**
*
* Hack for [[php::is_dir() | ]] that checks the include_path.
*
* Use this to see if a directory exists anywhere in the include_path.
*
* {{code: php
* $dir = Solar_Dir::exists('path/to/dir')
* if ($dir) {
* $files = scandir($dir);
* } else {
* echo "Not found in the include-path.";
* }
* }}
*
* @param string $dir Check for this directory in the include_path.
*
* @return mixed If the directory exists in the include_path, returns the
* absolute path; if not, returns boolean false.
*
*/
public static function exists($dir)
{
// no file requested?
$dir = trim($dir);
if (! $dir) {
return false;
}
// using an absolute path for the file?
// dual check for Unix '/' and Windows '\',
// or Windows drive letter and a ':'.
$abs = ($dir[0] == '/' || $dir[0] == '\\' || $dir[1] == ':');
if ($abs && is_dir($dir)) {
return $dir;
}
// using a relative path on the file
$path = explode(PATH_SEPARATOR, ini_get('include_path'));
foreach ($path as $base) {
// strip Unix '/' and Windows '\'
$target = rtrim($base, '\\/') . DIRECTORY_SEPARATOR . $dir;
if (is_dir($target)) {
return $target;
}
}
// never found it
return false;
}
/**
*
* "Fixes" a directory string for the operating system.
*
* Use slashes anywhere you need a directory separator. Then run the
* string through fixdir() and the slashes will be converted to the
* proper separator (for example '\' on Windows).
*
* Always adds a final trailing separator.
*
* @param string $dir The directory string to 'fix'.
*
* @return string The "fixed" directory string.
*
*/
public static function fix($dir)
{
$dir = str_replace('/', DIRECTORY_SEPARATOR, $dir);
return rtrim($dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}
/**
*
* Convenience method for dirname() and higher-level directories.
*
* @param string $file Get the dirname() of this file.
*
* @param int $up Move up in the directory structure this many
* times, default 0.
*
* @return string The dirname() of the file.
*
*/
public static function name($file, $up = 0)
{
$dir = dirname($file);
while ($up --) {
$dir = dirname($dir);
}
return $dir;
}
/**
*
* Returns the OS-specific directory for temporary files.
*
* @param string $sub Add this subdirectory to the returned temporary
* directory name.
*
* @return string The temporary directory path.
*
*/
public static function tmp($sub = '')
{
// find the tmp dir if needed
if (! Solar_Dir::$_tmp) {
// use the system if we can
if (function_exists('sys_get_temp_dir')) {
$tmp = sys_get_temp_dir();
} else {
$tmp = Solar_Dir::_tmp();
}
// remove trailing separator and save
Solar_Dir::$_tmp = rtrim($tmp, DIRECTORY_SEPARATOR);
}
// do we have a subdirectory request?
$sub = trim($sub);
if ($sub) {
// remove leading and trailing separators, and force exactly
// one trailing separator
$sub = trim($sub, DIRECTORY_SEPARATOR)
. DIRECTORY_SEPARATOR;
}
return Solar_Dir::$_tmp . DIRECTORY_SEPARATOR . $sub;
}
/**
*
* Returns the OS-specific temporary directory location.
*
* @return string The temp directory path.
*
*/
protected static function _tmp()
{
// non-Windows system?
if (strtolower(substr(PHP_OS, 0, 3)) != 'win') {
$tmp = empty($_ENV['TMPDIR']) ? getenv('TMPDIR') : $_ENV['TMPDIR'];
if ($tmp) {
return $tmp;
} else {
return '/tmp';
}
}
// Windows 'TEMP'
$tmp = empty($_ENV['TEMP']) ? getenv('TEMP') : $_ENV['TEMP'];
if ($tmp) {
return $tmp;
}
// Windows 'TMP'
$tmp = empty($_ENV['TMP']) ? getenv('TMP') : $_ENV['TMP'];
if ($tmp) {
return $tmp;
}
// Windows 'windir'
$tmp = empty($_ENV['windir']) ? getenv('windir') : $_ENV['windir'];
if ($tmp) {
return $tmp;
}
// final fallback for Windows
return getenv('SystemRoot') . '\\temp';
}
}

View File

@@ -5,13 +5,11 @@ require_once 'Minify/Cache/File.php';
function test_Minify_Cache_File()
{
global $minifyCachePath;
$data = str_repeat(md5(time()) . 'í', 100); // 3400 bytes in UTF-8
$id = 'Minify_test_cache_noLock';
$prefix = 'Minify_Cache_File : ';
$cache = new Minify_Cache_File($minifyCachePath);
$cache = new Minify_Cache_File();
echo "NOTE: Minify_Cache_File : path is set to: '" . $cache->getPath() . "'.\n";
@@ -33,7 +31,7 @@ function test_Minify_Cache_File()
// test with locks
$id = 'Minify_test_cache_withLock';
$cache = new Minify_Cache_File($minifyCachePath, true);
$cache = new Minify_Cache_File('', true);
assertTrue(true === $cache->store($id, $data), $prefix . 'store w/ lock');

View File

@@ -0,0 +1,33 @@
<?php
require_once '_inc.php';
require_once 'Minify/Cache/ZendPlatform.php';
function test_Minify_Cache_ZendPlatform()
{
$prefix = 'Minify_Cache_ZendPlatform : ';
if (! function_exists('output_cache_put')) {
return;
}
$data = str_repeat(md5(time()) . 'í', 100); // 3400 bytes in UTF-8
$id = 'Minify_test_cache';
$cache = new Minify_Cache_ZendPlatform();
assertTrue(true === $cache->store($id, $data), $prefix . 'store');
assertTrue(countBytes($data) === $cache->getSize($id), $prefix . 'getSize');
assertTrue(true === $cache->isValid($id, $_SERVER['REQUEST_TIME'] - 10), $prefix . 'isValid');
ob_start();
$cache->display($id);
$displayed = ob_get_contents();
ob_end_clean();
assertTrue($data === $displayed, $prefix . 'display');
assertTrue($data === $cache->fetch($id), $prefix . 'fetch');
}
test_Minify_Cache_ZendPlatform();

View File

@@ -6,6 +6,7 @@ require 'test_Minify_HTML_Helper.php';
require 'test_Minify_Cache_APC.php';
require 'test_Minify_Cache_File.php';
require 'test_Minify_Cache_Memcache.php';
require 'test_Minify_Cache_ZendPlatform.php';
require 'test_Minify_CSS.php';
require 'test_Minify_CSS_UriRewriter.php';
require 'test_Minify_CommentPreserver.php';