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