<?php
namespace EasyChat\Library;








class RequestContext {
    public static function &get() {
        return self::bucket('_GET');
    }

    public static function &post() {
        return self::bucket('_POST');
    }

    public static function &request() {
        return self::bucket('_REQUEST');
    }

    public static function &server() {
        return self::bucket('_SERVER');
    }

    public static function &cookie() {
        return self::bucket('_COOKIE');
    }

    public static function &files() {
        return self::bucket('_FILES');
    }

    public static function &session() {
        return self::bucket('_SESSION');
    }

    public static function replaceSession(array $values = []) {
        $GLOBALS['_SESSION'] = $values;
    }

    public static function method($default = 'GET') {
        $server = self::server();
        $method = isset($server['REQUEST_METHOD']) ? strtoupper((string)$server['REQUEST_METHOD']) : strtoupper((string)$default);
        return $method !== '' ? $method : strtoupper((string)$default);
    }

    public static function bootstrapServerDefaults(array $defaults) {
        $server =& self::server();
        foreach ($defaults as $key => $value) {
            if (!isset($server[$key]) || $server[$key] === '') {
                $server[$key] = $value;
            }
        }
    }

    private static function &bucket($name) {
        static $hydrated = [];

        if (!isset($GLOBALS[$name]) || !is_array($GLOBALS[$name])) {
            $GLOBALS[$name] = self::nativeBucket($name);
            $hydrated[$name] = true;
        } elseif ($name !== '_SESSION' && empty($GLOBALS[$name]) && empty($hydrated[$name])) {
            $native = self::nativeBucket($name);
            if (!empty($native)) {
                $GLOBALS[$name] = $native;
            }
            $hydrated[$name] = true;
        }

        return $GLOBALS[$name];
    }

    private static function nativeBucket($name) {
         
         
         
         
         
        switch ($name) {
            case '_GET':
                return isset($_GET) && is_array($_GET) ? $_GET : [];
            case '_POST':
                return isset($_POST) && is_array($_POST) ? $_POST : [];
            case '_REQUEST':
                return isset($_REQUEST) && is_array($_REQUEST) ? $_REQUEST : [];
            case '_SERVER':
                return isset($_SERVER) && is_array($_SERVER) ? $_SERVER : [];
            case '_COOKIE':
                return isset($_COOKIE) && is_array($_COOKIE) ? $_COOKIE : [];
            case '_FILES':
                return isset($_FILES) && is_array($_FILES) ? $_FILES : [];
            case '_SESSION':
                return isset($_SESSION) && is_array($_SESSION) ? $_SESSION : [];
            default:
                return [];
        }
    }
}
