Rack::run PHP Method

run() public static method

public static run ( )
    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);
        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;
        }
    }

Usage Example

Esempio n. 1
0
<?php

define("ROOT", dirname(dirname(__FILE__)));
require ROOT . "/lib/rack.php";
// add some middlewares
Rack::add("Format", ROOT . "/app/format.php");
Rack::add("App", ROOT . "/app/app.php");
// insert the Api middleware before App
Rack::insert_before("App", "Api", ROOT . "/app/api.php");
Rack::run();