Version 5.1 - All the GraphQL #32
@ -23,21 +23,21 @@ require_once('./min.php');
|
|||||||
*/
|
*/
|
||||||
class CSSMin extends BaseMin {
|
class CSSMin extends BaseMin {
|
||||||
|
|
||||||
protected $css_root;
|
protected $cssRoot;
|
||||||
protected $path_from;
|
protected $pathFrom;
|
||||||
protected $path_to;
|
protected $pathTo;
|
||||||
protected $group;
|
protected $group;
|
||||||
protected $last_modified;
|
protected $lastModified;
|
||||||
protected $requested_time;
|
protected $requestedTime;
|
||||||
|
|
||||||
public function __construct(array $config, array $groups)
|
public function __construct(array $config, array $groups)
|
||||||
{
|
{
|
||||||
$group = $_GET['g'];
|
$group = $_GET['g'];
|
||||||
$this->css_root = $config['css_root'];
|
$this->cssRoot = $config['css_root'];
|
||||||
$this->path_from = $config['path_from'];
|
$this->pathFrom = $config['path_from'];
|
||||||
$this->path_to = $config['path_to'];
|
$this->pathTo = $config['path_to'];
|
||||||
$this->group = $groups[$group];
|
$this->group = $groups[$group];
|
||||||
$this->last_modified = $this->get_last_modified();
|
$this->lastModified = $this->getLastModified();
|
||||||
|
|
||||||
$this->send();
|
$this->send();
|
||||||
}
|
}
|
||||||
@ -49,14 +49,14 @@ class CSSMin extends BaseMin {
|
|||||||
*/
|
*/
|
||||||
protected function send()
|
protected function send()
|
||||||
{
|
{
|
||||||
if($this->last_modified >= $this->get_if_modified() && $this->is_not_debug())
|
if($this->lastModified >= $this->getIfModified() && $this->isNotDebug())
|
||||||
{
|
{
|
||||||
throw new FileNotChangedException();
|
throw new FileNotChangedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
$css = ( ! array_key_exists('debug', $_GET))
|
$css = ( ! array_key_exists('debug', $_GET))
|
||||||
? $this->compress($this->get_css())
|
? $this->compress($this->getCss())
|
||||||
: $this->get_css();
|
: $this->getCss();
|
||||||
|
|
||||||
$this->output($css);
|
$this->output($css);
|
||||||
}
|
}
|
||||||
@ -99,7 +99,7 @@ class CSSMin extends BaseMin {
|
|||||||
*
|
*
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
protected function get_last_modified()
|
protected function getLastModified()
|
||||||
{
|
{
|
||||||
$modified = [];
|
$modified = [];
|
||||||
|
|
||||||
@ -108,8 +108,8 @@ class CSSMin extends BaseMin {
|
|||||||
{
|
{
|
||||||
foreach($this->group as $file)
|
foreach($this->group as $file)
|
||||||
{
|
{
|
||||||
$new_file = realpath("{$this->css_root}{$file}");
|
$newFile = realpath("{$this->cssRoot}{$file}");
|
||||||
$modified[] = filemtime($new_file);
|
$modified[] = filemtime($newFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,19 +127,19 @@ class CSSMin extends BaseMin {
|
|||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function get_css()
|
protected function getCss()
|
||||||
{
|
{
|
||||||
$css = '';
|
$css = '';
|
||||||
|
|
||||||
foreach($this->group as $file)
|
foreach($this->group as $file)
|
||||||
{
|
{
|
||||||
$new_file = realpath("{$this->css_root}{$file}");
|
$newFile = realpath("{$this->cssRoot}{$file}");
|
||||||
$css .= file_get_contents($new_file);
|
$css .= file_get_contents($newFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Correct paths that have changed due to concatenation
|
// Correct paths that have changed due to concatenation
|
||||||
// based on rules in the config file
|
// based on rules in the config file
|
||||||
$css = str_replace($this->path_from, $this->path_to, $css);
|
$css = str_replace($this->pathFrom, $this->pathTo, $css);
|
||||||
|
|
||||||
return $css;
|
return $css;
|
||||||
}
|
}
|
||||||
@ -151,7 +151,7 @@ class CSSMin extends BaseMin {
|
|||||||
*/
|
*/
|
||||||
protected function output($css)
|
protected function output($css)
|
||||||
{
|
{
|
||||||
$this->send_final_output($css, 'text/css', $this->last_modified);
|
$this->sendFinalOutput($css, 'text/css', $this->lastModified);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,27 +29,27 @@ require_once('./min.php');
|
|||||||
*/
|
*/
|
||||||
class JSMin extends BaseMin {
|
class JSMin extends BaseMin {
|
||||||
|
|
||||||
protected $js_root;
|
protected $jsRoot;
|
||||||
protected $js_group;
|
protected $jsGroup;
|
||||||
protected $js_groups_file;
|
protected $jsGroupsFile;
|
||||||
protected $cache_file;
|
protected $cacheFile;
|
||||||
|
|
||||||
protected $last_modified;
|
protected $lastModified;
|
||||||
protected $requested_time;
|
protected $requestedTime;
|
||||||
protected $cache_modified;
|
protected $cacheModified;
|
||||||
|
|
||||||
public function __construct(array $config, array $groups)
|
public function __construct(array $config, array $groups)
|
||||||
{
|
{
|
||||||
$group = $_GET['g'];
|
$group = $_GET['g'];
|
||||||
|
|
||||||
$this->js_root = $config['js_root'];
|
$this->jsRoot = $config['js_root'];
|
||||||
$this->js_group = $groups[$group];
|
$this->jsGroup = $groups[$group];
|
||||||
$this->js_groups_file = $config['js_groups_file'];
|
$this->jsGroupsFile = $config['js_groups_file'];
|
||||||
$this->cache_file = "{$this->js_root}cache/{$group}";
|
$this->cacheFile = "{$this->jsRoot}cache/{$group}";
|
||||||
$this->last_modified = $this->get_last_modified();
|
$this->lastModified = $this->getLastModified();
|
||||||
|
|
||||||
$this->cache_modified = (is_file($this->cache_file))
|
$this->cacheModified = (is_file($this->cacheFile))
|
||||||
? filemtime($this->cache_file)
|
? filemtime($this->cacheFile)
|
||||||
: 0;
|
: 0;
|
||||||
|
|
||||||
// Output some JS!
|
// Output some JS!
|
||||||
@ -59,24 +59,24 @@ class JSMin extends BaseMin {
|
|||||||
protected function send()
|
protected function send()
|
||||||
{
|
{
|
||||||
// Override caching if debug key is set
|
// Override caching if debug key is set
|
||||||
if($this->is_debug_call())
|
if($this->isDebugCall())
|
||||||
{
|
{
|
||||||
return $this->output($this->get_files());
|
return $this->output($this->getFiles());
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the browser's cached version is up to date,
|
// If the browser's cached version is up to date,
|
||||||
// don't resend the file
|
// don't resend the file
|
||||||
if($this->last_modified == $this->get_if_modified() && $this->is_not_debug())
|
if($this->lastModified == $this->getIfModified() && $this->isNotDebug())
|
||||||
{
|
{
|
||||||
throw new FileNotChangedException();
|
throw new FileNotChangedException();
|
||||||
}
|
}
|
||||||
|
|
||||||
if($this->cache_modified < $this->last_modified)
|
if($this->cacheModified < $this->lastModified)
|
||||||
{
|
{
|
||||||
$js = $this->minify($this->get_files());
|
$js = $this->minify($this->getFiles());
|
||||||
|
|
||||||
//Make sure cache file gets created/updated
|
//Make sure cache file gets created/updated
|
||||||
if (file_put_contents($this->cache_file, $js) === FALSE)
|
if (file_put_contents($this->cacheFile, $js) === FALSE)
|
||||||
{
|
{
|
||||||
echo 'Cache file was not created. Make sure you have the correct folder permissions.';
|
echo 'Cache file was not created. Make sure you have the correct folder permissions.';
|
||||||
return;
|
return;
|
||||||
@ -86,7 +86,7 @@ class JSMin extends BaseMin {
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return $this->output(file_get_contents($this->cache_file));
|
return $this->output(file_get_contents($this->cacheFile));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,7 +96,7 @@ class JSMin extends BaseMin {
|
|||||||
* @param array $options - Form parameters
|
* @param array $options - Form parameters
|
||||||
* @return object
|
* @return object
|
||||||
*/
|
*/
|
||||||
protected function closure_call(array $options)
|
protected function closureCall(array $options)
|
||||||
{
|
{
|
||||||
$formFields = http_build_query($options);
|
$formFields = http_build_query($options);
|
||||||
|
|
||||||
@ -123,19 +123,19 @@ class JSMin extends BaseMin {
|
|||||||
* @param array $options
|
* @param array $options
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function check_minify_errors($options)
|
protected function checkMinifyErrors($options)
|
||||||
{
|
{
|
||||||
$error_res = $this->closure_call($options);
|
$errorRes = $this->closureCall($options);
|
||||||
$error_json = $error_res->getBody();
|
$errorJson = $errorRes->getBody();
|
||||||
$error_obj = Json::decode($error_json) ?: (object)[];
|
$errorObj = Json::decode($errorJson) ?: (object)[];
|
||||||
|
|
||||||
|
|
||||||
// Show error if exists
|
// Show error if exists
|
||||||
if ( ! empty($error_obj->errors) || ! empty($error_obj->serverErrors))
|
if ( ! empty($errorObj->errors) || ! empty($errorObj->serverErrors))
|
||||||
{
|
{
|
||||||
$error_json = Json::encode($error_obj, JSON_PRETTY_PRINT);
|
$errorJson = Json::encode($errorObj, JSON_PRETTY_PRINT);
|
||||||
header('Content-type: application/javascript');
|
header('Content-type: application/javascript');
|
||||||
echo "console.error(${error_json});";
|
echo "console.error(${errorJson});";
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -148,14 +148,14 @@ class JSMin extends BaseMin {
|
|||||||
*
|
*
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
protected function get_files()
|
protected function getFiles()
|
||||||
{
|
{
|
||||||
$js = '';
|
$js = '';
|
||||||
|
|
||||||
foreach($this->js_group as $file)
|
foreach($this->jsGroup as $file)
|
||||||
{
|
{
|
||||||
$new_file = realpath("{$this->js_root}{$file}");
|
$newFile = realpath("{$this->jsRoot}{$file}");
|
||||||
$js .= file_get_contents($new_file) . "\n\n";
|
$js .= file_get_contents($newFile) . "\n\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
return $js;
|
return $js;
|
||||||
@ -166,24 +166,24 @@ class JSMin extends BaseMin {
|
|||||||
*
|
*
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
protected function get_last_modified()
|
protected function getLastModified()
|
||||||
{
|
{
|
||||||
$modified = [];
|
$modified = [];
|
||||||
|
|
||||||
foreach($this->js_group as $file)
|
foreach($this->jsGroup as $file)
|
||||||
{
|
{
|
||||||
$new_file = realpath("{$this->js_root}{$file}");
|
$newFile = realpath("{$this->jsRoot}{$file}");
|
||||||
$modified[] = filemtime($new_file);
|
$modified[] = filemtime($newFile);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Add this page too, as well as the groups file
|
//Add this page too, as well as the groups file
|
||||||
$modified[] = filemtime(__FILE__);
|
$modified[] = filemtime(__FILE__);
|
||||||
$modified[] = filemtime($this->js_groups_file);
|
$modified[] = filemtime($this->jsGroupsFile);
|
||||||
|
|
||||||
rsort($modified);
|
rsort($modified);
|
||||||
$last_modified = $modified[0];
|
$lastModified = $modified[0];
|
||||||
|
|
||||||
return $last_modified;
|
return $lastModified;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -205,11 +205,11 @@ class JSMin extends BaseMin {
|
|||||||
];
|
];
|
||||||
|
|
||||||
// Check for errors
|
// Check for errors
|
||||||
$this->check_minify_errors($options);
|
$this->checkMinifyErrors($options);
|
||||||
|
|
||||||
// Now actually retrieve the compiled code
|
// Now actually retrieve the compiled code
|
||||||
$options['output_info'] = 'compiled_code';
|
$options['output_info'] = 'compiled_code';
|
||||||
$res = $this->closure_call($options);
|
$res = $this->closureCall($options);
|
||||||
$json = $res->getBody();
|
$json = $res->getBody();
|
||||||
$obj = Json::decode($json);
|
$obj = Json::decode($json);
|
||||||
|
|
||||||
@ -225,7 +225,7 @@ class JSMin extends BaseMin {
|
|||||||
*/
|
*/
|
||||||
protected function output($js)
|
protected function output($js)
|
||||||
{
|
{
|
||||||
$this->send_final_output($js, 'application/javascript', $this->last_modified);
|
$this->sendFinalOutput($js, 'application/javascript', $this->lastModified);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -235,11 +235,11 @@ class JSMin extends BaseMin {
|
|||||||
|
|
||||||
$config = require_once('../app/appConf/minify_config.php');
|
$config = require_once('../app/appConf/minify_config.php');
|
||||||
$groups = require_once($config['js_groups_file']);
|
$groups = require_once($config['js_groups_file']);
|
||||||
$cache_dir = "{$config['js_root']}cache";
|
$cacheDir = "{$config['js_root']}cache";
|
||||||
|
|
||||||
if ( ! is_dir($cache_dir))
|
if ( ! is_dir($cacheDir))
|
||||||
{
|
{
|
||||||
mkdir($cache_dir);
|
mkdir($cacheDir);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( ! array_key_exists($_GET['g'], $groups))
|
if ( ! array_key_exists($_GET['g'], $groups))
|
||||||
|
@ -17,10 +17,10 @@ namespace Aviat\EasyMin;
|
|||||||
$pi = $_SERVER['PATH_INFO'];
|
$pi = $_SERVER['PATH_INFO'];
|
||||||
$pia = explode('/', $pi);
|
$pia = explode('/', $pi);
|
||||||
|
|
||||||
$pia_len = count($pia);
|
$piaLen = count($pia);
|
||||||
$i = 1;
|
$i = 1;
|
||||||
|
|
||||||
while($i < $pia_len)
|
while($i < $piaLen)
|
||||||
{
|
{
|
||||||
$j = $i+1;
|
$j = $i+1;
|
||||||
$j = (isset($pia[$j])) ? $j : $i;
|
$j = (isset($pia[$j])) ? $j : $i;
|
||||||
@ -38,7 +38,7 @@ class BaseMin {
|
|||||||
*
|
*
|
||||||
* @return int - timestamp to compare for cache control
|
* @return int - timestamp to compare for cache control
|
||||||
*/
|
*/
|
||||||
protected function get_if_modified()
|
protected function getIfModified()
|
||||||
{
|
{
|
||||||
return (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER))
|
return (array_key_exists('HTTP_IF_MODIFIED_SINCE', $_SERVER))
|
||||||
? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])
|
? strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE'])
|
||||||
@ -50,7 +50,7 @@ class BaseMin {
|
|||||||
*
|
*
|
||||||
* @return string - the etag to compare
|
* @return string - the etag to compare
|
||||||
*/
|
*/
|
||||||
protected function get_if_none_match()
|
protected function getIfNoneMatch()
|
||||||
{
|
{
|
||||||
return (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER))
|
return (array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER))
|
||||||
? $_SERVER['HTTP_IF_NONE_MATCH']
|
? $_SERVER['HTTP_IF_NONE_MATCH']
|
||||||
@ -62,9 +62,9 @@ class BaseMin {
|
|||||||
*
|
*
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
protected function is_not_debug()
|
protected function isNotDebug()
|
||||||
{
|
{
|
||||||
return ! $this->is_debug_call();
|
return ! $this->isDebugCall();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -72,7 +72,7 @@ class BaseMin {
|
|||||||
*
|
*
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
protected function is_debug_call()
|
protected function isDebugCall()
|
||||||
{
|
{
|
||||||
return array_key_exists('debug', $_GET);
|
return array_key_exists('debug', $_GET);
|
||||||
}
|
}
|
||||||
@ -81,24 +81,24 @@ class BaseMin {
|
|||||||
* Send actual output to browser
|
* Send actual output to browser
|
||||||
*
|
*
|
||||||
* @param string $content - the body of the response
|
* @param string $content - the body of the response
|
||||||
* @param string $mime_type - the content type
|
* @param string $mimeType - the content type
|
||||||
* @param int $last_modified - the last modified date
|
* @param int $lastModified - the last modified date
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function send_final_output($content, $mime_type, $last_modified)
|
protected function sendFinalOutput($content, $mimeType, $lastModified)
|
||||||
{
|
{
|
||||||
//This GZIPs the CSS for transmission to the user
|
//This GZIPs the CSS for transmission to the user
|
||||||
//making file size smaller and transfer rate quicker
|
//making file size smaller and transfer rate quicker
|
||||||
ob_start("ob_gzhandler");
|
ob_start("ob_gzhandler");
|
||||||
|
|
||||||
$expires = $last_modified + 691200;
|
$expires = $lastModified + 691200;
|
||||||
$last_modified_date = gmdate('D, d M Y H:i:s', $last_modified);
|
$lastModifiedDate = gmdate('D, d M Y H:i:s', $lastModified);
|
||||||
$expires_date = gmdate('D, d M Y H:i:s', $expires);
|
$expiresDate = gmdate('D, d M Y H:i:s', $expires);
|
||||||
|
|
||||||
header("Content-Type: {$mime_type}; charset=utf8");
|
header("Content-Type: {$mimeType}; charset=utf8");
|
||||||
header("Cache-control: public, max-age=691200, must-revalidate");
|
header("Cache-control: public, max-age=691200, must-revalidate");
|
||||||
header("Last-Modified: {$last_modified_date} GMT");
|
header("Last-Modified: {$lastModifiedDate} GMT");
|
||||||
header("Expires: {$expires_date} GMT");
|
header("Expires: {$expiresDate} GMT");
|
||||||
|
|
||||||
echo $content;
|
echo $content;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user