microphp/Core/Request.php

120 lines
3.2 KiB
PHP

<?php
include_once "Reflection.php";
include_once "RequestHeader.php";
include_once "RequestFile.php";
include_once "Request.php";
include_once "Session.php";
class Request
{
use ReflectionHook;
public static $header;
public static $cookie;
public static $method;
public static $session;
public static $contentType;
public static $file;
public static $data = [];
public static $query = [];
public static $ready = false;
function __construct()
{
if(!Request::$ready)
{
Request::$header = new RequestHeader();
Request::$cookie = new RequestCookie();
Request::$file = new RequestFile();
Request::$session = new Session();
Request::$method = strtolower($_SERVER["REQUEST_METHOD"]);
Request::$contentType = strtolower($_SERVER["CONTENT_TYPE"] ?? "");
Request::$ready = true;
$this->mutateRequest();
}
}
function getJSONRequest()
{
return json_decode(file_get_contents('php://input'), true);
}
function mutateRequest()
{
if(Request::$contentType == "application/json")
{
Request::$data = $this->getJSONRequest();
}
else
{
if(Request::$method == "post")
{
Request::$data = $_POST;
}
Request::$query = $_GET;
};
}
function contentType()
{
return Request::$contentType;
}
function method()
{
return Request::$method;
}
function get($name)
{
return Request::$query[$name] ?? null;
}
function has($name)
{
return $this->input($name) !== null;
}
function post($name)
{
return Request::$data[$name] ?? null;
}
function input($name)
{
return Request::staticGet($name) ?? Request::staticPost($name) ?? null;
}
function staticContentType()
{
return $this->contentType();
}
function staticMethod()
{
return $this->method();
}
function staticGet($name)
{
return $this->get($name);
}
function staticHeaders()
{
return Request::$headers;
}
function staticFile()
{
return Request::$file;
}
function staticSession()
{
return Request::$session;
}
function staticCookie()
{
return Request::$cookie;
}
function staticPost($name)
{
return $this->post($name);
}
function staticInput($name)
{
return $this->input($name);
}
function staticHas($name)
{
return $this->has($name);
}
function getAttribute($name)
{
return Request::staticInput($name);
}
};