Put file paths in a central config file
This commit is contained in:
parent
1e61405909
commit
004944fdd8
@ -4,7 +4,7 @@ A simple set of minifying scripts for CSS and Javascript
|
||||
|
||||
## Basic Use: ##
|
||||
|
||||
1. Figure out your file paths, and set them in css.php and js.php.
|
||||
1. Figure out your file paths, and set them in `config/config.php`.
|
||||
2. Add your css and javascript files to groups, in `config/css_groups.php` and `config/js_groups.php` respectively
|
||||
3. Point your CSS links in your HTML to `css.php/g/[group_name]`, and likewise your javascript to `js.php/g/[group_name]`
|
||||
4. Add a folder named "cache" to your js path
|
||||
|
36
config/config.php
Normal file
36
config/config.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Document Root
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The folder where the index of the website exists. In most situations,
|
||||
| this will not need to be changed.
|
||||
|
|
||||
| If the website is in a folder off of the domain name, like:
|
||||
| http://example.com/website/
|
||||
| you will need to add that folder to the document root.
|
||||
|
|
||||
*/
|
||||
$document_root = $_SERVER['DOCUMENT_ROOT'];
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| CSS Folder
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The folder where css files exist, in relation to the document root
|
||||
|
|
||||
*/
|
||||
$css_root = $document_root. '/css/';
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| JS Folder
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
||||
| The folder where javascript files exist, in relation to the document root
|
||||
|
|
||||
*/
|
||||
$js_root = $document_root. '/js/';
|
26
css.php
26
css.php
@ -1,10 +1,13 @@
|
||||
<?php
|
||||
//Get config files
|
||||
require('./config/config.php');
|
||||
|
||||
//Include the css groups
|
||||
$groups = require("./config/css_groups.php");
|
||||
|
||||
//The name of this file
|
||||
$this_file = $css_root.'css.php';
|
||||
|
||||
//Change as needed
|
||||
$base_path = $_SERVER['DOCUMENT_ROOT'];
|
||||
//This GZIPs the CSS for transmission to the user
|
||||
//making file size smaller and transfer rate quicker
|
||||
ob_start("ob_gzhandler");
|
||||
|
||||
//Function for compressing the CSS as tightly as possible
|
||||
function compress($buffer) {
|
||||
@ -33,9 +36,6 @@ while($i < $pia_len)
|
||||
$i = $j + 1;
|
||||
};
|
||||
|
||||
//Include the css groups
|
||||
$groups = require("./config/css_groups.php");
|
||||
|
||||
$css = '';
|
||||
$modified = array();
|
||||
|
||||
@ -43,14 +43,14 @@ if(isset($groups[$_GET['g']]))
|
||||
{
|
||||
foreach($groups[$_GET['g']] as $file)
|
||||
{
|
||||
$new_file = realpath($base_path.$file);
|
||||
$new_file = realpath($css_root.$file);
|
||||
$css .= file_get_contents($new_file);
|
||||
$modified[] = filemtime($new_file);
|
||||
}
|
||||
}
|
||||
|
||||
//Add this page too
|
||||
$modified[] = filemtime($base_path."css.php");
|
||||
$modified[] = filemtime($this_file);
|
||||
|
||||
//Get the latest modified date
|
||||
rsort($modified);
|
||||
@ -71,10 +71,14 @@ if($last_modified === $requested_time)
|
||||
exit();
|
||||
}
|
||||
|
||||
//This GZIPs the CSS for transmission to the user
|
||||
//making file size smaller and transfer rate quicker
|
||||
ob_start("ob_gzhandler");
|
||||
|
||||
header("Content-Type: text/css; charset=utf8");
|
||||
header("Cache-control: public, max-age=691200, must-revalidate");
|
||||
header("Last-Modified: ".gmdate('D, d M Y H:i:s', $last_modified)." GMT");
|
||||
header("Expires: ".gmdate('D, d M Y H:i:s', (filemtime($base_path.'css.php') + 691200))." GMT");
|
||||
header("Expires: ".gmdate('D, d M Y H:i:s', (filemtime($this_file) + 691200))." GMT");
|
||||
|
||||
echo $css;
|
||||
|
||||
|
103
js.php
103
js.php
@ -1,14 +1,41 @@
|
||||
<?php
|
||||
//Change as needed
|
||||
$base_path = $_SERVER['DOCUMENT_ROOT'];
|
||||
|
||||
//Get config files
|
||||
require('./config/config.php');
|
||||
require('./config/jshrink.php');
|
||||
|
||||
//This GZIPs the js for transmission to the user
|
||||
//making file size smaller and transfer rate quicker
|
||||
ob_start("ob_gzhandler");
|
||||
//Include the js groups
|
||||
$groups = require("./config/js_groups.php");
|
||||
|
||||
//Creative rewriting
|
||||
//The name of this file
|
||||
$this_file = $js_root.'js.php';
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Get Files
|
||||
*
|
||||
* Concatonates the javascript files for the current
|
||||
* group as a string
|
||||
* @return string
|
||||
*/
|
||||
function get_files()
|
||||
{
|
||||
global $groups, $js_root;
|
||||
|
||||
$js = '';
|
||||
|
||||
foreach($groups[$_GET['g']] as $file)
|
||||
{
|
||||
$new_file = realpath($js_root.$file);
|
||||
$js .= file_get_contents($new_file);
|
||||
}
|
||||
|
||||
return $js;
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
//Creative rewriting of /g/groupname to ?g=groupname
|
||||
$pi = $_SERVER['PATH_INFO'];
|
||||
$pia = explode('/', $pi);
|
||||
|
||||
@ -25,38 +52,39 @@ while($i < $pia_len)
|
||||
$i = $j + 1;
|
||||
};
|
||||
|
||||
//Include the js groups
|
||||
$groups = require("./config/js_groups.php");
|
||||
|
||||
$js = '';
|
||||
$modified = array();
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
//Aggregate the last modified times of the files
|
||||
if(isset($groups[$_GET['g']]))
|
||||
{
|
||||
$cache_file = $base_path.'/cache/'.$_GET['g'];
|
||||
$cache_file = $js_root.'/cache/'.$_GET['g'];
|
||||
|
||||
foreach($groups[$_GET['g']] as $file)
|
||||
{
|
||||
$new_file = realpath($base_path.$file);
|
||||
$js .= file_get_contents($new_file);
|
||||
$new_file = realpath($js_root.$file);
|
||||
$modified[] = filemtime($new_file);
|
||||
}
|
||||
|
||||
//Add this page too
|
||||
$modified[] = filemtime($this_file);
|
||||
|
||||
$cache_modified = 0;
|
||||
|
||||
//Add the cache file
|
||||
if(is_file($cache_file))
|
||||
{
|
||||
$cache_modified = filemtime($cache_file);
|
||||
}
|
||||
}
|
||||
else //Nothing to display? Just exit
|
||||
{
|
||||
die("You must specify a group that exists");
|
||||
}
|
||||
|
||||
//Add this page too
|
||||
$modified[] = filemtime($base_path."js.php");
|
||||
|
||||
$cache_modified = 0;
|
||||
|
||||
//Add the cache file
|
||||
if(is_file($cache_file))
|
||||
{
|
||||
$cache_modified = filemtime($cache_file);
|
||||
}
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
//Get the latest modified date
|
||||
rsort($modified);
|
||||
@ -66,26 +94,47 @@ $requested_time=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
|
||||
? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])
|
||||
: time();
|
||||
|
||||
// If the browser's cached version is up to date,
|
||||
// don't resend the file
|
||||
if($last_modified === $requested_time)
|
||||
{
|
||||
header("HTTP/1.1 304 Not Modified");
|
||||
exit();
|
||||
}
|
||||
|
||||
if(!isset($_GET['debug']) && ($cache_modified < $last_modified))
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
//Determine what to do: rebuild cache, send files as is, or send cache.
|
||||
if($cache_modified < $last_modified)
|
||||
{
|
||||
$js = trim(JShrink::minify($js, array('flaggedComments' => false)));
|
||||
file_put_contents($cache_file, $js);
|
||||
$js = trim(JShrink::minify(get_files(), array('flaggedComments' => false)));
|
||||
$cs = file_put_contents($cache_file, $js);
|
||||
|
||||
//Make sure cache file gets created/updated
|
||||
if($cs === FALSE)
|
||||
{
|
||||
die("Cache file was not created. Make sure you have the correct folder permissions.");
|
||||
}
|
||||
}
|
||||
else if(isset($_GET['debug']))
|
||||
{
|
||||
$js = get_files();
|
||||
}
|
||||
else
|
||||
{
|
||||
$js = file_get_contents($cache_file);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
//This GZIPs the js for transmission to the user
|
||||
//making file size smaller and transfer rate quicker
|
||||
ob_start("ob_gzhandler");
|
||||
|
||||
header("Content-Type: application/x-javascript; charset=utf8");
|
||||
header("Cache-control: public, max-age=691200, must-revalidate");
|
||||
header("Last-Modified: ".gmdate('D, d M Y H:i:s', $last_modified)." GMT");
|
||||
header("Expires: ".gmdate('D, d M Y H:i:s', (filemtime($base_path.'js.php') + 691200))." GMT");
|
||||
header("Expires: ".gmdate('D, d M Y H:i:s', (filemtime($this_file) + 691200))." GMT");
|
||||
|
||||
echo $js;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user