mirror of
https://github.com/mrclay/minify.git
synced 2025-08-31 09:31:48 +02:00
Apply php-cs-fixer new rules
This commit is contained in:
@@ -7,15 +7,15 @@ if (is_file(__DIR__ . '/bootstrap.php')) {
|
||||
$bootstrap_file = __DIR__ . '/../bootstrap.php';
|
||||
}
|
||||
|
||||
$send_400 = function($content = 'Bad URL') {
|
||||
$send_400 = function ($content = 'Bad URL') {
|
||||
http_response_code(400);
|
||||
die($content);
|
||||
};
|
||||
|
||||
$send_301 = function($url) {
|
||||
$send_301 = function ($url) {
|
||||
http_response_code(301);
|
||||
header("Cache-Control: max-age=31536000");
|
||||
header("Location: $url");
|
||||
header('Cache-Control: max-age=31536000');
|
||||
header("Location: ${url}");
|
||||
exit;
|
||||
};
|
||||
|
||||
@@ -59,7 +59,7 @@ $query = $m[2];
|
||||
// these parameters anyway.
|
||||
$get_params = array();
|
||||
foreach (explode('&', $query) as $piece) {
|
||||
if (false === strpos($piece, '=')) {
|
||||
if (strpos($piece, '=') === false) {
|
||||
$send_400();
|
||||
}
|
||||
|
||||
@@ -97,31 +97,31 @@ if (!$sources) {
|
||||
die('File not found');
|
||||
}
|
||||
if ($sources[0]->getId() === 'id::missingFile') {
|
||||
$send_400("Bad URL: missing file");
|
||||
$send_400('Bad URL: missing file');
|
||||
}
|
||||
|
||||
// we need URL to end in appropriate extension
|
||||
$type = $sources[0]->getContentType();
|
||||
$ext = ($type === Minify::TYPE_JS) ? '.js' : '.css';
|
||||
if (substr($query, - strlen($ext)) !== $ext) {
|
||||
$send_301("$root_uri/$cache_time/{$query}&z=$ext");
|
||||
if (substr($query, -strlen($ext)) !== $ext) {
|
||||
$send_301("${root_uri}/${cache_time}/{$query}&z=${ext}");
|
||||
}
|
||||
|
||||
// fix the cache dir in the URL
|
||||
if ($cache_time !== $requested_cache_dir) {
|
||||
$send_301("$root_uri/$cache_time/$query");
|
||||
$send_301("${root_uri}/${cache_time}/${query}");
|
||||
}
|
||||
|
||||
$content = $app->minify->combine($sources);
|
||||
|
||||
// save and send file
|
||||
$file = __DIR__ . "/$cache_time/$query";
|
||||
$file = __DIR__ . "/${cache_time}/${query}";
|
||||
if (!is_dir(dirname($file))) {
|
||||
mkdir(dirname($file), 0777, true);
|
||||
}
|
||||
|
||||
file_put_contents($file, $content);
|
||||
|
||||
header("Content-Type: $type;charset=utf-8");
|
||||
header("Cache-Control: max-age=31536000");
|
||||
header("Content-Type: ${type};charset=utf-8");
|
||||
header('Cache-Control: max-age=31536000');
|
||||
echo $content;
|
||||
|
@@ -8,32 +8,37 @@ namespace Minify\StaticService;
|
||||
* @param string $static_uri E.g. "/min/static"
|
||||
* @param string $query E.g. "b=scripts&f=1.js,2.js"
|
||||
* @param string $type "css" or "js"
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function build_uri($static_uri, $query, $type) {
|
||||
function build_uri($static_uri, $query, $type)
|
||||
{
|
||||
$static_uri = rtrim($static_uri, '/');
|
||||
$query = ltrim($query, '?');
|
||||
|
||||
$ext = ".$type";
|
||||
if (substr($query, - strlen($ext)) !== $ext) {
|
||||
$query .= "&z=$ext";
|
||||
$ext = ".${type}";
|
||||
if (substr($query, -strlen($ext)) !== $ext) {
|
||||
$query .= "&z=${ext}";
|
||||
}
|
||||
|
||||
$cache_time = get_cache_time();
|
||||
|
||||
return "$static_uri/$cache_time/$query";
|
||||
return "${static_uri}/${cache_time}/${query}";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of the current cache directory within static/. E.g. "1467089473"
|
||||
*
|
||||
* @param bool $auto_create Automatically create the directory if missing?
|
||||
* @return null|string null if missing or can't create
|
||||
*
|
||||
* @return string|null null if missing or can't create
|
||||
*/
|
||||
function get_cache_time($auto_create = true) {
|
||||
function get_cache_time($auto_create = true)
|
||||
{
|
||||
foreach (scandir(__DIR__) as $entry) {
|
||||
if (ctype_digit($entry)) {
|
||||
return $entry;
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -42,26 +47,28 @@ function get_cache_time($auto_create = true) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$time = (string)time();
|
||||
if (!mkdir(__DIR__ . "/$time")) {
|
||||
$time = (string) time();
|
||||
if (!mkdir(__DIR__ . "/${time}")) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $time;
|
||||
}
|
||||
|
||||
function flush_cache() {
|
||||
function flush_cache()
|
||||
{
|
||||
$time = get_cache_time(false);
|
||||
if ($time) {
|
||||
remove_tree(__DIR__ . "/$time");
|
||||
remove_tree(__DIR__ . "/${time}");
|
||||
}
|
||||
}
|
||||
|
||||
function remove_tree($dir) {
|
||||
function remove_tree($dir)
|
||||
{
|
||||
$files = array_diff(scandir($dir), array('.', '..'));
|
||||
|
||||
foreach ($files as $file) {
|
||||
is_dir("$dir/$file") ? remove_tree("$dir/$file") : unlink("$dir/$file");
|
||||
is_dir("${dir}/${file}") ? remove_tree("${dir}/${file}") : unlink("${dir}/${file}");
|
||||
}
|
||||
|
||||
return rmdir($dir);
|
||||
|
Reference in New Issue
Block a user