Skip to content

Instantly share code, notes, and snippets.

@dhotson
Created March 22, 2010 09:42
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 dhotson/339915 to your computer and use it in GitHub Desktop.
Save dhotson/339915 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
chdir(dirname(__FILE__));
require_once '../lib/kelpie/kelpie_init.php';
require_once('../includes/myframework.php');
class MyApp
{
public function __construct()
{
$this->_frontController = new MyFramework_Controller_FrontController();
}
public function call($env)
{
// basic static content handling
if (preg_match('#/static/#', $env['PATH_INFO']))
{
$path = '../htdocs'.$env['PATH_INFO']; // probably not secure..
if (file_exists($path))
{
// dodgy mime type detection..
$type = preg_match('/\.css$/', $path)
? 'text/css'
: ( preg_match('/\.js$/', $path)
? 'text/js'
: 'text/plain');
return array(
200,
array('Content-Type' => $type),
array(file_get_contents($path))
);
}
}
$this->_setGlobals($env);
$response = $this->_frontController->dispatch(MyFramework::getRequest()->getPath());
$headers = array();
foreach ($response->getHeaders() as $h)
{
$tmp = explode(': ', $h, 2);
$headers[$tmp[0]] = $tmp[1];
}
return array(
$response->getStatusCode(),
$headers,
array($response->getContent())
);
}
private function _setGlobals($env)
{
$_SERVER = $env;
$_GET = array();
if (!empty($env['QUERY_STRING']))
{
foreach (explode('&', $env['QUERY_STRING']) as $str)
{
$kv = explode('=', $str, 2);
$_GET[$kv[0]] = isset($kv[1]) ? $kv[1] : '';
}
}
$_POST = array();
if (!empty($env['REQUEST_BODY']))
{
foreach (explode('&', $env['REQUEST_BODY']) as $str)
{
$kv = explode('=', $str, 2);
$_POST[$kv[0]] = isset($kv[1]) ? $kv[1] : '';
}
}
$_COOKIE = array();
if (!empty($env['HTTP_COOKIE']))
{
foreach (explode('; ', $env['HTTP_COOKIE']) as $str)
{
$kv = explode('=', $str, 2);
$_COOKIE[$kv[0]] = isset($kv[1]) ? $kv[1] : '';
}
}
}
}
$server = new Kelpie_Server('0.0.0.0', 80);
$server->start(new MyApp());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment