Skip to content

Instantly share code, notes, and snippets.

@shenjunru
Created October 6, 2012 05:52
Show Gist options
  • Save shenjunru/3844145 to your computer and use it in GitHub Desktop.
Save shenjunru/3844145 to your computer and use it in GitHub Desktop.
HTML5 Cache Manifest Builder
<?php
/*!
* HTML5 Cache Manifest Builder
* @author Shen Junru
*
* in Command Line:
* - remote=path (ex://domain/path/)
* - path/type=remote (ex: ../js/*.js=/js)
* in Browser:
* - remote=path (ex://domain/path/)
* - caches=path/type=remote[, ...] (ex: ../js/*.js=/js,../css/*.css)
*/
/**
* separates a string to ($key, $val) by a separator string
*
* @param string $string - string to be separated
* @param string $separator - separator string
* @param string &$key - result: $key
* @param string &$val - result: $val
* @param bool [$keep=false] - true as keeps the separator on the key side
*/
function separate($string, $separator, &$key='', &$val='', $keep=FALSE){
$pos=(int)strpos($string, $separator);
$pos=0<$pos?$pos:strlen($string);
$key=substr($string, 0, $keep?$pos+1:$pos);
$val=substr($string, $pos+1);
}
/**
* parses a parameter string to ($path, $type, $remote)
*
* @param string $param - string to be parsed
* @param string &$path - result: $path
* @param string &$type - result: $type
* @param string &$remote - result: $remote
*/
function parseParam($param, &$path='', &$type='', &$remote=''){
separate($param, '=', $path, $remote);
separate($path, '/', $path, $type, TRUE);
}
/**
* recursive finds files in the directory
*
* @param string &$caches - result: $caches matched files paths
* @param string &$hashes - result: $hashes md5 hashes
* @param string $path - directory path
* @param string $type - file type
* @param string $remote - remote path prefix
*/
function findFiles(&$caches, &$hashes, $path, $type='*.*', $remote='', /*internals*/$local='', $self=FALSE){
// remote path prefix
$remote=dirname((empty($remote)?$path:$remote).'/#').'/';
// directory path
$path=realpath(($self?'':__DIR__)."/$path");
// local path prefix
$local=dirname((empty($local)?$path:$local).'/#');
// finds files in this directory
$files=glob("$path/$type");
if (!empty($files)) {
$caches.=str_replace($local.'/', $remote, join("\n", $files))."\n";
foreach ($files as $file) {
$hashes.=md5_file($file);
}
}
// recursive subdirectories
$dirs=glob("$path/*/");
if (FALSE !== $dirs) {
foreach ($dirs as $dir) {
findFiles($caches, $hashes, $dir, $type, $remote, $local, TRUE);
}
}
}
// parses parameters
$params=array();
$_remote='';
if (isset($argv)) {
// in Command Line
array_shift($argv);
foreach ($argv as $i=>$param) {
if ('remote=' === substr($param, 0, 7)) {
$_remote=substr($param, 7);
} else {
$params[]=$param;
}
}
} else if (isset($_GET)) {
// in Browser
if (isset($_GET['remote'])) {
$_remote=$_GET['remote'];
}
if (isset($_GET['caches'])) {
$params=explode(',', $_GET['caches']);
}
}
$caches='';
$hashes='';
foreach ($params as $param) {
parseParam($param, $path, $type, $remote);
findFiles($caches, $hashes, $path, $type, empty($remote)?$_remote:$remote);
}
$output='CACHE MANIFEST';
$output.="\nNETWORK:\n";
$output.="*\n";
$output.="\nCACHE:\n";
$output.=$caches;
$output.="#".md5($hashes)."\n";
if (!isset($argv)) {
header('Content-Type:text/plain');
}
echo($output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment