Created
April 2, 2012 22:08
-
-
Save christianchristensen/2287554 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env php | |
<?php | |
/** | |
* Pre PHP 5.4 local PHP server wrapper script | |
* Host a "PHP website" out of any directory. | |
* | |
* Uses: https://github.com/youngj/httpserver | |
* Clone the above repo and put this script at | |
* the same level at the httpserver folder and | |
* run it. Pass "-h" for help. | |
*/ | |
require_once __DIR__ . '/httpserver/httpserver.php'; | |
class SimpleLocalServer extends HTTPServer | |
{ | |
private $directory; | |
function __construct($addr = "127.0.0.1", $port = 8000, $dir = '') | |
{ | |
$this->directory = getcwd() . $dir; | |
parent::__construct(array( | |
'addr' => $addr, | |
'port' => $port, | |
)); | |
} | |
function route_request($request) | |
{ | |
$uri = $request->uri; | |
$doc_root = $this->directory; | |
if (preg_match('#/$#', $uri)) | |
{ | |
$uri .= "index.php"; | |
} | |
if (preg_match('#\.php$#', $uri)) | |
{ | |
return $this->get_php_response($request, "$doc_root$uri"); | |
} | |
else | |
{ | |
return $this->get_static_response($request, "$doc_root$uri"); | |
} | |
} | |
} | |
$options = getopt("a::p::d::h::"); | |
if (array_key_exists('h', $options)) { | |
echo <<<END | |
-h\t\tThis help page. | |
-a\t\tSpecify the address to host on. | |
-p\t\tSpecify the port to listen on. | |
-d\t\tSpecify the path to host. | |
END; | |
exit(); | |
} | |
$addr = array_key_exists('a', $options) ? $options['a'] : "127.0.0.1"; | |
$port = array_key_exists('p', $options) ? $options['p'] : 8000; | |
$dir = array_key_exists('d', $options) ? $options['d'] : ""; | |
$server = new SimpleLocalServer($addr, $port, $dir); | |
$server->run_forever(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment