Framework base

This commit is contained in:
Abdussamed 2023-04-11 22:59:27 +03:00
parent 9e5f941436
commit fd6de43a48
8 changed files with 2311 additions and 525 deletions

View File

@ -6,3 +6,7 @@
include_once "Helpers.php"; include_once "Helpers.php";
include_once "Blade.php"; include_once "Blade.php";
include_once "Route.php"; include_once "Route.php";
include_once "Medoo.php";
//include_once "DatabaseORM.php";
//include_once "DatabaseMysqlEngine.php";

View File

@ -1,111 +0,0 @@
<?php
include_once "Helpers.php";
class DatabaseConnection
{
public $client = "mysql";
public $user = "root";
public $pass = "";
public $host = "127.0.0.1";
public $port = 3306;
public $database = "";
public $charset = "utf8mb4";
public $file = "";
public $connectionString = null;
public static $db = null;
public $ready = false;
public function __construct(
$client,
$user,
$pass,
$host = "127.0.0.1",
$port = "3306",
$database = "",
$charset = ""
)
{
$this->client = $client;
$this->user = $user;
$this->pass = $pass;
$this->host = $host;
$this->port = $port;
$this->database = $database;
$this->charset = $charset;
}
public function connect()
{
if($this->db == null)
{
$this->db = new PDO(
$this->createConnectionString(),
$this->user,
$this->pass,
array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
)
);
$this->ready = true;
}
}
public function assertConnection()
{
if($this->db == null)
{
$this->connect();
if($this->ready == false)
{
throw new Exception("Connection not established");
}
};
}
public function createConnectionString()
{
if($this->connectionString)
{
return $this->connectionString;
}else{
$str = new tstring($this->client);
return $str
->concat(":", "host=", $this->host, ";")
->concat("port=", $this->port, ";")
->concat("database=", $this->database, ";")
->concat("host=", $this->host, ";");
}
}
public function exec($sql)
{
$this->assertConnection();
$effected = $this->db->exec($sql);
if($effected === false)
{
throw new Exception($this->db->errorInfo());
}else{
return $effected;
}
}
public function lastinsertid()
{
$this->assertConnection();
return $this->db->lastinsertid();
}
public function prepare($sql, $params = [])
{
$this->assertConnection();
$prepare = $this->db->prepare($sql);
if($prepare->execute($params))
{
return $prepare;
}else{
throw new Exception($this->db->errorInfo());
}
}
public function fetch($sql, $params = [])
{
$this->assertConnection();
return $this->prepare($sql, $params)->fetch(PDO::FETCH_ASSOC);
}
public function fetchAll($sql, $params = [])
{
$this->assertConnection();
return $this->prepare($sql, $params)->fetchAll(PDO::FETCH_ASSOC);
}
};

View File

@ -1,37 +0,0 @@
<?php
include_once "Helpers.php";
include_once "DatabaseORM.php";
include_once "DatabaseConnection.php";
class MySQLEngine extends DatabaseConnection
{
public function query(ORM $orm)
{
return $this->exec();
}
public function migrate(ORMMigrate $orm)
{
$sql = [];
$sql[] = "CREATE TABLE IF NOT EXISTS ";
$sql[] = $orm->table;
$sql[] = " ( ";
$columns = [];
foreach ($orm->columns as $name => $value) {
$type = $value[0];
$propety = $value[1] ?? "";
$columns[] = "$name $type $propety";
}
$sql[] = implode(", ",$columns);
$sql[] = ") charset=" . $orm->charset;
}
}
ORM::$defaultConnection = new MySQLEngine("mysql","root","");
ORM::migrate(function($table){
$table->id("int","auto_increment primary key");
$table->name("text");
$table->value("text");
});

View File

@ -1,350 +0,0 @@
<?php
include_once "Reflection.php";
include_once "Helpers.php";
function name($name)
{
return (
new tstring($name)
)->wrap(".", ".", function($item){
return "`" . $item->match("/^[\"\']?(.*?)[\"\']?$/")[1] . "`";
});
}
function isString($data)
{
return gettype($data) == "string" || $data instanceof tstring;
}
class ORM
{
public $mode = "select";
public $database = "";
public $table = "";
public $select = [];
public $where = [];
public $groupBy = [];
public $having = [];
public $orderby = [];
public $limit = false;
public $union = [];
public $set = [];
public $join = [];
public $alter = [];
public $israw = false;
public $raw = "";
public static $defaultConnection = null;
public $connection;
public function __construct()
{
$this->connection = ORM::$defaultConnection;
}
public static function name($args)
{
return name($args);
}
public static function raw($sql)
{
$orm = new static();
$orm->raw = $sql;
$orm->israw = true;
return $orm;
}
public static function migrate($class)
{
$orm = new static();
$migration = new ORMMigrate();
$class($migration);
$orm->connection->migrate($migration);
}
public function from($orm)
{
foreach (get_class_vars($orm) as $key => $value) {
$this->{$key} = $value;
}
}
public function table($table)
{
$this->table = $table;
return $this;
}
public function database($database)
{
$this->database = $database;
return $this;
}
public function select(...$args)
{
$this->select = array_merge($this->select, array_map(
function($item){
return new tstring($item);
},
$args
));
return $this;
}
public function update(...$arguments)
{
$this->mode = "update";
$this->set($arguments);
}
public function delete()
{
$this->mode = "delete";
}
public function insert(...$arguments)
{
$this->mode = "insert";
$this->set($arguments);
}
public function renameTable($from, $to)
{
$this->mode = "alter";
$this->alter[] = ["renametable", $from, $to];
}
public function renameColumn($from, $to)
{
$this->mode = "alter";
$this->alter[] = ["renamecolumn", $from, $to];
}
public function modifyColumn($from, $to)
{
$this->mode = "alter";
$this->alter[] = ["modifyColumn", $from, $to];
}
public function addColumn($from)
{
$this->mode = "alter";
$this->alter[] = ["addColumn", $from, $to];
}
public function removeColumn($from)
{
$this->mode = "alter";
$this->alter[] = ["addColumn", $from, $to];
}
public function union(ORM $orm)
{
if($orm->mode == "select")
{
$this->union[] = $orm;
}
}
public function limit($offset = 0, $length = false)
{
$this->limit = [
"offset" => $offset,
"length" => $length
];
}
public function addWhere($statement, $name, $operator, $value)
{
$this->where = array_merge(
$this->where,
[
$statement,
name($name),
$operator,
$value
]
);
}
public function where(...$statements)
{
if(
(count($statements) == 3 && isString($statements[0]) && isString($statements[1]))
)
{
$this->addWhere("and",...$statements);
}
else if(
(count($statements) == 2 && isString($statements[0]))
)
{
$this->addWhere("and",$statements[0],"=",$statements[1]);
}
else if(count($statements) == 1 && gettype($statements[0]) == "array")
{
foreach ($statements[0] as $key => $value) {
if(is_numeric($key))
{
$this->addWhere("and",...$value);
}else{
$this->addWhere("and",$key,"=",$value);
}
}
}
else if(count($statements) == 1 && gettype($statements[0]) == "string")
{
$this->addWhere("and",$statements[0],"IS","null");
};
return $this;
}
public function whereNot(...$statements)
{
if(
(count($statements) == 3 && isString($statements[0]) && isString($statements[1]))
)
{
$this->addWhere("and",...$statements);
}
else if(
(count($statements) == 2 && isString($statements[0]))
)
{
$this->addWhere("and",$statements[0],"!=",$statements[1]);
}
else if(count($statements) == 1 && gettype($statements[0]) == "array")
{
foreach ($statements[0] as $key => $value) {
if(is_numeric($key))
{
$this->addWhere("and",...$value);
}else{
$this->addWhere("and",$key,"!=",$value);
}
}
}
else if(count($statements) == 1 && gettype($statements[0]) == "string")
{
$this->addWhere("and",$statements[0],"IS","null");
};
return $this;
}
public function orWhere(...$statements)
{
if(count($statements) == 1 && gettype($statements[0]) == "array")
{
foreach ($statements[0] as $key => $value) {
if(is_numeric($key))
{
$this->addWhere("or",...$value);
}else{
$this->addWhere("or",$key,"=",$value);
}
}
}
else if(count($statements) == 1 && gettype($statements[0]) == "string")
{
$this->addWhere("or",$statements[0],"IS","null");
}
else if(count($statements) == 3 && gettype($statements[0]) == "string" && gettype($statements[1]) == "string")
{
$this->addWhere("or",...$statements);
}
return $this;
}
public function orWhereNot(...$statements)
{
if(count($statements) == 1 && gettype($statements[0]) == "array")
{
foreach ($statements[0] as $key => $value) {
if(is_numeric($key))
{
$this->addWhere("or",...$value);
}else{
$this->addWhere("or",$key,"!=",$value);
}
}
}
else if(count($statements) == 1 && gettype($statements[0]) == "string")
{
$this->addWhere("or",$statements[0],"IS NOT","null");
}
else if(count($statements) == 3 && gettype($statements[0]) == "string" && gettype($statements[1]) == "string")
{
$this->addWhere("or",...$statements);
}
return $this;
}
public function whereIn($name, $arr)
{
$this->where(name($name), "IN", '('. implode(', ',$arr) .')');
return $this;
}
public function orWhereIn($name, $arr)
{
$this->orwhere(name($name), "IN", '('. implode(', ',$arr) .')');
return $this;
}
public function whereNotIn($name, $arr)
{
$this->where(name($name), "NOT IN", '('. implode(', ',$arr) .')');
return $this;
}
public function orWhereNotIn($name, $arr)
{
$this->orwhere(name($name), "NOT IN", '('. implode(', ',$arr) .')');
return $this;
}
public function between($name, $state1, $state2)
{
$this->orwhere(name($name), "BETWEEN", $state1 . " AND " . $state2);
return $this;
}
public function groupBy(...$name)
{
$this->groupBy = array_merge(
$this->groupBy,
...$name
);
return $this;
}
public function having(...$name)
{
$this->having = array_merge(
$this->having,
...$name
);
return $this;
}
public function orderBy($name,$mode = "ASC")
{
$this->orderBy[] = [$name,$mode];
return $this;
}
public function set($name,$value = false)
{
if(is_array($name))
{
foreach ($name as $key => $value) {
$this->set[] = [
name($key),
$value
];
}
}else{
$this->set[] = [
name($name),
$value
];
}
return $this;
}
};
class ORMMigrate
{
use ReflectionHook;
public $table = "";
public $charset = "";
public $columns = [];
public $modifycolumns = [];
public $removecolumns = [];
public function table($name)
{
$this->table = name($name);
}
public function charset($charset)
{
$this->charset = $charset;
}
public function callable($name, $type = "int", $property = "")
{
$this->columns[name($name).""] = [$type,$property];
}
public function setAttribute($name, $value)
{
$this->modifycolumns[name($name).""] = $value;
}
public function removeAttribute($name)
{
$this->removecolumns[] = name($name)."";
}
};

View File

@ -280,3 +280,16 @@
highlight_string("<?=\n\n" . var_export($data, true) . "\n\n?>"); highlight_string("<?=\n\n" . var_export($data, true) . "\n\n?>");
die(); die();
} }
function includeAllSubFiles($path)
{
$files = scandir($path);
foreach ($files as $file) {
if(in_array($file,['.','..']))
{
continue;
}
$filepath = realpath("$path/$file");
require_once $filepath;
}
}

2239
Core/Medoo.php Normal file

File diff suppressed because it is too large Load Diff

View File

@ -43,7 +43,7 @@
RequestHeader::set("Content-Type","application/json; charset=$encoding"); RequestHeader::set("Content-Type","application/json; charset=$encoding");
echo $content; echo $content;
} }
public static function HTML($content, $encoding = "utf8") public static function html($content, $encoding = "utf8")
{ {
RequestHeader::set("Content-Length",strlen($content)); RequestHeader::set("Content-Length",strlen($content));
RequestHeader::set("Content-Type","text/html; charset=$encoding"); RequestHeader::set("Content-Type","text/html; charset=$encoding");
@ -53,6 +53,10 @@
{ {
http_response_code($code); http_response_code($code);
} }
public static function view(...$args)
{
Response::html(view(...$args));
}
public static function send($content) public static function send($content)
{ {
switch(gettype($content)) switch(gettype($content))

View File

@ -69,6 +69,7 @@
class Route class Route
{ {
public static $routes = []; public static $routes = [];
public static $errors = [];
public static $publics = []; public static $publics = [];
public static $current = null; public static $current = null;
public static function post($path, $callback) public static function post($path, $callback)
@ -92,6 +93,14 @@
Route::$routes[] = $route; Route::$routes[] = $route;
return $route; return $route;
} }
public static function error($method, $code, $callback)
{
if(!isset(Route::$errors[$code]))
{
Route::$errors[$code] = [];
}
Route::$errors[$code][$method] = $callback;
}
public static function get($path, $callback) public static function get($path, $callback)
{ {
$magic = magicRoute($path); $magic = magicRoute($path);
@ -143,41 +152,56 @@
Route::$routes[] = $route; Route::$routes[] = $route;
return $route; return $route;
} }
public static function ThrowErrorCode($code)
{
$method = Request::$method;
if(isset(Route::$errors[$code][$method]))
{
$callback = Route::$errors[$code][$method];
Route::$errors[$code][$method]();
return;
};
Response::Code($code);
}
public static function CheckCurrent() public static function CheckCurrent()
{ {
if(Route::$current == null) try{
if(Route::$current == null)
{
foreach (Route::$routes as $route) {
$route->clear();
if($route->Run())
{
Route::$current = $route;
return $route;
}
};
foreach (Route::$publics as $route) {
$route->clear();
if($route->Run())
{
Route::$current = $route;
return $route;
}
};
Route::ThrowErrorCode(404);
return false;
};
}
catch(Exception $e)
{ {
foreach (Route::$routes as $route) { Route::ThrowErrorCode(500);
$route->clear(); }
if($route->Run())
{
Route::$current = $route;
return $route;
}
};
foreach (Route::$publics as $route) {
$route->clear();
if($route->Run())
{
Route::$current = $route;
return $route;
}
};
return false;
};
return Route::$current; return Route::$current;
} }
public static function Execute() public static function Execute()
{ {
$route = Route::CheckCurrent(); $route = Route::CheckCurrent();
if($route === false) if($route !== false)
{ {
Response::Code(404); $route->call();
exit;
}; };
$route->call();
} }
public static function from(...$args) public static function from(...$args)
@ -254,7 +278,7 @@
{ {
$filename = $url->slice(strlen($path)); // remove first / char $filename = $url->slice(strlen($path)); // remove first / char
$rpath = realpath($this->src . $filename); $rpath = realpath($this->src . $filename);
if(file_exists($rpath)) if(file_exists($rpath) && is_file($rpath))
{ {
$this->callback = $rpath; $this->callback = $rpath;
return true; return true;