Route public / mimetypes

This commit is contained in:
Abdussamed 2023-04-11 00:30:15 +03:00
parent d4cf0fd331
commit 9e5f941436
24 changed files with 205 additions and 37 deletions

0
Core/Blade.php Normal file → Executable file
View File

0
Core/BladeOne.php Normal file → Executable file
View File

0
Core/BladeOneCache.php Normal file → Executable file
View File

4
Core/Core.php Normal file → Executable file
View File

@ -1,10 +1,8 @@
<?php
include_once "Request.php";
include_once "Response.php";
include_once "mimes.php";
include_once "Session.php";
include_once "Helpers.php";
include_once "DatabaseConnection.php";
include_once "DatabaseORM.php";
include_once "DatabaseMySQLEngine.php";
include_once "Blade.php";
include_once "Route.php";

0
Core/DatabaseConnection.php Normal file → Executable file
View File

0
Core/DatabaseMysqlEngine.php Normal file → Executable file
View File

0
Core/DatabaseORM.php Normal file → Executable file
View File

0
Core/Helpers.php Normal file → Executable file
View File

0
Core/Reflection.php Normal file → Executable file
View File

1
Core/Request.php Normal file → Executable file
View File

@ -2,7 +2,6 @@
include_once "Reflection.php";
include_once "RequestHeader.php";
include_once "RequestFile.php";
include_once "Request.php";
include_once "Session.php";
class Request
{

0
Core/RequestCookie.php Normal file → Executable file
View File

0
Core/RequestFile.php Normal file → Executable file
View File

0
Core/RequestHeader.php Normal file → Executable file
View File

6
Core/Response.php Normal file → Executable file
View File

@ -2,12 +2,16 @@
include_once "RequestHeader.php";
class Response
{
public static $mimes;
public static function File($path, $mime = "auto")
{
if($mime == "auto")
{
$mime = mime_content_type($path);
$exploder = explode(".", $path);
$ext = end($exploder);
$mime = Response::$mimes[$ext] ?? "application/octet-stream";
};
RequestHeader::set("Content-Type",$mime);
$fp = fopen($path, 'rb');
fpassthru($fp);
fclose($fp);

90
Core/Route.php Normal file → Executable file
View File

@ -69,6 +69,7 @@
class Route
{
public static $routes = [];
public static $publics = [];
public static $current = null;
public static function post($path, $callback)
{
@ -112,6 +113,15 @@
Route::$routes[] = $route;
return $route;
}
public static function public($route, $storagepath)
{
Route::$publics[] = Route::from([
"type" => "public",
"path" => preg_quote($route),
"src" => $storagepath,
"method" => "get"
]);
}
public static function any($path, $callback)
{
$magic = magicRoute($path);
@ -146,6 +156,14 @@
return $route;
}
};
foreach (Route::$publics as $route) {
$route->clear();
if($route->Run())
{
Route::$current = $route;
return $route;
}
};
return false;
};
return Route::$current;
@ -157,6 +175,7 @@
if($route === false)
{
Response::Code(404);
exit;
};
$route->call();
}
@ -168,6 +187,7 @@
public string $type;
public string $path;
public string $src;
public $callback;
public string $method;
@ -185,8 +205,17 @@
}
public function call(...$args)
{
$callback = $this->callback;
$callback(...$args);
switch ($this->type) {
case 'magic':
case 'text':
$callback = $this->callback;
$callback(...$args);
break;
case 'public':
$callback = $this->callback;
Response::File($callback);
break;
}
}
public function Run()
{
@ -197,27 +226,46 @@
return false;
}
$url = parse_url($requestUrl, PHP_URL_PATH);
if($this->type == "text")
switch($this->type)
{
if($url == $this->path)
{
return true;
}else{
return false;
case "text":{
if($url == $this->path)
{
return true;
}else{
return false;
}
break;
}
case "magic":{
if($t === false)
{
return false;
}else{
$this->match = $t;
return true;
}
break;
}
case "public":{
$path = tstring::from($this->path);
$url = tstring::from($url);
if($url->startsWith($path))
{
$filename = $url->slice(strlen($path)); // remove first / char
$rpath = realpath($this->src . $filename);
if(file_exists($rpath))
{
$this->callback = $rpath;
return true;
}else{
return false;
}
}else{
return false;
}
break;
}
};
$t = validateMagicRoute(
$this->path,
$url
);
if($t === false)
{
return false;
}else{
$this->match = $t;
return true;
}
}
}

0
Core/Session.php Normal file → Executable file
View File

90
Core/mimes.php Normal file
View File

@ -0,0 +1,90 @@
<?php
Response::$mimes = [
"htm" => "text/html",
"html" => "text/html",
"shtml" => "text/html",
"css" => "text/css",
"xml" => "text/xml",
"gif" => "image/gif",
"jpeg" => "image/jpeg",
"jpg" => "image/jpeg",
"js" => "application/javascript",
"atom" => "application/atom+xml",
"rss" => "application/rss+xml",
"mml" => "text/mathml",
"txt" => "text/plain",
"jad" => "text/vnd.sun.j2me.app-descriptor",
"wml" => "text/vnd.wap.wml",
"htc" => "text/x-component",
"avif" => "image/avif",
"png" => "image/png",
"svgz" => "image/svg+xml",
"svg" => "image/svg+xml",
"tiff" => "image/tiff",
"tif" => "image/tiff",
"wbmp" => "image/vnd.wap.wbmp",
"webp" => "image/webp",
"ico" => "image/x-icon",
"jng" => "image/x-jng",
"bmp" => "image/x-ms-bmp",
"woff" => "font/woff",
"woff2" => "font/woff2",
"json" => "application/json",
"hqx" => "application/mac-binhex40",
"doc" => "application/msword",
"pdf" => "application/pdf",
"rtf" => "application/rtf",
"m3u8" => "application/vnd.apple.mpegurl",
"kml" => "application/vnd.google-earth.kml+xml",
"kmz" => "application/vnd.google-earth.kmz",
"xls" => "application/vnd.ms-excel",
"eot" => "application/vnd.ms-fontobject",
"ppt" => "application/vnd.ms-powerpoint",
"odg" => "application/vnd.oasis.opendocument.graphics",
"odp" => "application/vnd.oasis.opendocument.presentation",
"ods" => "application/vnd.oasis.opendocument.spreadsheet",
"odt" => "application/vnd.oasis.opendocument.text",
"pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation",
"xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"wmlc" => "application/vnd.wap.wmlc",
"wasm" => "application/wasm",
"7z" => "application/x-7z-compressed",
"cco" => "application/x-cocoa",
"jardiff" => "application/x-java-archive-diff",
"jnlp" => "application/x-java-jnlp-file",
"run" => "application/x-makeself",
"rar" => "application/x-rar-compressed",
"rpm" => "application/x-redhat-package-manager",
"sea" => "application/x-sea",
"swf" => "application/x-shockwave-flash",
"sit" => "application/x-stuffit",
"xpi" => "application/x-xpinstall",
"xhtml" => "application/xhtml+xml",
"xspf" => "application/xspf+xml",
"zip" => "application/zip",
"bin" => "application/octet-stream",
"exe" => "application/octet-stream",
"dll" => "application/octet-stream",
"deb" => "application/octet-stream",
"dmg" => "application/octet-stream",
"img" => "application/octet-stream",
"iso" => "application/octet-stream",
"msi" => "application/octet-stream",
"mp3" => "audio/mpeg",
"ogg" => "audio/ogg",
"m4a" => "audio/x-m4a",
"3gp" => "video/3gpp",
"3gpp" => "video/3gpp",
"ts" => "video/mp2t",
"mp4" => "video/mp4",
"mpg" => "video/mpeg",
"mpeg" => "video/mpeg",
"mov" => "video/quicktime",
"webm" => "video/webm",
"flv" => "video/x-flv",
"m4v" => "video/x-m4v",
"mng" => "video/x-mng",
"wmv" => "video/x-ms-wmv",
"avi" => "video/x-msvideo"
];

0
README.md Normal file → Executable file
View File

0
cache/.gitignore vendored Normal file → Executable file
View File

View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
<?php echo \htmlentities($title??'', ENT_QUOTES, 'UTF-8', false); ?>
</title>
</head>
<body>
<?php echo \htmlentities($text??'', ENT_QUOTES, 'UTF-8', false); ?>
<script src="/index.js"></script>
</body>
</html>

18
index.php Normal file → Executable file
View File

@ -1,12 +1,8 @@
<?php
include_once "Core/Core.php";
Route::get("/",function(){
echo view("hello",[
"text" => "Bir hata oluştu !"
]);
});
Route::Execute();
if(/*debug*/true)
{
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
}
include_once "Core/Core.php";

1
public/index.js Normal file
View File

@ -0,0 +1 @@
alert("Merhaba");

0
views/.gitignore vendored Normal file → Executable file
View File

15
views/hello.blade.php Executable file
View File

@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>
{{$title}}
</title>
</head>
<body>
{{$text}}
<script src="/index.js"></script>
</body>
</html>