-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApplication.php
More file actions
88 lines (77 loc) · 2.4 KB
/
Application.php
File metadata and controls
88 lines (77 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
namespace khokonc\mvc;
use App\Exceptions\NotFoundException;
use khokonc\mvc\Auth;
use khokonc\mvc\Database\Database;
use khokonc\mvc\Exceptions\HttpRedirectException;
use khokonc\mvc\Http\HttpRedirectResponse;
use khokonc\mvc\Request;
use khokonc\mvc\Routing\Route;
use khokonc\mvc\Routing\Router;
use khokonc\mvc\Session;
use khokonc\mvc\Views\View;
/**
*
*/
class Application
{
public View $view;
public Request $request;
public Session $session;
public Auth $auth;
public Database $db;
public Router $router;
public array $middleware;
public static $app;
public function __construct()
{
$this->loadCredential();
$this->session = new Session();
$this->request = new Request($this->session);
$this->view = new View($this->request, $this->session);
$this->auth = new Auth($this->session);
$this->router = new Router();
$this->db = new Database();
self::$app = $this;
}
public function init()
{
new Route($this);
}
private function loadCredential()
{
require_once "Helpers.php";
$this->middleware = require_once config('app.base_url')."/app/Kernel.php";
}
public function run()
{
try {
$response = $this->router->resolve();
if($response instanceof HttpRedirectResponse){
throw new HttpRedirectException($response->url);
}
return $response;
} catch (NotFoundException $error) {
http_response_code($error->getCode());
$view = config('app.error_view_path')."/404.php";
if (file_exists($view)) {
return view('errors.404', [
'pageTitle' => 'Page not found',
'code' => $error->getCode(),
'message' => $error->getMessage()
]);
}
return $error->getMessage();
}catch (HttpRedirectException $error){
http_response_code($error->getCode());
header("location:".$error->getMessage());
}
catch (\Exception $error) {
http_response_code($error->getCode());
if(config('app.app_debug')){
return $this->view->renderError($error->getMessage(),$error->getCode());
}
return $error->getMessage();
}
}
}