Initial import
This commit is contained in:
77
lib/App.php
Executable file
77
lib/App.php
Executable file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
class App {
|
||||
|
||||
static function dispatch($req) {
|
||||
|
||||
$apiroute = Routes::find_api($req->type(), $req->path());
|
||||
if ($apiroute !== false) { // It is an API route. Treat it as such.
|
||||
$req->set_route($apiroute);
|
||||
$out = $apiroute->call($req);
|
||||
header("Content-type: application/json");
|
||||
if ($out instanceof Collection) {
|
||||
print(json_encode($out->all()));
|
||||
return;
|
||||
}
|
||||
if (is_array($out)) { // This is an array return type. Do things depending on the result key
|
||||
|
||||
$status = $out[0];
|
||||
$data = $out[1];
|
||||
if (count($out) > 2) {
|
||||
$headers = $out[2];
|
||||
} else {
|
||||
$headers = [];
|
||||
}
|
||||
|
||||
header("HTTP/1.1 $status");
|
||||
foreach ($headers as $k=>$v) {
|
||||
header("$k: $v");
|
||||
}
|
||||
if (is_array($data)) {
|
||||
print(json_encode($data));
|
||||
} else {
|
||||
print($data);
|
||||
}
|
||||
return;
|
||||
}
|
||||
print(json_encode($out));
|
||||
return;
|
||||
}
|
||||
|
||||
$webroute = Routes::find_web($req->type(), $req->path());
|
||||
if ($webroute !== false) { // It is a WEB route. Treat it as such.
|
||||
$req->set_route($webroute);
|
||||
$out = $webroute->call($req);
|
||||
if (is_array($out)) { // This is an array return type. Do things depending on the result key
|
||||
|
||||
$status = $out[0];
|
||||
$data = $out[1];
|
||||
if (count($out) > 2) {
|
||||
$headers = $out[2];
|
||||
} else {
|
||||
$headers = [];
|
||||
}
|
||||
|
||||
header("HTTP/1.1 $status");
|
||||
foreach ($headers as $k=>$v) {
|
||||
header("$k: $v");
|
||||
}
|
||||
print($data);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($out instanceof File) {
|
||||
$out->set_header("Content-Length", $out->size());
|
||||
$out->emit();
|
||||
return;
|
||||
}
|
||||
|
||||
print($out);
|
||||
return;
|
||||
}
|
||||
|
||||
header("HTTP/1.1 404 Not Found");
|
||||
print(blade("404"));
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user