1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class App extends Prefab{ |
4
|
|
|
public $db; |
|
|
|
|
5
|
|
|
public $app; |
6
|
|
|
public $session; |
7
|
|
|
public $authenticatedUser; |
8
|
|
|
|
9
|
|
|
public function __construct() { |
10
|
|
|
$this->app = $this->app ?: Base::instance(); |
11
|
|
|
} |
12
|
|
|
|
13
|
|
|
static public function singleton() { |
14
|
|
|
if (Registry::exists('APP')) |
15
|
|
|
$app = Registry::get('APP'); |
16
|
|
|
else { |
17
|
|
|
$app = new self; |
18
|
|
|
Registry::set('APP',$app); |
19
|
|
|
} |
20
|
|
|
return $app; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function db() { |
|
|
|
|
24
|
|
|
return $this->db ?: $this->app->DB; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function session() { |
28
|
|
|
return $this->session ?: $this->app->SESSION; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function user() { |
32
|
|
|
return $this->authenticatedUser = $this->app->get('SESSION.USER')?:false; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function initialized($def = null) { |
36
|
|
|
return null !== $def ? $this->app->set('INITIALIZED', $def) : $this->app->get('INITIALIZED'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function config($key = null) { |
40
|
|
|
return $this->app->get($key?:'CONFIG') ; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function status() { |
44
|
|
|
return $this->app->IS_LIVE; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function run() { |
48
|
|
|
$this->loadConfig(); |
49
|
|
|
$this->loadRoutes(); |
50
|
|
|
$this->checkForMaintenance(); |
51
|
|
|
$this->configureDebug(); |
52
|
|
|
$this->configureDB(); |
53
|
|
|
$this->configureSession(); |
54
|
|
|
$this->configureAssets(); |
55
|
|
|
$this->registerErrorHandler(); |
56
|
|
|
$this->initialized(true); |
57
|
|
|
$this->app->run(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function loadRoutes($file = null){ |
61
|
|
|
$this->_load('routes', $file); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function loadConfig($file = null){ |
65
|
|
|
$this->_load('config', $file); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function _load($path, $file = null) { |
69
|
|
|
if($file) { |
70
|
|
|
$this->app->config(base_path("{path}/{$file}")); |
71
|
|
|
}else{ |
72
|
|
|
foreach(glob(base_path("{$path}/*.ini")) as $file) { |
73
|
|
|
$this->app->config($file); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function checkForMaintenance() { |
79
|
|
|
if(!$this->status()) { |
80
|
|
|
template('maintenance'); |
81
|
|
|
exit(); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function configureDebug() { |
86
|
|
|
if(!$this->app->DEV) { |
87
|
|
|
$this->app->set('DEBUG', 0); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function configureDB() { |
92
|
|
|
$type = strtolower($this->app->DB_TYPE); |
93
|
|
|
|
94
|
|
|
if($type == 'jig'){ |
95
|
|
|
$this->db = new DB\Jig($this->app->DB_PATH, DB\Jig::FORMAT_JSON); |
96
|
|
|
}else if($type == 'sql'){ |
97
|
|
|
$this->db = new DB\SQL($this->app->DB, $this->app->DB_USER, $this->app->DB_PSWD); |
98
|
|
|
}else if($type == 'mongo'){ |
99
|
|
|
$this->db = new DB\Mongo($this->app->DB, $this->app->DB_USER); |
100
|
|
|
} |
101
|
|
|
$this->app->set('DB', $this->db); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function configureSession() { |
105
|
|
|
$type = strtolower($this->app->SESSION); |
106
|
|
|
|
107
|
|
|
if($type) { |
108
|
|
|
if($this->app->CSRF && ('jig' == $type || 'sql' == $type || 'mongo' == $type)) { |
109
|
|
|
$this->configureCSRF($type); |
110
|
|
|
}else if($this->app->CSRF){ |
111
|
|
|
$this->session = new Session(null, 'CSRF'); |
112
|
|
|
}else{ |
113
|
|
|
if($type == 'jig' || $type == 'mongo' || $type == 'sql'){ |
114
|
|
|
$session = str_ireplace('/', '', 'DB\/'.$this->_getDBType($type).'\Session'); |
115
|
|
|
$this->session = new $session($this->app->DB); |
116
|
|
|
}else{ |
117
|
|
|
$this->session = new Session(); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
$this->app->set('SESSION', $this->session); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function configureCSRF($type) { |
125
|
|
|
$session = str_ireplace('/', '', 'DB\/'.$this->_getDBType($type).'\Session'); |
126
|
|
|
$this->session = new $session($this->app->DB, 'sessions', null, 'CSRF'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
private function _getDBType($type) { |
130
|
|
|
if($type == 'jig' || $type == 'mongo'){ |
131
|
|
|
return ucfirst($type); |
132
|
|
|
}else if($type == 'sql') { |
133
|
|
|
return strtoupper($type); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function configureAssets() { |
138
|
|
|
$assets = Assets::instance(); |
|
|
|
|
139
|
|
|
$this->app->set('ASSETS.onFileNotFound',function($file) { |
140
|
|
|
echo 'file not found: '.$file; |
141
|
|
|
}); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function registerErrorHandler() { |
145
|
|
|
if($this->app->DEV) { |
146
|
|
|
Falsum\Run::handler($this->app->DEBUG != 3); |
147
|
|
|
}else{ |
148
|
|
|
$this->app->set('ONERROR', 'App\Core\Controllers\ErrorController->init'); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
} |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.