mirror of
https://github.com/mrclay/minify.git
synced 2025-08-16 02:54:33 +02:00
Work on better HTML helper
This commit is contained in:
43
min/builder/test.php
Normal file
43
min/builder/test.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
exit();
|
||||
/* currently unused.
|
||||
|
||||
// capture PHP's default setting (may get overridden in config
|
||||
$_oc = ini_get('zlib.output_compression');
|
||||
|
||||
// allow access only if builder is enabled
|
||||
require dirname(__FILE__) . '/../config.php';
|
||||
if (! $min_enableBuilder) {
|
||||
exit();
|
||||
}
|
||||
|
||||
if (isset($_GET['oc'])) {
|
||||
header('Content-Type: text/plain');
|
||||
echo (int)$_oc;
|
||||
|
||||
} elseif (isset($_GET['text']) && in_array($_GET['text'], array('js', 'css', 'fake'))) {
|
||||
ini_set('zlib.output_compression', '0');
|
||||
$type = ($_GET['text'] == 'js')
|
||||
? 'application/x-javascript'
|
||||
: "text/{$_GET['text']}";
|
||||
header("Content-Type: {$type}");
|
||||
echo 'Hello';
|
||||
|
||||
} elseif (isset($_GET['docroot'])) {
|
||||
if (false === realpath($_SERVER['DOCUMENT_ROOT'])) {
|
||||
echo "<p class=topWarning><strong>realpath(DOCUMENT_ROOT) failed.</strong> You may need "
|
||||
. "to set \$min_documentRoot manually (hopefully realpath() is not "
|
||||
. "broken in your environment).</p>";
|
||||
}
|
||||
if (0 !== strpos(realpath(__FILE__), realpath($_SERVER['DOCUMENT_ROOT']))) {
|
||||
echo "<p class=topWarning><strong>DOCUMENT_ROOT doesn't contain this file.</strong> You may "
|
||||
. " need to set \$min_documentRoot manually</p>";
|
||||
}
|
||||
if (isset($_SERVER['SUBDOMAIN_DOCUMENT_ROOT'])) {
|
||||
echo "<p class=topNote><strong>\$_SERVER['SUBDOMAIN_DOCUMENT_ROOT'] is set.</strong> "
|
||||
. "You may need to set \$min_documentRoot to this in config.php</p>";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//*/
|
184
min/lib/Minify/HTML/Helper.php
Normal file
184
min/lib/Minify/HTML/Helper.php
Normal file
@@ -0,0 +1,184 @@
|
||||
<?php
|
||||
/**
|
||||
* Class Minify_HTML_Helper
|
||||
* @package Minify
|
||||
*/
|
||||
|
||||
/**
|
||||
* Helpers for writing Minfy URIs into HTML
|
||||
*
|
||||
* @package Minify
|
||||
* @author Stephen Clay <steve@mrclay.org>
|
||||
*/
|
||||
class Minify_HTML_Helper {
|
||||
public $rewriteWorks = true;
|
||||
public $minAppUri = '/min';
|
||||
|
||||
public static function groupUri($key, $farExpires = true, $debug = false, $charset = 'UTF-8')
|
||||
{
|
||||
$h = new self;
|
||||
$h->setGroup($key, $farExpires);
|
||||
$uri = $h->getRawUri($farExpires, $debug);
|
||||
return htmlspecialchars($uri, ENT_QUOTES, $charset);
|
||||
}
|
||||
|
||||
public static function filesUri($files, $farExpires = true, $debug = false, $charset = 'UTF-8')
|
||||
{
|
||||
$h = new self;
|
||||
$h->setFiles($files, $farExpires);
|
||||
$uri = $h->getRawUri($farExpires, $debug);
|
||||
return htmlspecialchars($uri, ENT_QUOTES, $charset);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get URI (not html-escaped) to minify a group/set of files
|
||||
*/
|
||||
public function getRawUri($farExpires = true, $debug = false)
|
||||
{
|
||||
$path = rtrim($this->minAppUri, '/') . '/';
|
||||
if (! $this->rewriteWorks) {
|
||||
$path .= '?';
|
||||
}
|
||||
if (null === $this->_groupKey) {
|
||||
// @todo: implement shortest uri
|
||||
$path .= "f=" . $this->_fileList;
|
||||
} else {
|
||||
$path .= "g=" . $this->_groupKey;
|
||||
}
|
||||
if ($debug) {
|
||||
$path .= "&debug";
|
||||
} elseif ($farExpires && $this->_lastModified) {
|
||||
$path .= "&" . $this->_lastModified;
|
||||
}
|
||||
return $path;
|
||||
}
|
||||
|
||||
public function setFiles($files, $checkLastModified = true)
|
||||
{
|
||||
$this->_groupKey = null;
|
||||
if ($checkLastModified) {
|
||||
$this->_sniffLastModified($files);
|
||||
}
|
||||
// normalize paths like in /min/f=<paths>
|
||||
foreach ($files as $k => $file) {
|
||||
if (0 === strpos($file, '//')) {
|
||||
$file = substr($file, 2);
|
||||
} elseif (0 === strpos($file, '/')
|
||||
|| 1 === strpos($file, ':\\')) {
|
||||
$file = substr($file, strlen($_SERVER['DOCUMENT_ROOT']) + 1);
|
||||
}
|
||||
$file = strtr($file, '\\', '/');
|
||||
$files[$k] = $file;
|
||||
}
|
||||
$this->_fileList = implode(',', $files);
|
||||
}
|
||||
|
||||
public function setGroup($key, $checkLastModified = true)
|
||||
{
|
||||
$this->_groupKey = $key;
|
||||
if ($checkLastModified) {
|
||||
$gcFile = (null === $this->_groupsConfigFile)
|
||||
? dirname(dirname(dirname(dirname(__FILE__)))) . '/groupsConfig.php'
|
||||
: $this->_groupsConfigFile;
|
||||
if (is_file($gcFile)) {
|
||||
$gc = (require $gcFile);
|
||||
if (isset($gc[$key])) {
|
||||
$this->_sniffLastModified($gc[$key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function setPathToMin($path)
|
||||
{
|
||||
if (0 === strpos($path, '.')) {
|
||||
// relative path
|
||||
$path = dirname(__FILE__) . "/" . $path;
|
||||
}
|
||||
$file = realpath(rtrim($path, '/\\') . '/groupsConfig.php');
|
||||
if (! $file) {
|
||||
return false;
|
||||
}
|
||||
$this->_groupsConfigFile = $file;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
protected $_groupKey = null; // if present, URI will be like g=...
|
||||
protected $_fileList = '';
|
||||
protected $_groupsConfigArray = array();
|
||||
protected $_groupsConfigFile = null;
|
||||
protected $_lastModified = null;
|
||||
|
||||
protected function _sniffLastModified($sources)
|
||||
{
|
||||
$max = 0;
|
||||
foreach ((array)$sources as $source) {
|
||||
if ($source instanceof Minify_Source) {
|
||||
$max = max($max, $source->lastModified);
|
||||
} elseif (is_string($source)) {
|
||||
if (0 === strpos($source, '//')) {
|
||||
$source = $_SERVER['DOCUMENT_ROOT'] . substr($source, 1);
|
||||
}
|
||||
if (is_file($source)) {
|
||||
$max = max($max, filemtime($source));
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->_lastModified = $max;
|
||||
}
|
||||
|
||||
/**
|
||||
* In a given array of strings, find the character they all have at
|
||||
* a particular index
|
||||
* @param Array arr array of strings
|
||||
* @param Number pos index to check
|
||||
* @return mixed a common char or '' if any do not match
|
||||
*/
|
||||
protected static function _getCommonCharAtPos($arr, $pos) {
|
||||
$l = count($arr);
|
||||
$c = $arr[0][$pos];
|
||||
if ($c === '' || $l === 1)
|
||||
return $c;
|
||||
for ($i = 1; $i < l; ++$i)
|
||||
if ($arr[$i][$pos] !== $c)
|
||||
return '';
|
||||
return $c;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the shortest URI to minify the set of source files
|
||||
* @param Array sources URIs
|
||||
*//*
|
||||
,getBestUri : function (sources) {
|
||||
var pos = 0
|
||||
,base = ''
|
||||
,c;
|
||||
while (true) {
|
||||
c = MUB.getCommonCharAtPos(sources, pos);
|
||||
if (c === '')
|
||||
break;
|
||||
else
|
||||
base += c;
|
||||
++pos;
|
||||
}
|
||||
base = base.replace(/[^\/]+$/, '');
|
||||
var uri = MUB._minRoot + 'f=' + sources.join(',');
|
||||
if (base.charAt(base.length - 1) === '/') {
|
||||
// we have a base dir!
|
||||
var basedSources = sources
|
||||
,i
|
||||
,l = sources.length;
|
||||
for (i = 0; i < l; ++i) {
|
||||
basedSources[i] = sources[i].substr(base.length);
|
||||
}
|
||||
base = base.substr(0, base.length - 1);
|
||||
var bUri = MUB._minRoot + 'b=' + base + '&f=' + basedSources.join(',');
|
||||
//window.console && console.log([uri, bUri]);
|
||||
uri = uri.length < bUri.length
|
||||
? uri
|
||||
: bUri;
|
||||
}
|
||||
return uri;
|
||||
}//*/
|
||||
}
|
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
/**
|
||||
* Utility functions for generating group URIs in HTML files
|
||||
* Utility functions for generating URIs in HTML files
|
||||
*
|
||||
* Before including this file, /min/lib must be in your include_path.
|
||||
*
|
||||
@@ -22,19 +22,19 @@ require_once 'Minify/Build.php';
|
||||
* before using this function.
|
||||
*
|
||||
* @param string $group a key from groupsConfig.php
|
||||
* @param boolean $forceAmpersand (default false) Set to true if the RewriteRule
|
||||
* @param boolean $modRewriteWorking (default false) Set to true if the RewriteRule
|
||||
* directives in .htaccess are functional. This will remove the "?" from URIs, making them
|
||||
* more cacheable by proxies.
|
||||
* @return string
|
||||
*/
|
||||
function Minify_groupUri($group, $forceAmpersand = false)
|
||||
function Minify_groupUri($group, $modRewriteWorking = false)
|
||||
{
|
||||
$path = $forceAmpersand
|
||||
$path = $modRewriteWorking
|
||||
? "/g={$group}"
|
||||
: "/?g={$group}";
|
||||
return _Minify_getBuild($group)->uri(
|
||||
'/' . basename(dirname(__FILE__)) . $path
|
||||
,$forceAmpersand
|
||||
,$modRewriteWorking
|
||||
);
|
||||
}
|
||||
|
||||
|
48
min_unit_tests/test_Minify_HTML_Helper.php
Normal file
48
min_unit_tests/test_Minify_HTML_Helper.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
require_once '_inc.php';
|
||||
|
||||
require_once 'Minify/HTML/Helper.php';
|
||||
|
||||
function test_Minify_HTML_Helper()
|
||||
{
|
||||
global $thisDir;
|
||||
|
||||
$file1 = $thisDir . '/_test_files/css/paths_prepend.css';
|
||||
$file2 = $thisDir . '/_test_files/css/styles.css';
|
||||
$maxTime = max(filemtime($file1), filemtime($file2));
|
||||
|
||||
$path1 = '/' . dirname($_SERVER['SCRIPT_NAME']) . '/_test_files/css/paths_prepend.css';
|
||||
$path2 = '/' . dirname($_SERVER['SCRIPT_NAME']) . '/_test_files/css/styles.css';
|
||||
|
||||
echo Minify_HTML_Helper::filesUri(array($path1, $path2)) . "\n";
|
||||
echo Minify_HTML_Helper::filesUri(array($file1, $file2)) . "\n";
|
||||
echo Minify_HTML_Helper::groupUri('notRealGroup') . "\n";
|
||||
|
||||
|
||||
//echo Minify_HTML_Helper::filesUri(array($file1, $file2));
|
||||
|
||||
/*
|
||||
$b = new Minify_Build($file1);
|
||||
assertTrue($b->lastModified == filemtime($file1)
|
||||
,'Minify_Build : single file path');
|
||||
|
||||
$b = new Minify_Build(array($file1, $file2));
|
||||
assertTrue($maxTime == $b->lastModified
|
||||
,'Minify_Build : multiple file paths');
|
||||
|
||||
require_once 'Minify.php';
|
||||
$b = new Minify_Build(array(
|
||||
$file1
|
||||
,new Minify_Source(array('filepath' => $file2))
|
||||
));
|
||||
|
||||
assertTrue($maxTime == $b->lastModified
|
||||
,'Minify_Build : file path and a Minify_Source');
|
||||
assertTrue($b->uri('/path') == "/path?{$maxTime}"
|
||||
,'Minify_Build : uri() with no querystring');
|
||||
assertTrue($b->uri('/path?hello') == "/path?hello&{$maxTime}"
|
||||
,'Minify_Build : uri() with existing querystring');
|
||||
//*/
|
||||
}
|
||||
|
||||
test_Minify_HTML_Helper();
|
Reference in New Issue
Block a user