1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-23 22:22:59 +02:00

Reorganized examples

This commit is contained in:
Steve Clay
2008-05-15 15:03:46 +00:00
parent e9035109c9
commit 048c689663
11 changed files with 178 additions and 112 deletions

View File

@@ -0,0 +1,11 @@
<?php
$base = realpath(dirname(__FILE__) . '/..');
$groupsSources = array(
'js' => array(
"{$base}/jquery-1.2.3.js"
,"{$base}/test space.js"
)
,'css' => array("{$base}/test.css")
);
unset($base);

View File

@@ -1,19 +1,18 @@
<?php
require '../../config.php';
ob_start();
require '_groupsSources.php';
require 'Minify/Build.php';
$jsBuild = new Minify_Build($groupsSources['js']);
$cssBuild = new Minify_Build($groupsSources['css']);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Minify Example 1</title>
<link rel="stylesheet" type="text/css" href="m.php?f=test.css&amp;v=3" />
<style type="text/css">
#cssFail {
width:2.8em;
overflow:hidden;
}
</style>
<link rel="stylesheet" type="text/css" href="<?php echo $cssBuild->uri('m.php/css'); ?>" />
<style type="text/css">#cssFail {width:2.8em; overflow:hidden;}</style>
</head>
<body>
@@ -23,47 +22,26 @@ ob_start();
<code>config.php</code>. Notice that minifying jQuery takes several seconds!.</p>
<?php endif; ?>
<h1>Minify Example 1</h1>
<h1>Minify Example 1 : Groups controller + Far-off Expires header</h1>
<p>This is an example of Minify serving a directory of single css/js files.
Each file is minified and sent with HTTP encoding (browser-permitting).</p>
<p>In this example, we use a single config file <code>_groupsSources.php</code>
to specify files for minification. During HTML generation,
<code>Minify_Build</code> is used
to stamp the latest modification times onto the minify URLs. Our minify server,
<code>m.php</code>, then sends the content with far-off Expires headers.</p>
<p>In this example, if m.php detects $_GET['v'], a 30-day Expires header is
sent rather than the conditional GET.</p>
<p>If one of our sources is modified, its URL (particularly the query string) is
changed in the HTML document, causing the browser to request a new version.</p>
<h2>Minify tests</h2>
<ul>
<li id="cssFail"><span>FAIL</span>PASS</li>
<li id="jsFail1">FAIL</li>
<li id="jsFail2">FAIL</li>
</ul>
<p><a href="">Link to this page (F5 can trigger no-cache headers)</a></p>
<h2>Test client cache</h2>
<p><a href="">Reload page</a> <small>(F5 can trigger no-cache headers)</small></p>
<script type="text/javascript" src="m.php?f=jquery-1.2.3.js&amp;v=1"></script>
<script type="text/javascript" src="m.php?f=test+space.js"></script>
<script type="text/javascript">
$(function () {
if ( 1 < 2 ) {
$('#jsFail2').html('PASS');
}
});
</script>
<script type="text/javascript" src="<?php echo $jsBuild->uri('m.php/js'); ?>"></script>
</body>
</html>
<?php
$content = ob_get_clean();
require 'Minify.php';
if ($minifyCachePath) {
Minify::useServerCache($minifyCachePath);
}
Minify::serve('Page', array(
'content' => $content
,'id' => __FILE__
,'lastModifiedTime' => filemtime(__FILE__)
// also minify the CSS/JS inside the HTML
,'minifyAll' => true
));
</html>

View File

@@ -1,49 +1,14 @@
<?php
/**
* This script will serve a single js/css file in this directory. Here we place
* the front-end-controller logic in user code, then use the "Files" controller
* to minify the file. Alternately, we could have created a custom controller
* with the same logic and passed it to Minify::handleRequest().
*/
require '../../config.php';
require '_groupsSources.php';
require 'Minify.php';
/**
* The Files controller only "knows" HTML, CSS, and JS files. Other files
* would only be trim()ed and sent as plain/text.
*/
$serveExtensions = array('css', 'js');
// set HTTP Expires header if GET 'v' is sent
$setExpires = isset($_GET['v'])
? (time() + 86400 * 30)
: null;
// serve
if (isset($_GET['f'])) {
$filename = basename($_GET['f']); // remove any naughty bits
$filenamePattern = '/[^\'"\\/\\\\]+\\.(?:'
.implode('|', $serveExtensions). ')$/';
if (preg_match($filenamePattern, $filename)
&& file_exists(dirname(__FILE__) . '/' . $filename)) {
require 'Minify.php';
if ($minifyCachePath) {
Minify::useServerCache($minifyCachePath);
}
// The Files controller serves an array of files, but here we just
// need one.
Minify::serve('Files', array(
'files' => array(dirname(__FILE__) . '/' . $filename)
,'setExpires' => $setExpires
));
exit();
}
if ($minifyCachePath) {
Minify::useServerCache($minifyCachePath);
}
header("HTTP/1.0 404 Not Found");
echo "HTTP/1.0 404 Not Found";
Minify::serve('Groups', array(
'groups' => $groupsSources
,'setExpires' => time() + 86400 * 365
));

View File

@@ -1,19 +0,0 @@
/* Test file to minify */
/* Minify copyright notice here... */
h1 {
color: #00cc00;
font-size: 20px;
}
ul, li {
padding:0;
margin:0;
display:block;
font-family: monospace;
}
#cssFail span {
display: none;
}

View File

@@ -1,6 +1,6 @@
<?php
$base = realpath(dirname(__FILE__) . '/../1');
$base = realpath(dirname(__FILE__) . '/..');
$groupsSources = array(
'js' => array(
"{$base}/jquery-1.2.3.js"

View File

@@ -15,11 +15,12 @@ ob_start();
<title>Minify Example 2</title>
<link rel="stylesheet" type="text/css" href="<?php echo $cssBuild->uri('m.php/css'); ?>" />
<style type="text/css">
#cssFail {
width:2.8em;
width:2.8em;
overflow:hidden;
}
</style>
</style>
</head>
<body>
@@ -29,26 +30,35 @@ ob_start();
<code>config.php</code>. Notice that minifying jQuery takes several seconds!.</p>
<?php endif; ?>
<h1>Minify Example 2</h1>
<h1>Minify Example 2: Minifying <em>Everything</em></h1>
<p>This is an example using Minify_Build and the Groups controller to
automatically create versioned minify URLs</p>
<p>In this example, external Javascript and CSS minification is identical to
example 1, but here Minify is also used to minify and serve the HTML, including
the contents of all <code>&lt;style&gt;</code> and <code>&lt;script&gt;</code>
elements.</p>
<p>As the document is XHTML, Minify_HTML places the 2nd <code>&lt;script&gt;</code>
element in a CDATA section because it contains "&lt;". The output is valid XHTML.</p>
<h2>Minify tests</h2>
<ul>
<li id="cssFail"><span>FAIL</span>PASS</li>
<li id="jsFail1">FAIL</li>
<li id="jsFail2">FAIL</li>
</ul>
<p><a href="">Link to this page (F5 can trigger no-cache headers)</a></p>
<h2>Test client cache</h2>
<p><a href="">Reload page</a> <small>(F5 can trigger no-cache headers)</small></p>
<script type="text/javascript" src="<?php echo $jsBuild->uri('m.php/js'); ?>"></script>
<script type="text/javascript">
$(function () {
if ( 1 < 2 ) {
$('#jsFail2').html('PASS');
}
});
</script>
</body>
</html>

37
web/examples/3/index.php Normal file
View File

@@ -0,0 +1,37 @@
<?php
require '../../config.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Minify Example 3</title>
<link rel="stylesheet" type="text/css" href="m.php?f=test.css" />
<style type="text/css">#cssFail {width:2.8em; overflow:hidden;}</style>
</head>
<body>
<?php if (! $minifyCachePath): ?>
<p><strong>Note:</strong> You should <em>always</em> enable caching using
<code>Minify::useServerCache()</code>. For the examples this can be set in
<code>config.php</code>. Notice that minifying jQuery takes several seconds!.</p>
<?php endif; ?>
<h1>Minify Example 3: Files controller</h1>
<p>This is an example of Minify serving a directory of single css/js files.
Each file is minified and sent with HTTP encoding (browser-permitting).</p>
<h2>Minify tests</h2>
<ul>
<li id="cssFail"><span>FAIL</span>PASS</li>
<li id="jsFail1">FAIL</li>
</ul>
<h2>Test client cache</h2>
<p><a href="">Reload page</a> <small>(F5 can trigger no-cache headers)</small></p>
<script type="text/javascript" src="m.php?f=jquery-1.2.3.js"></script>
<script type="text/javascript" src="m.php?f=test+space.js"></script>
</body>
</html>

43
web/examples/3/m.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
/**
* This script will serve a single js/css file in this directory. Here we place
* the front-end-controller logic in user code, then use the "Files" controller
* to minify the file. Alternately, we could have created a custom controller
* with the same logic and passed it to Minify::handleRequest().
*/
require '../../config.php';
/**
* The Files controller only "knows" HTML, CSS, and JS files. Other files
* would only be trim()ed and sent as plain/text.
*/
$serveExtensions = array('css', 'js');
// serve
if (isset($_GET['f'])) {
$filename = basename($_GET['f']); // remove any naughty bits
$filenamePattern = '/[^\'"\\/\\\\]+\\.(?:'
.implode('|', $serveExtensions). ')$/';
if (preg_match($filenamePattern, $filename)
&& file_exists(dirname(__FILE__) . '/../' . $filename)) {
require 'Minify.php';
if ($minifyCachePath) {
Minify::useServerCache($minifyCachePath);
}
// The Files controller can serve an array of files, but here we just
// need one.
Minify::serve('Files', array(
'files' => array(dirname(__FILE__) . '/../' . $filename)
));
exit();
}
}
header("HTTP/1.0 404 Not Found");
echo "HTTP/1.0 404 Not Found";

View File

@@ -1,5 +1,5 @@
(function(){
/*
/*!
* jQuery 1.2.3 - New Wave Javascript
*
* Copyright (c) 2008 John Resig (jquery.com)

41
web/examples/test.css Normal file
View File

@@ -0,0 +1,41 @@
/* Test file to minify */
/*! copyright notice here... */
html {
margin:0;
padding:0;
background:#aaa;
}
body {
margin:0 auto;
padding:1em;
width:550px;
background:#fff;
}
body > *:first-child {margin-top:0;}
h1 {
color: #00cc00;
font-size: 20px;
}
h2 {
color: #009900;
font-size: 17px;
}
h1, h2 {margin:1.3em 0 .3em;}
p {
margin:0 0 1em;
}
ul, li {
padding:0;
margin:0;
display:block;
font-family: monospace;
}
#cssFail span {
display: none;
}