Created
April 15, 2013 14:17
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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