Convert to a more modern class-based setup
This commit is contained in:
parent
d056f7fac5
commit
51a118edf6
@ -5,64 +5,87 @@
|
|||||||
* Simple minification for better website performance
|
* Simple minification for better website performance
|
||||||
*
|
*
|
||||||
* @author Timothy J. Warren
|
* @author Timothy J. Warren
|
||||||
* @copyright Copyright (c) 2012
|
* @copyright Copyright (c) 2012-2016
|
||||||
* @link https://github.com/aviat4ion/Easy-Min
|
* @link https://git.timshomepage.net/aviat4ion/Easy-Min
|
||||||
* @license http://philsturgeon.co.uk/code/dbad-license
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
/*
|
return [
|
||||||
|--------------------------------------------------------------------------
|
/*
|
||||||
| Document Root
|
|--------------------------------------------------------------------------
|
||||||
|--------------------------------------------------------------------------
|
| Document Root
|
||||||
|
|
|--------------------------------------------------------------------------
|
||||||
| The folder where the index of the website exists. In most situations,
|
|
|
||||||
| this will not need to be changed.
|
| 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/
|
| If the website is in a folder off of the domain name, like:
|
||||||
| you will need to add that folder to the document root.
|
| http://example.com/website/
|
||||||
|
|
| you will need to add that folder to the document root.
|
||||||
*/
|
|
|
||||||
$document_root = $_SERVER['DOCUMENT_ROOT'];
|
*/
|
||||||
|
'document_root' => $_SERVER['DOCUMENT_ROOT'],
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| CSS Folder
|
| CSS Folder
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
|
||||||
| The folder where css files exist, in relation to the document root
|
| The folder where css files exist, in relation to the document root
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
$css_root = $document_root. '/css/';
|
'css_root' => $document_root. '/css/',
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Path from
|
| Path from
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
|
||||||
| Path fragment to rewrite in css files
|
| Path fragment to rewrite in css files
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
$path_from = '';
|
'path_from' => '',
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| Path to
|
| Path to
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
|
||||||
| The path fragment replacement for the css files
|
| The path fragment replacement for the css files
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
$path_to = '';
|
'path_to' => '',
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
| JS Folder
|
| CSS Groups file
|
||||||
|--------------------------------------------------------------------------
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
|
||||||
| The folder where javascript files exist, in relation to the document root
|
| The file where the css groups are configured
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
$js_root = $document_root. '/js/';
|
'css_groups_file' => realpath(__DIR__ . '/css_groups.php'),
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| JS Folder
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| The folder where javascript files exist, in relation to the document root
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
'js_root' => $document_root. '/js/',
|
||||||
|
|
||||||
|
/*
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
| JS Groups file
|
||||||
|
|--------------------------------------------------------------------------
|
||||||
|
|
|
||||||
|
| The file where the javascript groups are configured
|
||||||
|
|
|
||||||
|
*/
|
||||||
|
'js_groups_file' => realpath(__DIR__ . '/js_groups.php'),
|
||||||
|
|
||||||
|
];
|
@ -15,7 +15,7 @@
|
|||||||
/**
|
/**
|
||||||
* This is the config array for css files to concatenate and minify
|
* This is the config array for css files to concatenate and minify
|
||||||
*/
|
*/
|
||||||
return array(
|
return [
|
||||||
/*-----
|
/*-----
|
||||||
Css
|
Css
|
||||||
-----*/
|
-----*/
|
||||||
@ -23,10 +23,10 @@ return array(
|
|||||||
/*
|
/*
|
||||||
For each group create an array like so
|
For each group create an array like so
|
||||||
|
|
||||||
'my_group' => array(
|
'my_group' => [
|
||||||
'path/to/css/file1.css',
|
'path/to/css/file1.css',
|
||||||
'path/to/css/file2.css'
|
'path/to/css/file2.css'
|
||||||
),
|
],
|
||||||
*/
|
*/
|
||||||
);
|
];
|
||||||
// End of css_groups.php
|
// End of css_groups.php
|
@ -15,15 +15,15 @@
|
|||||||
/**
|
/**
|
||||||
* This is the config array for javascript files to concatenate and minify
|
* This is the config array for javascript files to concatenate and minify
|
||||||
*/
|
*/
|
||||||
return array(
|
return [
|
||||||
/*
|
/*
|
||||||
For each group create an array like so
|
For each group create an array like so
|
||||||
|
|
||||||
'my_group' => array(
|
'my_group' => [
|
||||||
'path/to/js/file1.js',
|
'path/to/js/file1.js',
|
||||||
'path/to/js/file2.js'
|
'path/to/js/file2.js'
|
||||||
),
|
],
|
||||||
*/
|
*/
|
||||||
);
|
];
|
||||||
|
|
||||||
// End of js_groups.php
|
// End of js_groups.php
|
249
css.php
249
css.php
@ -5,124 +5,173 @@
|
|||||||
* Simple minification for better website performance
|
* Simple minification for better website performance
|
||||||
*
|
*
|
||||||
* @author Timothy J. Warren
|
* @author Timothy J. Warren
|
||||||
* @copyright Copyright (c) 2012
|
* @copyright Copyright (c) 2012-2016
|
||||||
* @link https://github.com/aviat4ion/Easy-Min
|
* @link https://git.timshomepage.net/aviat4ion/Easy-Min
|
||||||
* @license http://philsturgeon.co.uk/code/dbad-license
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
|
namespace Aviat\EasyMin;
|
||||||
|
|
||||||
//Get config files
|
require_once('./min.php');
|
||||||
require('./config/config.php');
|
|
||||||
|
|
||||||
//Include the css groups
|
/**
|
||||||
$groups = require("./config/css_groups.php");
|
* Simple CSS Minifier
|
||||||
|
*/
|
||||||
|
class CSSMin extends BaseMin {
|
||||||
|
|
||||||
//The name of this file
|
protected $css_root;
|
||||||
$this_file = 'css.php';
|
protected $path_from;
|
||||||
|
protected $path_to;
|
||||||
|
protected $group;
|
||||||
|
protected $last_modified;
|
||||||
|
protected $requested_time;
|
||||||
|
|
||||||
|
public function __construct(array $config, array $groups)
|
||||||
//Function for compressing the CSS as tightly as possible
|
|
||||||
function compress($buffer) {
|
|
||||||
|
|
||||||
//Remove CSS comments
|
|
||||||
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
|
|
||||||
|
|
||||||
//Remove tabs, spaces, newlines, etc.
|
|
||||||
$buffer = preg_replace('`\s+`', ' ', $buffer);
|
|
||||||
$replace = array(
|
|
||||||
' )' => ')',
|
|
||||||
') ' => ')',
|
|
||||||
' }' => '}',
|
|
||||||
'} ' => '}',
|
|
||||||
' {' => '{',
|
|
||||||
'{ ' => '{',
|
|
||||||
', ' => ',',
|
|
||||||
': ' => ':',
|
|
||||||
'; ' => ';',
|
|
||||||
);
|
|
||||||
|
|
||||||
//Eradicate every last space!
|
|
||||||
$buffer = trim(strtr($buffer, $replace));
|
|
||||||
$buffer = str_replace('{ ', '{', $buffer);
|
|
||||||
$buffer = str_replace('} ', '}', $buffer);
|
|
||||||
|
|
||||||
return $buffer;
|
|
||||||
}
|
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
|
||||||
|
|
||||||
//Creative rewriting
|
|
||||||
$pi = $_SERVER['PATH_INFO'];
|
|
||||||
$pia = explode('/', $pi);
|
|
||||||
|
|
||||||
$pia_len = count($pia);
|
|
||||||
$i = 1;
|
|
||||||
|
|
||||||
while($i < $pia_len)
|
|
||||||
{
|
|
||||||
$j = $i+1;
|
|
||||||
$j = (isset($pia[$j])) ? $j : $i;
|
|
||||||
|
|
||||||
$_GET[$pia[$i]] = $pia[$j];
|
|
||||||
|
|
||||||
$i = $j + 1;
|
|
||||||
};
|
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
|
||||||
|
|
||||||
$css = '';
|
|
||||||
$modified = array();
|
|
||||||
|
|
||||||
// Get all the css files, and concatenate them together
|
|
||||||
if(isset($groups[$_GET['g']]))
|
|
||||||
{
|
|
||||||
foreach($groups[$_GET['g']] as $file)
|
|
||||||
{
|
{
|
||||||
$new_file = realpath($css_root.$file);
|
$group = $_GET['g'];
|
||||||
$css .= file_get_contents($new_file);
|
$this->css_root = $config['css_root'];
|
||||||
$modified[] = filemtime($new_file);
|
$this->path_from = $config['path_from'];
|
||||||
|
$this->path_to = $config['path_to'];
|
||||||
|
$this->group = $groups[$group];
|
||||||
|
$this->last_modified = $this->get_last_modified();
|
||||||
|
|
||||||
|
$this->send();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send the CSS
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function send()
|
||||||
|
{
|
||||||
|
if($this->last_modified >= $this->get_if_modified() && $this->is_not_debug())
|
||||||
|
{
|
||||||
|
throw new FileNotChangedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
$css = ( ! array_key_exists('debug', $_GET))
|
||||||
|
? $this->compress($this->get_css())
|
||||||
|
: $this->get_css();
|
||||||
|
|
||||||
|
$this->output($css);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function for compressing the CSS as tightly as possible
|
||||||
|
*
|
||||||
|
* @param string $buffer
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function compress($buffer)
|
||||||
|
{
|
||||||
|
//Remove CSS comments
|
||||||
|
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
|
||||||
|
|
||||||
|
//Remove tabs, spaces, newlines, etc.
|
||||||
|
$buffer = preg_replace('`\s+`', ' ', $buffer);
|
||||||
|
$replace = array(
|
||||||
|
' )' => ')',
|
||||||
|
') ' => ')',
|
||||||
|
' }' => '}',
|
||||||
|
'} ' => '}',
|
||||||
|
' {' => '{',
|
||||||
|
'{ ' => '{',
|
||||||
|
', ' => ',',
|
||||||
|
': ' => ':',
|
||||||
|
'; ' => ';',
|
||||||
|
);
|
||||||
|
|
||||||
|
//Eradicate every last space!
|
||||||
|
$buffer = trim(strtr($buffer, $replace));
|
||||||
|
$buffer = str_replace('{ ', '{', $buffer);
|
||||||
|
$buffer = str_replace('} ', '}', $buffer);
|
||||||
|
|
||||||
|
return $buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the most recent file modification date
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
protected function get_last_modified()
|
||||||
|
{
|
||||||
|
$modified = array();
|
||||||
|
|
||||||
|
// Get all the css files, and concatenate them together
|
||||||
|
if(isset($this->group))
|
||||||
|
{
|
||||||
|
foreach($this->group as $file)
|
||||||
|
{
|
||||||
|
$new_file = realpath("{$this->css_root}{$file}");
|
||||||
|
$modified[] = filemtime($new_file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Add this page for last modified check
|
||||||
|
$modified[] = filemtime(__FILE__);
|
||||||
|
|
||||||
|
//Get the latest modified date
|
||||||
|
rsort($modified);
|
||||||
|
|
||||||
|
return array_shift($modified);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the css to display
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function get_css()
|
||||||
|
{
|
||||||
|
$css = '';
|
||||||
|
|
||||||
|
foreach($this->group as $file)
|
||||||
|
{
|
||||||
|
$new_file = realpath("{$this->css_root}{$file}");
|
||||||
|
$css .= file_get_contents($new_file);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Correct paths that have changed due to concatenation
|
||||||
|
// based on rules in the config file
|
||||||
|
$css = str_replace($this->path_from, $this->path_to, $css);
|
||||||
|
|
||||||
|
return $css;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Output the CSS
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function output($css)
|
||||||
|
{
|
||||||
|
$this->send_final_output($css, 'text/css', $this->last_modified);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//Add this page for last modified check
|
// --------------------------------------------------------------------------
|
||||||
$modified[] = filemtime($this_file);
|
// ! Start Minifying
|
||||||
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
//Get the latest modified date
|
//Get config files
|
||||||
rsort($modified);
|
$config = require('config/config.php');
|
||||||
$last_modified = $modified[0];
|
$groups = require($config['css_groups_file']);
|
||||||
|
|
||||||
// If not in debug mode, minify the css
|
if ( ! array_key_exists($_GET['g'], $groups))
|
||||||
if( ! isset($_GET['debug']))
|
|
||||||
{
|
{
|
||||||
$css = compress($css);
|
throw new InvalidArgumentException('You must specify a css group that exists');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Correct paths that have changed due to concatenation
|
try
|
||||||
// based on rules in the config file
|
|
||||||
$css = strtr($css, $path_from, $path_to);
|
|
||||||
|
|
||||||
$requested_time=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
|
|
||||||
? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])
|
|
||||||
: time();
|
|
||||||
|
|
||||||
// Send 304 when not modified for faster response
|
|
||||||
if($last_modified === $requested_time)
|
|
||||||
{
|
{
|
||||||
header("HTTP/1.1 304 Not Modified");
|
new CSSMin($config, $groups);
|
||||||
exit();
|
}
|
||||||
|
catch (FileNotChangedException $e)
|
||||||
|
{
|
||||||
|
BaseMin::send304();
|
||||||
}
|
}
|
||||||
|
|
||||||
//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($this_file) + 691200))." GMT");
|
|
||||||
|
|
||||||
echo $css;
|
|
||||||
|
|
||||||
ob_end_flush();
|
|
||||||
//End of css.php
|
//End of css.php
|
335
js.php
335
js.php
@ -5,178 +5,243 @@
|
|||||||
* Simple minification for better website performance
|
* Simple minification for better website performance
|
||||||
*
|
*
|
||||||
* @author Timothy J. Warren
|
* @author Timothy J. Warren
|
||||||
* @copyright Copyright (c) 2012
|
* @copyright Copyright (c) 2012-2016
|
||||||
* @link https://github.com/aviat4ion/Easy-Min
|
* @link https://git.timshomepage.net/aviat4ion/Easy-Min
|
||||||
* @license http://philsturgeon.co.uk/code/dbad-license
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
//Get config files
|
namespace Aviat\EasyMin;
|
||||||
require('./config/config.php');
|
|
||||||
|
|
||||||
//Include the js groups
|
use GuzzleHttp\Client;
|
||||||
$groups_file = "./config/js_groups.php";
|
use GuzzleHttp\Psr7\Request;
|
||||||
$groups = require($groups_file);
|
|
||||||
|
|
||||||
//The name of this file
|
// Include guzzle
|
||||||
$this_file = 'js.php';
|
require_once('../vendor/autoload.php');
|
||||||
|
require_once('./min.php');
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
/**
|
||||||
|
* Simple Javascript minfier, using google closure compiler
|
||||||
|
*/
|
||||||
|
class JSMin extends BaseMin {
|
||||||
|
|
||||||
/**
|
protected $js_root;
|
||||||
* Get Files
|
protected $js_group;
|
||||||
*
|
protected $js_groups_file;
|
||||||
* Concatenates the javascript files for the current
|
protected $cache_file;
|
||||||
* group as a string
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
function get_files()
|
|
||||||
{
|
|
||||||
global $groups, $js_root;
|
|
||||||
|
|
||||||
$js = '';
|
protected $last_modified;
|
||||||
|
protected $requested_time;
|
||||||
|
protected $cache_modified;
|
||||||
|
|
||||||
foreach($groups[$_GET['g']] as $file)
|
public function __construct(array $config, array $groups)
|
||||||
{
|
{
|
||||||
$new_file = realpath($js_root.$file);
|
$group = $_GET['g'];
|
||||||
$js .= file_get_contents($new_file);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $js;
|
$this->js_root = $config['js_root'];
|
||||||
}
|
$this->js_group = $groups[$group];
|
||||||
|
$this->js_groups_file = $config['js_groups_file'];
|
||||||
|
$this->cache_file = "{$this->js_root}cache/{$group}";
|
||||||
|
$this->last_modified = $this->get_last_modified();
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
$this->cache_modified = (is_file($this->cache_file))
|
||||||
|
? filemtime($this->cache_file)
|
||||||
|
: 0;
|
||||||
|
|
||||||
/**
|
// Output some JS!
|
||||||
* Google Min
|
$this->send();
|
||||||
*
|
}
|
||||||
* Minifies javascript using google's closure compiler
|
|
||||||
* @param string $new_file
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
function google_min($new_file)
|
|
||||||
{
|
|
||||||
//Get a much-minified version from Google's closure compiler
|
|
||||||
$ch = curl_init('http://closure-compiler.appspot.com/compile');
|
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
||||||
curl_setopt($ch, CURLOPT_POST, 1);
|
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, 'output_info=compiled_code&output_format=text&compilation_level=SIMPLE_OPTIMIZATIONS&js_code=' . urlencode($new_file));
|
|
||||||
$output = curl_exec($ch);
|
|
||||||
curl_close($ch);
|
|
||||||
return $output;
|
|
||||||
}
|
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
protected function send()
|
||||||
|
{
|
||||||
|
// Override caching if debug key is set
|
||||||
|
if($this->is_debug_call())
|
||||||
|
{
|
||||||
|
return $this->output($this->get_files());
|
||||||
|
}
|
||||||
|
|
||||||
//Creative rewriting of /g/groupname to ?g=groupname
|
// If the browser's cached version is up to date,
|
||||||
$pi = $_SERVER['PATH_INFO'];
|
// don't resend the file
|
||||||
$pia = explode('/', $pi);
|
if($this->last_modified == $this->get_if_modified() && $this->is_not_debug())
|
||||||
|
{
|
||||||
|
throw new FileNotChangedException();
|
||||||
|
}
|
||||||
|
|
||||||
$pia_len = count($pia);
|
if($this->cache_modified < $this->last_modified)
|
||||||
$i = 1;
|
{
|
||||||
|
$js = $this->minify($this->get_files());
|
||||||
|
|
||||||
while($i < $pia_len)
|
//Make sure cache file gets created/updated
|
||||||
{
|
if (file_put_contents($this->cache_file, $js) === FALSE)
|
||||||
$j = $i+1;
|
{
|
||||||
$j = (isset($pia[$j])) ? $j : $i;
|
echo 'Cache file was not created. Make sure you have the correct folder permissions.';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$_GET[$pia[$i]] = $pia[$j];
|
return $this->output($js);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return $this->output(file_get_contents($this->cache_file));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$i = $j + 1;
|
/**
|
||||||
};
|
* Makes a call to google closure compiler service
|
||||||
|
*
|
||||||
|
* @param array $options - Form parameters
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
protected function closure_call(array $options)
|
||||||
|
{
|
||||||
|
$client = new Client();
|
||||||
|
$response = $client->post('http://closure-compiler.appspot.com/compile', [
|
||||||
|
'headers' => [
|
||||||
|
'Accept-Encoding' => 'gzip',
|
||||||
|
'Content-type' => 'application/x-www-form-urlencoded'
|
||||||
|
],
|
||||||
|
'form_params' => $options
|
||||||
|
]);
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
return $response;
|
||||||
|
}
|
||||||
|
|
||||||
$js = '';
|
/**
|
||||||
$modified = array();
|
* Do a call to the closure compiler to check for compilation errors
|
||||||
|
*
|
||||||
|
* @param array $options
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function check_minify_errors($options)
|
||||||
|
{
|
||||||
|
$error_res = $this->closure_call($options);
|
||||||
|
$error_json = $error_res->getBody();
|
||||||
|
$error_obj = json_decode($error_json) ?: (object)[];
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// Show error if exists
|
||||||
|
if ( ! empty($error_obj->errors) || ! empty($error_obj->serverErrors))
|
||||||
|
{
|
||||||
|
$error_json = json_encode($error_obj, JSON_PRETTY_PRINT);
|
||||||
|
header('Content-type: application/javascript');
|
||||||
|
echo "console.error(${error_json});";
|
||||||
|
die();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//Aggregate the last modified times of the files
|
/**
|
||||||
if(isset($groups[$_GET['g']]))
|
* Get Files
|
||||||
{
|
*
|
||||||
$cache_file = $js_root.'cache/'.$_GET['g'];
|
* Concatenates the javascript files for the current
|
||||||
|
* group as a string
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function get_files()
|
||||||
|
{
|
||||||
|
$js = '';
|
||||||
|
|
||||||
foreach($groups[$_GET['g']] as $file)
|
foreach($this->js_group as $file)
|
||||||
{
|
{
|
||||||
$new_file = realpath($js_root.$file);
|
$new_file = realpath("{$this->js_root}{$file}");
|
||||||
$modified[] = filemtime($new_file);
|
$js .= file_get_contents($new_file) . "\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
//Add this page too, as well as the groups file
|
return $js;
|
||||||
$modified[] = filemtime($this_file);
|
}
|
||||||
$modified[] = filemtime($groups_file);
|
|
||||||
|
|
||||||
$cache_modified = 0;
|
/**
|
||||||
|
* Get the most recent modified date
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
protected function get_last_modified()
|
||||||
|
{
|
||||||
|
$modified = array();
|
||||||
|
|
||||||
//Add the cache file
|
foreach($this->js_group as $file)
|
||||||
if(is_file($cache_file))
|
{
|
||||||
{
|
$new_file = realpath("{$this->js_root}{$file}");
|
||||||
$cache_modified = filemtime($cache_file);
|
$modified[] = filemtime($new_file);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else //Nothing to display? Just exit
|
|
||||||
{
|
|
||||||
die("You must specify a group that exists");
|
|
||||||
}
|
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
//Add this page too, as well as the groups file
|
||||||
|
$modified[] = filemtime(__FILE__);
|
||||||
|
$modified[] = filemtime($this->js_groups_file);
|
||||||
|
|
||||||
//Get the latest modified date
|
rsort($modified);
|
||||||
rsort($modified);
|
$last_modified = $modified[0];
|
||||||
$last_modified = $modified[0];
|
|
||||||
|
|
||||||
$requested_time=(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
|
return $last_modified;
|
||||||
? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])
|
}
|
||||||
: time();
|
|
||||||
|
|
||||||
// If the browser's cached version is up to date,
|
/**
|
||||||
// don't resend the file
|
* Minifies javascript using google's closure compiler
|
||||||
if($last_modified === $requested_time)
|
*
|
||||||
{
|
* @param string $js
|
||||||
header("HTTP/1.1 304 Not Modified");
|
* @return string
|
||||||
exit();
|
*/
|
||||||
}
|
protected function minify($js)
|
||||||
|
{
|
||||||
|
$options = [
|
||||||
|
'output_info' => 'errors',
|
||||||
|
'output_format' => 'json',
|
||||||
|
'compilation_level' => 'SIMPLE_OPTIMIZATIONS',
|
||||||
|
//'compilation_level' => 'ADVANCED_OPTIMIZATIONS',
|
||||||
|
'js_code' => $js,
|
||||||
|
'language' => 'ECMASCRIPT6_STRICT',
|
||||||
|
'language_out' => 'ECMASCRIPT5_STRICT'
|
||||||
|
];
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
// Check for errors
|
||||||
|
$this->check_minify_errors($options);
|
||||||
|
|
||||||
//Determine what to do: rebuild cache, send files as is, or send cache.
|
// Now actually retrieve the compiled code
|
||||||
if($cache_modified < $last_modified)
|
$options['output_info'] = 'compiled_code';
|
||||||
{
|
$res = $this->closure_call($options);
|
||||||
$js = google_min(get_files());
|
$json = $res->getBody();
|
||||||
$cs = file_put_contents($cache_file, $js);
|
$obj = json_decode($json);
|
||||||
|
|
||||||
//Make sure cache file gets created/updated
|
return $obj->compiledCode;
|
||||||
if($cs === FALSE)
|
}
|
||||||
{
|
|
||||||
die("Cache file was not created. Make sure you have the correct folder permissions.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// If debug is set, just concatenate
|
|
||||||
else if(isset($_GET['debug']))
|
|
||||||
{
|
|
||||||
$js = get_files();
|
|
||||||
}
|
|
||||||
// Otherwise, send the cached file
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$js = file_get_contents($cache_file);
|
|
||||||
}
|
|
||||||
|
|
||||||
// --------------------------------------------------------------------------
|
/**
|
||||||
|
* Output the minified javascript
|
||||||
|
*
|
||||||
|
* @param string $js
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function output($js)
|
||||||
|
{
|
||||||
|
$this->send_final_output($js, 'application/javascript', $this->last_modified);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//This GZIPs the js for transmission to the user
|
// --------------------------------------------------------------------------
|
||||||
//making file size smaller and transfer rate quicker
|
// ! Start Minifying
|
||||||
ob_start("ob_gzhandler");
|
// --------------------------------------------------------------------------
|
||||||
|
|
||||||
// Set important caching headers
|
$config = require_once('config/config.php');
|
||||||
header("Content-Type: application/javascript; charset=utf8");
|
$groups = require_once($config['js_groups_file']);
|
||||||
header("Cache-control: public, max-age=691200, must-revalidate");
|
$cache_dir = "{$config['js_root']}cache";
|
||||||
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($this_file) + 691200))." GMT");
|
|
||||||
|
|
||||||
echo $js;
|
if ( ! is_dir($cache_dir))
|
||||||
|
{
|
||||||
|
mkdir($cache_dir);
|
||||||
|
}
|
||||||
|
|
||||||
ob_end_flush();
|
if ( ! array_key_exists($_GET['g'], $groups))
|
||||||
//end of js.php
|
{
|
||||||
|
throw new InvalidArgumentException('You must specify a js group that exists');
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
new JSMin($config, $groups);
|
||||||
|
}
|
||||||
|
catch (FileNotChangedException $e)
|
||||||
|
{
|
||||||
|
BaseMin::send304();
|
||||||
|
}
|
||||||
|
|
||||||
|
//end of js.php
|
117
min.php
Normal file
117
min.php
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Easy Min
|
||||||
|
*
|
||||||
|
* Simple minification for better website performance
|
||||||
|
*
|
||||||
|
* @author Timothy J. Warren
|
||||||
|
* @copyright Copyright (c) 2012-2016
|
||||||
|
* @link https://git.timshomepage.net/aviat4ion/Easy-Min
|
||||||
|
* @license http://philsturgeon.co.uk/code/dbad-license
|
||||||
|
*/
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------------
|
||||||
|
namespace Aviat\EasyMin;
|
||||||
|
|
||||||
|
//Creative rewriting of /g/groupname to ?g=groupname
|
||||||
|
$pi = $_SERVER['PATH_INFO'];
|
||||||
|
$pia = explode('/', $pi);
|
||||||
|
|
||||||
|
$pia_len = count($pia);
|
||||||
|
$i = 1;
|
||||||
|
|
||||||
|
while($i < $pia_len)
|
||||||
|
{
|
||||||
|
$j = $i+1;
|
||||||
|
$j = (isset($pia[$j])) ? $j : $i;
|
||||||
|
|
||||||
|
$_GET[$pia[$i]] = $pia[$j];
|
||||||
|
|
||||||
|
$i = $j + 1;
|
||||||
|
};
|
||||||
|
|
||||||
|
class FileNotChangedException extends \Exception {}
|
||||||
|
class BaseMin {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get value of the if-modified-since header
|
||||||
|
*
|
||||||
|
* @return int - timestamp to compare for cache control
|
||||||
|
*/
|
||||||
|
protected function get_if_modified()
|
||||||
|
{
|
||||||
|
return (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER))
|
||||||
|
? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])
|
||||||
|
: time();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get value of etag to compare to hash of output
|
||||||
|
*
|
||||||
|
* @return string - the etag to compare
|
||||||
|
*/
|
||||||
|
protected function get_if_none_match()
|
||||||
|
{
|
||||||
|
return (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER))
|
||||||
|
? $_SERVER['HTTP_IF_NONE_MATCH']
|
||||||
|
: '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether or not to send debug version
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
protected function is_not_debug()
|
||||||
|
{
|
||||||
|
return ! $this->is_debug_call();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determine whether or not to send debug version
|
||||||
|
*
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
protected function is_debug_call()
|
||||||
|
{
|
||||||
|
return array_key_exists('debug', $_GET);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send actual output to browser
|
||||||
|
*
|
||||||
|
* @param string $content - the body of the response
|
||||||
|
* @param string $mime_type - the content type
|
||||||
|
* @param int $last_modified - the last modified date
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function send_final_output($content, $mime_type, $last_modified)
|
||||||
|
{
|
||||||
|
//This GZIPs the CSS for transmission to the user
|
||||||
|
//making file size smaller and transfer rate quicker
|
||||||
|
ob_start("ob_gzhandler");
|
||||||
|
|
||||||
|
$expires = $last_modified + 691200;
|
||||||
|
$last_modified_date = gmdate('D, d M Y H:i:s', $last_modified);
|
||||||
|
$expires_date = gmdate('D, d M Y H:i:s', $expires);
|
||||||
|
|
||||||
|
header("Content-Type: {$mime_type}; charset=utf8");
|
||||||
|
header("Cache-control: public, max-age=691200, must-revalidate");
|
||||||
|
header("Last-Modified: {$last_modified_date} GMT");
|
||||||
|
header("Expires: {$expires_date} GMT");
|
||||||
|
|
||||||
|
echo $content;
|
||||||
|
|
||||||
|
ob_end_flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Send a 304 Not Modified header
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public static function send304()
|
||||||
|
{
|
||||||
|
header("status: 304 Not Modified", true, 304);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user