Skip to content

Instantly share code, notes, and snippets.

@virasak
Created April 15, 2013 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save virasak/5388399 to your computer and use it in GitHub Desktop.
Save virasak/5388399 to your computer and use it in GitHub Desktop.
Serve AngularJS documents directly from distributed zip file with PHP 5.4 built-in web server.
<?php
// To run this script:
// php localhost:8080 docserver.php
$dirname = 'angular-1.0.6'; // without '.zip'
$uri = $_SERVER['REQUEST_URI'];
if (in_array($uri, array('/', '/docs', '/docs/'))) {
header('Location: /docs/index.html', 302);
} else {
$zip = zip_open($dirname . '.zip');
$entries = array();
$entry_names = array();
while ($entry = zip_read($zip)){
$entries[] = $entry;
$entry_names[] = zip_entry_name($entry);
}
$filename = $dirname . $uri;
if ($index = array_search($filename, $entry_names)) {
if(preg_match("/.css$/",$uri)) {
header('Content-Type: text/css');
} else if(preg_match("/.js$/",$uri)) {
header('Content-Type: application/x-javascript');
} else if(preg_match("/.woff$/",$uri)) {
header('Content-Type: application/octet-stream');
} // other types should be correctly detected by browser
zip_entry_open($zip, $entries[$index]);
while($data = zip_entry_read($entries[$index])) {
echo $data;
}
zip_entry_close($entries[$index]);
} else {
if (preg_match("/^\/docs\//", $uri)) {
header('Location: /docs/index.html#!' . substr($uri, 5), 302);
}
}
zip_close($zip);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment