1
0
mirror of https://github.com/mrclay/minify.git synced 2025-08-11 16:44:44 +02:00

Minify now allows static file serving

With slightly altered URLs, Minify can cache files so they're served directly
from the filesystem instead of through PHP. A simple library helps create URLs
and clearing the cache.

See `static/README.md` for details.
This commit is contained in:
Steve Clay
2016-06-28 01:12:01 -04:00
parent 22fb644834
commit 59d4c97ffc
10 changed files with 340 additions and 1 deletions

68
static/lib.php Normal file
View File

@@ -0,0 +1,68 @@
<?php
namespace Minify\StaticService;
/**
* Build a URI for the static cache
*
* @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) {
$static_uri = rtrim($static_uri, '/');
$query = ltrim($query, '?');
$ext = ".$type";
if (substr($query, - strlen($ext)) !== $ext) {
$query .= "&z=$ext";
}
$cache_time = get_cache_time();
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
*/
function get_cache_time($auto_create = true) {
foreach (scandir(__DIR__) as $entry) {
if (ctype_digit($entry)) {
return $entry;
break;
}
}
if (!$auto_create) {
return null;
}
$time = (string)time();
if (!mkdir(__DIR__ . "/$time")) {
return null;
}
return $time;
}
function flush_cache() {
$time = get_cache_time(false);
if ($time) {
remove_tree(__DIR__ . "/$time");
}
}
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");
}
return rmdir($dir);
}