commit 43a7bae14a79bd0a0f22ae654e8c6e0997a9b772 Author: Jim Myhrberg Date: Tue Jan 12 19:36:01 2010 +0200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5ca0973 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store + diff --git a/app/api.php b/app/api.php new file mode 100644 index 0000000..8771cff --- /dev/null +++ b/app/api.php @@ -0,0 +1,19 @@ +app =& $app; + } + + function call (&$env) { + if ( preg_match("/^\/api\/([a-z]{1}[a-z0-9]*)/i", $env["PATH_INFO"], $match) ) { + return array(200, array("Content-Type" => "text/json"), array("{api_call: \"".$match[1]."\"}")); + } else { + return $this->app->call($env); + } + } + +} + +?> \ No newline at end of file diff --git a/app/app.php b/app/app.php new file mode 100644 index 0000000..11ea17f --- /dev/null +++ b/app/app.php @@ -0,0 +1,16 @@ + "text/html"), array("Welcome Home")); + } elseif ( preg_match("/^\/about\/?/i", $env["PATH_INFO"]) ) { + return array(200, array("Content-Type" => "text/html"), array("Rack-style middleware is cool.")); + } + return Rack::not_found(); + } + +} + +?> \ No newline at end of file diff --git a/app/format.php b/app/format.php new file mode 100644 index 0000000..981ada1 --- /dev/null +++ b/app/format.php @@ -0,0 +1,29 @@ +app =& $app; + } + + function call (&$env) { + + // available formats + $formats = array("txt" => "text/plain", "xml" => "application/xml"); + + // call the next middleware in the stack + list($status, $headers, $body) = $this->app->call($env); + + // do something with response headers + foreach( $formats as $key => $value ) { + if ( !empty($env["request.vars"]["format"]) && $env["request.get"]["format"] == $key ) { + $headers["Content-Type"] = $value; + } + } + + return array($status, $headers, $body); + } + +} + +?> \ No newline at end of file diff --git a/lib/rack.php b/lib/rack.php new file mode 100644 index 0000000..9f5a292 --- /dev/null +++ b/lib/rack.php @@ -0,0 +1,176 @@ + "text/html"), + $constructed = false, + $ob_started = false; + + + public static function init ($middleware = array()) { + + // quick initialization + if ( !empty($middleware) && is_array($middleware) ) { + self::$middleware = array_merge(self::$middleware, $middleware); + } + + // don't output anything before Rack has output it's headers + ob_start(); + self::$ob_started = true; + } + + + public static function add ($name, $file = null) { + if ( !self::$ob_started ) self::init(); + if ( !self::$constructed ) { + self::$middleware[$name] = true; + self::require_file($file); + return true; + } + return false; + } + + + public static function insert_before ($target, $name, $file = null) { + if ( !self::$ob_started ) self::init(); + if ( !self::$constructed ) { + if ( array_key_exists($target, self::$middleware) ) { + $keys = array_keys(self::$middleware); + $length = count($keys); + $middleware = array(); + for ( $i=0; $i < $length; $i++ ) { + if ( $keys[$i] == $target ) { + $middleware[$name] = true; + } + $middleware[$keys[$i]] =& self::$middleware[$keys[$i]]; + } + self::$middleware = $middleware; + self::require_file($file); + return true; + } + } + return false; + } + + + public static function insert_after ($target, $name, $file = null) { + if ( !self::$ob_started ) self::init(); + if ( !self::$constructed ) { + if ( array_key_exists($target, self::$middleware) ) { + $keys = array_keys(self::$middleware); + $length = count($keys); + $middleware = array(); + for ( $i=0; $i < $length; $i++ ) { + $middleware[$keys[$i]] =& self::$middleware[$keys[$i]]; + if ( $keys[$i] == $target ) { + $middleware[$name] = true; + } + } + self::$middleware = $middleware; + self::require_file($file); + return false; + } + } + return false; + } + + + public static function not_found () { + return array(404, array("Content-Type" => "text/html"), "Not Found"); + } + + + public static function run () { + + // build ENV + self::$env =& $_SERVER; + if ( strstr($_SERVER['REQUEST_URI'], '?') ) { + self::$env["PATH_INFO"] = substr($_SERVER['REQUEST_URI'], 0, strpos($_SERVER['REQUEST_URI'], '?')); + } else { + self::$env["PATH_INFO"] = $_SERVER['REQUEST_URI']; + } + self::$env["request.vars"] =& $_REQUEST; + self::$env["request.get"] =& $_GET; + self::$env["request.post"] =& $_POST; + self::$env["request.files"] =& $_FILES; + self::$env["request.method"] =& $_SERVER["REQUEST_METHOD"]; + self::$env["cookies"] =& $_COOKIE; + + // construct middlewares + self::$constructed = true; + $middleware = array_reverse(self::$middleware); + $previous = null; + foreach( $middleware as $key => $value ) { + self::$middleware[$key] = new $key($previous); + $previous =& self::$middleware[$key]; + } + + // call the middleware stack + reset(self::$middleware); + $first = current(array_keys(self::$middleware)); + list($status, $headers, $body) = self::$middleware[$first]->call(self::$env); + + // send headers + header(self::$env["SERVER_PROTOCOL"]." ".$status); + $headers = array_merge(self::$headers, $headers); + foreach( $headers as $key => $value ) { + header($key.": ".$value); + } + + // output any buffered content from middlewares + $buffer = ob_get_contents(); + ob_end_clean(); + if ( !empty($buffer) ) { + echo $buffer; + } + + // output body + if ( is_array($body) ) { + echo implode("", $body); + } else { + echo $body; + } + } + + + private static function require_file ($file = null) { + if ( $file != null && is_file($file) ) { + require($file); + } + } + +} + +?> \ No newline at end of file diff --git a/public/.htaccess b/public/.htaccess new file mode 100644 index 0000000..49fa542 --- /dev/null +++ b/public/.htaccess @@ -0,0 +1,10 @@ +RewriteEngine On + +RewriteRule ^.*\.svn.*$ /404 + +RewriteRule ^$ index.html [QSA] +RewriteCond %{REQUEST_FILENAME} !-f +RewriteCond %{REQUEST_FILENAME} !-d +RewriteCond %{REQUEST_FILENAME} !favicon.ico +RewriteCond %{REQUEST_FILENAME} !robots.txt +RewriteRule ^(.*)$ /rackup.php [QSA,L] diff --git a/public/rackup.php b/public/rackup.php new file mode 100644 index 0000000..817f18a --- /dev/null +++ b/public/rackup.php @@ -0,0 +1,16 @@ + \ No newline at end of file