Route public / mimetypes
This commit is contained in:
parent
d4cf0fd331
commit
9e5f941436
|
@ -1,10 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
include_once "Request.php";
|
include_once "Request.php";
|
||||||
include_once "Response.php";
|
include_once "Response.php";
|
||||||
|
include_once "mimes.php";
|
||||||
include_once "Session.php";
|
include_once "Session.php";
|
||||||
include_once "Helpers.php";
|
include_once "Helpers.php";
|
||||||
include_once "DatabaseConnection.php";
|
|
||||||
include_once "DatabaseORM.php";
|
|
||||||
include_once "DatabaseMySQLEngine.php";
|
|
||||||
include_once "Blade.php";
|
include_once "Blade.php";
|
||||||
include_once "Route.php";
|
include_once "Route.php";
|
|
@ -2,7 +2,6 @@
|
||||||
include_once "Reflection.php";
|
include_once "Reflection.php";
|
||||||
include_once "RequestHeader.php";
|
include_once "RequestHeader.php";
|
||||||
include_once "RequestFile.php";
|
include_once "RequestFile.php";
|
||||||
include_once "Request.php";
|
|
||||||
include_once "Session.php";
|
include_once "Session.php";
|
||||||
class Request
|
class Request
|
||||||
{
|
{
|
||||||
|
|
|
@ -2,12 +2,16 @@
|
||||||
include_once "RequestHeader.php";
|
include_once "RequestHeader.php";
|
||||||
class Response
|
class Response
|
||||||
{
|
{
|
||||||
|
public static $mimes;
|
||||||
public static function File($path, $mime = "auto")
|
public static function File($path, $mime = "auto")
|
||||||
{
|
{
|
||||||
if($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');
|
$fp = fopen($path, 'rb');
|
||||||
fpassthru($fp);
|
fpassthru($fp);
|
||||||
fclose($fp);
|
fclose($fp);
|
||||||
|
|
|
@ -69,6 +69,7 @@
|
||||||
class Route
|
class Route
|
||||||
{
|
{
|
||||||
public static $routes = [];
|
public static $routes = [];
|
||||||
|
public static $publics = [];
|
||||||
public static $current = null;
|
public static $current = null;
|
||||||
public static function post($path, $callback)
|
public static function post($path, $callback)
|
||||||
{
|
{
|
||||||
|
@ -112,6 +113,15 @@
|
||||||
Route::$routes[] = $route;
|
Route::$routes[] = $route;
|
||||||
return $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)
|
public static function any($path, $callback)
|
||||||
{
|
{
|
||||||
$magic = magicRoute($path);
|
$magic = magicRoute($path);
|
||||||
|
@ -146,6 +156,14 @@
|
||||||
return $route;
|
return $route;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
foreach (Route::$publics as $route) {
|
||||||
|
$route->clear();
|
||||||
|
if($route->Run())
|
||||||
|
{
|
||||||
|
Route::$current = $route;
|
||||||
|
return $route;
|
||||||
|
}
|
||||||
|
};
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
return Route::$current;
|
return Route::$current;
|
||||||
|
@ -157,6 +175,7 @@
|
||||||
if($route === false)
|
if($route === false)
|
||||||
{
|
{
|
||||||
Response::Code(404);
|
Response::Code(404);
|
||||||
|
exit;
|
||||||
};
|
};
|
||||||
$route->call();
|
$route->call();
|
||||||
}
|
}
|
||||||
|
@ -168,6 +187,7 @@
|
||||||
|
|
||||||
public string $type;
|
public string $type;
|
||||||
public string $path;
|
public string $path;
|
||||||
|
public string $src;
|
||||||
public $callback;
|
public $callback;
|
||||||
public string $method;
|
public string $method;
|
||||||
|
|
||||||
|
@ -185,8 +205,17 @@
|
||||||
}
|
}
|
||||||
public function call(...$args)
|
public function call(...$args)
|
||||||
{
|
{
|
||||||
|
switch ($this->type) {
|
||||||
|
case 'magic':
|
||||||
|
case 'text':
|
||||||
$callback = $this->callback;
|
$callback = $this->callback;
|
||||||
$callback(...$args);
|
$callback(...$args);
|
||||||
|
break;
|
||||||
|
case 'public':
|
||||||
|
$callback = $this->callback;
|
||||||
|
Response::File($callback);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public function Run()
|
public function Run()
|
||||||
{
|
{
|
||||||
|
@ -197,21 +226,18 @@
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$url = parse_url($requestUrl, PHP_URL_PATH);
|
$url = parse_url($requestUrl, PHP_URL_PATH);
|
||||||
if($this->type == "text")
|
switch($this->type)
|
||||||
{
|
{
|
||||||
|
case "text":{
|
||||||
if($url == $this->path)
|
if($url == $this->path)
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}else{
|
}else{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
break;
|
||||||
|
}
|
||||||
$t = validateMagicRoute(
|
case "magic":{
|
||||||
$this->path,
|
|
||||||
$url
|
|
||||||
);
|
|
||||||
|
|
||||||
if($t === false)
|
if($t === false)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
|
@ -219,5 +245,27 @@
|
||||||
$this->match = $t;
|
$this->match = $t;
|
||||||
return true;
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -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,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>
|
|
@ -1,12 +1,8 @@
|
||||||
<?php
|
<?php
|
||||||
|
if(/*debug*/true)
|
||||||
|
{
|
||||||
|
ini_set('display_errors', 1);
|
||||||
|
ini_set('display_startup_errors', 1);
|
||||||
|
error_reporting(E_ALL);
|
||||||
|
}
|
||||||
include_once "Core/Core.php";
|
include_once "Core/Core.php";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Route::get("/",function(){
|
|
||||||
echo view("hello",[
|
|
||||||
"text" => "Bir hata oluştu !"
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
|
|
||||||
Route::Execute();
|
|
|
@ -0,0 +1 @@
|
||||||
|
alert("Merhaba");
|
|
@ -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>
|
Loading…
Reference in New Issue