First commit

This commit is contained in:
Abdussamed 2023-04-02 10:00:20 +03:00
parent db052409c2
commit 8e17db2908
9 changed files with 702 additions and 0 deletions

8
Core/Core.php Normal file
View File

@ -0,0 +1,8 @@
<?php
include_once "Request.php";
include_once "Response.php";
include_once "Session.php";
define("request", new Request(), false);
define("response", new Response(), false);
define("session", new Session(), false);

161
Core/Reflection.php Normal file
View File

@ -0,0 +1,161 @@
<?php
trait ReflectionHook
{
public static function __callStatic($name, $arguments): mixed
{
$formarly = "static" . ucfirst($name);
if(method_exists(__CLASS__, $formarly))
{
return (new static)->{$formarly}(...$arguments);
}
else if(method_exists(__CLASS__, "call$name"))
{
return (new static)->{"call$name"}($name, ...$arguments);
}
else
{
if(method_exists(__CLASS__, $name))
{
var_dump($callback);
exit;
}
try{
return static::{$name}(...$arguments);
}catch(Exception $e){
}
return null;
}
}
public function __call($name, $arguments)
{
$formarlyA = "call" . ucfirst($name);
$formarlyB = "static" . ucfirst($name);
if(method_exists($this, $formarlyA))
{
return $this->{$formarlyA}(...$arguments);
}else if(method_exists(__CLASS__, $formarlyB))
{
return (new static)->{$formarlyB}(...$arguments);
}
else if(method_exists(__CLASS__, "call$name"))
{
return (new static)->{"call$name"}($name, ...$arguments);
}
}
public function __get($name)
{
$formarly = ucfirst($name);
if(method_exists(__CLASS__, "get" . $formarly . "Attribute"))
{
return $this->{$formarly};
}
else if(method_exists(__CLASS__, "getAttribute"))
{
return $this->getAttribute($name);
}
else if(property_exists(__CLASS__, "_attributes"))
{
return $this->_attributes[$name] ?? null;
}
}
public function __set($name, $value)
{
$formarly = ucfirst($name);
if(method_exists($this, "get" . $formarly . "Attribute"))
{
$this->{$formarly} = $value;
}
else if(method_exists($this, "setAttribute"))
{
return $this->setAttribute($name, $value);
}
else if(property_exists($this, "_attributes") && isset($this->_attributes[$name]))
{
$this->_attributes[$name] = $value;
}
}
public function __isset($name)
{
$formarly = ucfirst($name);
if(method_exists($this, "get" . $formarly . "Attribute"))
{
return true;
}
else
{
return isset($this->_attributes[$name]);
}
}
public function __unset($name)
{
$formarly = ucfirst($name);
if(method_exists($this, "set" . $formarly . "Attribute"))
{
$this->{"set" . $formarly . "Attribute"}();
}
else if(method_exists($this, "setAttribute"))
{
return $this->setAttribute($name, null);
}
else if(method_exists($this, "remoteAttribute"))
{
return $this->remoteAttribute($name);
}
else if(property_exists($this, "_attributes") && isset($this->_attributes[$name]))
{
unset($this->_attributes[$name]);
}
}
public function __invoke(...$args)
{
if(method_exists($this, "reflectionCall"))
{
return $this->reflectionCall(...$args);
}
else
{
if(property_exists(__CLASS__, "reflectionCallException"))
{
if(!(new static)->reflectionCallException)
{
return;
}
}
throw new Exception("\"".__CLASS__."()\" not callable");
}
}
public function __toString()
{
if(method_exists($this, "toString"))
{
return $this->toString();
}
else
{
if(property_exists(__CLASS__, "reflectionCallException"))
{
if(!(new static)->reflectionCallException)
{
return "";
}
}
throw new Exception("\"".__CLASS__."()\" not callable");
}
}
public function __serialize()
{
if(method_exists($this, "getProperties"))
{
return $this->getProperties();
}
}
public function __unserialize($datas)
{
if(method_exists($this, "setProperties"))
{
$this->setProperties($datas);
}
}
}

120
Core/Request.php Normal file
View File

@ -0,0 +1,120 @@
<?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);
}
};

28
Core/RequestCookie.php Normal file
View File

@ -0,0 +1,28 @@
<?php
include_once "Reflection.php";
class RequestCookie {
use ReflectionHook;
static $_attributes = [];
static $readyHeaders = false;
function __construct()
{
if(!RequestCookie::$readyHeaders)
{
RequestCookie::$_attributes = (array) $_COOKIE;
};
}
function setAttribute($name, $value, $time = '+1 years')
{
$formarly = strtolower($name);
setcookie($name, $value, strtotime( $time ));
}
function getAttribute($name)
{
$formarly = strtolower($name);
return RequestCookie::$_attributes[$formarly];
}
function removeAttribute($name)
{
$this->setAttribute($name, 0, "-1 days");
}
};

148
Core/RequestFile.php Normal file
View File

@ -0,0 +1,148 @@
<?php
include_once "Reflection.php";
class RequestFile
{
use ReflectionHook;
public static $ready = false;
public static $files = [];
function __construct()
{
if(!RequestFile::$ready)
{
RequestFile::$files = $this->files();
RequestFile::$ready = true;
};
}
public function getAttribute($name)
{
return $this->getFile($name);
}
public function hasFile($name)
{
return RequestFile::$files->{$name} ? $this->count($name) : false;
}
public function getFile($name)
{
return $this->hasFile($name) ? RequestFile::$files->{$name} : null;
}
public function count($name)
{
return count(RequestFile::$files->{$name});
}
public function save($field, $directory, $filename = false, $index = 0)
{
$file = $this->getFile($field)[$index];
if($filename == false)
{
$filename = explode('.', $file->name);
if(count($filename) == 1)
{
$ext = ".bin";
}
else
{
$ext = end($filename);
};
$filename = bin2hex(random_bytes(16)) . "." . $ext;
}
if(move_uploaded_file($file->tmp, $directory . "/" . $filename))
{
return $filename;
}
else
{
return false;
}
}
public function saveAll($field, $directory, $filename = false, $max = -1)
{
$files = $this->getFile($field);
$current = 0;
foreach ($files as $file)
{
$file->save($directory, $filename);
$current++;
if($current == $max && $max != -1)
{
break;
}
}
}
public function files()
{
$arg = new stdClass;
foreach ($_FILES as $name => $file)
{
$arg->{$name} = [];
for($i = 0; $i < count($file["name"]); $i++)
{
$arg->{$name}[] = new UploadedFile(
$i,
$this,
$name,
$file["name"][$i],
$file["full_path"][$i],
$file["type"][$i],
$file["tmp_name"][$i],
$file["error"][$i],
$file["size"][$i]
);
}
};
return $arg;
}
}
class UploadedFile
{
public $name;
public $path;
public $mime;
public $tmp;
public $error;
public $size;
public $index;
public $requestFile;
public $field;
public function __construct(
$index,
$requestFile,
$field,
$name,
$path,
$mime,
$tmp,
$error,
$size
)
{
$this->name = $name;
$this->path = $path;
$this->mime = $mime;
$this->tmp = $tmp;
$this->error = $error;
$this->size = $size;
$this->index = $index;
$this->requestFile = $requestFile;
$this->field = $field;
}
//save($field, $directory, $filename = false, $index = 0)
public function save($directory, $filename = false)
{
$this->requestFile->save(
$this->field,
$directory,
$filename,
$this->index
);
}
public function saveAll($directory, $filename = false, $max = -1)
{
$this->requestFile->saveAll(
$this->field,
$directory,
$filename,
$max
);
}
}

39
Core/RequestHeader.php Normal file
View File

@ -0,0 +1,39 @@
<?php
include_once "Reflection.php";
class RequestHeader {
use ReflectionHook;
static $_attributes = [];
static $readyHeaders = false;
function __construct()
{
if(!RequestHeader::$readyHeaders)
{
foreach ($_SERVER as $name => $value)
{
if (substr($name, 0, 5) == 'HTTP_')
{
RequestHeader::$_attributes[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
}
}
};
}
public static function get($name)
{
return (new static)->{$name};
}
public static function set($name, $value)
{
$requestHeader = new static;
$requestHeader->{$name} = $value;
}
function setAttribute($name, $value)
{
$formarly = ucfirst(strtolower($name));
header("$formarly: $value");
}
function getAttribute($name)
{
$formarly = ucfirst(strtolower($name));
return RequestHeader::$_attributes[$formarly] ?? null;
}
};

75
Core/Response.php Normal file
View File

@ -0,0 +1,75 @@
<?php
include_once "RequestHeader.php";
class Response
{
public static function File($path, $mime = "auto")
{
if($mime == "auto")
{
$mime = mime_content_type($path);
};
readfile($path);
}
public static function Download($path, $mimetrue = false)
{
if($mimetrue == false)
{
RequestHeader::set("Content-Type","application/octet-stream");
}
RequestHeader::set("Content-Transfer-Encoding","Binary");
RequestHeader::set("Content-Disposition",'attachment; filename="'.basename($path).'"');
RequestHeader::set("Content-Length",filesize($path));
flush();
readfile($path);
}
public static function Text($content, $encoding = "utf8")
{
RequestHeader::set("Content-Length",strlen($content));
RequestHeader::set("Content-Type","text/plain; charset=$encoding");
echo $content;
}
public static function Json($content, $encoding = "utf8")
{
$content = json_encode($content);
RequestHeader::set("Content-Length",strlen($content));
RequestHeader::set("Content-Type","application/json; charset=$encoding");
echo $content;
}
public static function HTML($content, $encoding = "utf8")
{
RequestHeader::set("Content-Length",strlen($content));
RequestHeader::set("Content-Type","text/html; charset=$encoding");
echo $content;
}
public static function Code($code = 200)
{
http_response_code($code);
}
public static function send($content)
{
switch(gettype($content))
{
case "boolean":
case "integer":
case "double":
case "float":
case "string":{
Response::Text($content);
break;
}
case "object":
case "array":{
Response::Json($content);
break;
}
default:{
throw new Exception("unknown type");
}
}
}
public static function Redirect($path, $code = 307)
{
http_response_code($code);
header("location: $path");
}
};

121
Core/Session.php Normal file
View File

@ -0,0 +1,121 @@
<?php
include_once "Reflection.php";
include_once "RequestCookie.php";
class Session {
use ReflectionHook;
static $active = false;
static $lifetime = '+7 days';
static $cookiename = 'session';
static $ready = false;
function __contruct()
{
session_set_cookie_params(strtotime(static::$lifetime));
session_name(static::$cookiename);
Session::$ready = true;
}
function staticStart()
{
session_start();
}
function staticFlush()
{
session_commit();
}
function staticDestroy()
{
session_destroy();
}
function staticReset()
{
session_reset();
}
function staticSet($name, $value)
{
$_SESSION[$name] = $value;
}
function staticGet($name)
{
return $_SESSION[$name];
}
function staticDelete($name)
{
unset($_SESSION[$name]);
}
function staticHas($name)
{
return isset($_SESSION[$name]);
}
function staticAdd($name, $item)
{
if(!Session::has($name))
{
$_SESSION[$name] = [];
}
$_SESSION[$name][] = $item;
}
function staticPop($name, $item)
{
if(!$this->has($name))
{
$_SESSION[$name] = [];
}
return array_pop($_SESSION[$name]);
}
function staticShift($name, $item)
{
if(!!$this->has($name))
{
$_SESSION[$name] = [];
}
return array_shift($_SESSION[$name]);
}
function staticUnshift($name, $item)
{
if(!!$this->has($name))
{
$_SESSION[$name] = [];
}
$_SESSION[$name] = [
$item,
...$_SESSION[$name]
];
}
function staticMerge($name, $item)
{
if(!!$this->has($name))
{
$_SESSION[$name] = [];
}
$_SESSION[$name] = [
...$_SESSION[$name],
...$item
];
}
function staticPath($path)
{
session_save_path($path);
}
function staticStatus()
{
switch(session_status())
{
case PHP_SESSION_ACTIVE:{
return Session::$active = true;
}
default:{
return Session::$active = false;
}
}
}
function staticInit()
{
if(!Session::$ready)
{
$this->__contruct();
}
if(!$this->staticStatus())
{
$this->staticStart();
}
}
};

2
index.php Normal file
View File

@ -0,0 +1,2 @@
<?php
include_once "Core/Core.php";