/
Application.php
94 lines (79 loc) · 2.55 KB
/
Application.php
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
89
90
91
92
93
94
<?php
namespace mcanan\framework;
require_once 'Common.php';
class Application
{
private $router = null;
private $cache = null;
public function setRouter($router)
{
$this->router = $router;
}
public function setCache($cache)
{
$this->cache = $cache;
}
public function setSecurity($security)
{
setSecurityInstance($security);
}
public function getRouter()
{
if ($this->router==null) {
require_once 'BasicRouter.php';
$this->router = new BasicRouter();
}
return $this->router;
}
public function getCache()
{
if ($this->cache==null) {
require_once 'BasicCache.php';
$this->cache = new BasicCache();
}
return $this->cache;
}
public function loadConfigurationFile($file)
{
require_once $file;
}
public function init()
{
$benchmark =& getBenchmarkInstance();
$benchmark->mark("application_start");
$output =& getOutputInstance();
$security =& getSecurityInstance();
$url = isset($_GET['url']) ? $_GET['url'] : '';
$this->getRouter()->setUrl($url);
$controller = $this->getRouter()->getController();
$action = $this->getRouter()->getAction();
$parameters = $this->getRouter()->getParameters();
// Check if it is authorized
if (is_null($security) || $security->isAuthorized($url, $controller, $action, $parameters)) {
// Check cache
if ($this->getCache()->exists($controller, $action, $parameters)) {
$time = $this->getCache()->getExpiration($controller, $action, $parameters);
$fileName = $this->getCache()->getFilename($controller, $action, $parameters);
if ($output->displayFromCache($fileName, $time)) {
// Get output from cache and exit
exit;
}
}
$benchmark->mark("controller_start c:$controller a:$action");
$this->getRouter()->dispatch();
$benchmark->mark("controller_end");
$benchmark->mark("application_end");
if ($output->hasContent()) {
$output->display();
}
} else {
$url = $security->getAccessDeniedUrl();
if (!is_null($url)) {
header('Location: '.$url);
} else {
header("HTTP/1.1 401 Unauthorized");
}
exit;
}
}
}