|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BFW; |
|
4
|
|
|
|
|
5
|
|
|
use Constants; |
|
6
|
|
|
|
|
7
|
|
|
class Application extends Subjects |
|
8
|
|
|
{ |
|
9
|
|
|
protected static $instance = null; |
|
10
|
|
|
|
|
11
|
|
|
protected $rootDir = ''; |
|
12
|
|
|
|
|
13
|
|
|
protected $config; |
|
14
|
|
|
|
|
15
|
|
|
protected $options; |
|
16
|
|
|
|
|
17
|
|
|
protected $composerLoader; |
|
18
|
|
|
|
|
19
|
|
|
protected $runPhases = []; |
|
20
|
|
|
|
|
21
|
|
|
protected $memcached; |
|
22
|
|
|
|
|
23
|
|
|
protected $request; |
|
24
|
|
|
|
|
25
|
|
|
protected $modules; |
|
26
|
|
|
|
|
27
|
|
|
protected $errors; |
|
28
|
|
|
|
|
29
|
|
|
protected function __construct($options) |
|
30
|
|
|
{ |
|
31
|
|
|
ob_start(); |
|
32
|
|
|
|
|
33
|
|
|
$this->initOptions($options); |
|
34
|
|
|
$this->initConstants(); |
|
35
|
|
|
$this->initComposerLoader(); |
|
36
|
|
|
$this->initConfig(); |
|
37
|
|
|
$this->initRequest(); |
|
38
|
|
|
$this->initSession(); |
|
39
|
|
|
$this->initErrors(); |
|
40
|
|
|
$this->initModules(); |
|
41
|
|
|
|
|
42
|
|
|
$this->declareRunPhases(); |
|
43
|
|
|
|
|
44
|
|
|
//Defaut http header. Define here add possiblity to override him |
|
45
|
|
|
header('Content-Type: text/html; charset=utf-8'); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
public static function getInstance($options = []) |
|
49
|
|
|
{ |
|
50
|
|
|
if (self::$instance === null) { |
|
51
|
|
|
$calledClass = get_called_class(); //Autorize extends this class |
|
52
|
|
|
|
|
53
|
|
|
self::$instance = new $calledClass($options); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return self::$instance; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public static function init($options = []) |
|
60
|
|
|
{ |
|
61
|
|
|
return self::getInstance($options); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function getComposerLoader() |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->composerLoader; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getConfig($configKey) |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->config->getConfig($configKey); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
public function getOption($optionKey) |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->options->getOption($optionKey); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
public function getRequest() |
|
80
|
|
|
{ |
|
81
|
|
|
return $this->request; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function initOptions($options) |
|
85
|
|
|
{ |
|
86
|
|
|
$defaultOptions = [ |
|
87
|
|
|
'rootDir' => null, |
|
88
|
|
|
'vendorDir' => null, |
|
89
|
|
|
'runSession' => true |
|
90
|
|
|
]; |
|
91
|
|
|
|
|
92
|
|
|
$this->options = new \BFW\Core\Options($defaultOptions, $options); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
protected function initConstants() |
|
96
|
|
|
{ |
|
97
|
|
|
Constants::create('ROOT_DIR', $this->options->getOption('rootDir')); |
|
98
|
|
|
|
|
99
|
|
|
Constants::create('APP_DIR', ROOT_DIR.'app/'); |
|
100
|
|
|
Constants::create('SRC_DIR', ROOT_DIR.'src/'); |
|
101
|
|
|
Constants::create('WEB_DIR', ROOT_DIR.'web/'); |
|
102
|
|
|
|
|
103
|
|
|
Constants::create('CONFIG_DIR', APP_DIR.'config/'); |
|
104
|
|
|
Constants::create('MODULES_DIR', APP_DIR.'modules/'); |
|
105
|
|
|
|
|
106
|
|
|
Constants::create('CLI_DIR', SRC_DIR.'cli/'); |
|
107
|
|
|
Constants::create('CTRL_DIR', SRC_DIR.'controllers/'); |
|
108
|
|
|
Constants::create('MODELES_DIR', SRC_DIR.'modeles/'); |
|
109
|
|
|
Constants::create('VIEW_DIR', SRC_DIR.'view/'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
protected function initComposerLoader() |
|
113
|
|
|
{ |
|
114
|
|
|
$this->composerLoader = require($this->options->getOption('vendorDir').'autoload.php'); |
|
115
|
|
|
$this->addComposerNamespaces(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
protected function initConfig() |
|
119
|
|
|
{ |
|
120
|
|
|
$this->config = new \BFW\Config('bfw'); |
|
121
|
|
|
$this->config->loadFiles(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
protected function initRequest() |
|
125
|
|
|
{ |
|
126
|
|
|
$this->request = \BFW\Request::getInstance(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
protected function initSession() |
|
130
|
|
|
{ |
|
131
|
|
|
if ($this->options->getOption('runSession') === false) { |
|
132
|
|
|
return; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
//Destroy session cookie if browser quit |
|
136
|
|
|
session_set_cookie_params(0); |
|
137
|
|
|
|
|
138
|
|
|
//Run session |
|
139
|
|
|
session_start(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
protected function initErrors() |
|
143
|
|
|
{ |
|
144
|
|
|
$this->errors = new \BFW\Core\Errors($this); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
protected function initModules() |
|
148
|
|
|
{ |
|
149
|
|
|
$this->modules = new \BFW\Modules; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
protected function addComposerNamespaces() |
|
153
|
|
|
{ |
|
154
|
|
|
$this->composerLoader->addPsr4('Controller\\', CTRL_DIR); |
|
155
|
|
|
$this->composerLoader->addPsr4('Modules\\', MODULES_DIR); |
|
156
|
|
|
$this->composerLoader->addPsr4('Modeles\\', MODELES_DIR); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
protected function declareRunPhases() |
|
160
|
|
|
{ |
|
161
|
|
|
$this->runPhases = [ |
|
162
|
|
|
[$this, 'loadMemcached'], |
|
163
|
|
|
[$this, 'readAllModules'], |
|
164
|
|
|
[$this, 'loadAllCoreModules'], |
|
165
|
|
|
[$this, 'loadAllAppModules'], |
|
166
|
|
|
[$this, 'runCliFile'] |
|
167
|
|
|
]; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
public function run() |
|
171
|
|
|
{ |
|
172
|
|
|
foreach ($this->runPhases as $action) { |
|
173
|
|
|
$action(); |
|
174
|
|
|
|
|
175
|
|
|
$notifyAction = $action; |
|
176
|
|
|
if (is_array($action)) { |
|
177
|
|
|
$notifyAction = $action[1]; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
$this->notifyAction('apprun_'.$notifyAction); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
$this->notifyAction('bfw_run_finish'); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
protected function loadMemcached() |
|
187
|
|
|
{ |
|
188
|
|
|
$memcacheConfig = $this->getConfig('memcached'); |
|
189
|
|
|
|
|
190
|
|
|
if ($memcacheConfig['enabled'] === false) { |
|
191
|
|
|
return; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$class = $memcacheConfig['class']; |
|
195
|
|
|
if (empty($class)) { |
|
196
|
|
|
throw new Exception('Memcached is active but no class is define'); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
if (class_exists($class) === false) { |
|
200
|
|
|
throw new Exception('Memcache class '.$class.' not found.'); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$this->memcached = new $class; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
protected function readAllModules() |
|
207
|
|
|
{ |
|
208
|
|
|
$listModules = array_diff(scandir(MODULES_DIR), ['.', '..']); |
|
209
|
|
|
|
|
210
|
|
|
foreach ($listModules as $moduleName) { |
|
211
|
|
|
$moduleName = realpath($moduleName); //Symlink |
|
212
|
|
|
|
|
213
|
|
|
if (!is_dir($moduleName)) { |
|
214
|
|
|
continue; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
$this->modules->addModule($moduleName); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
$this->modules->generateTree(); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
protected function loadAllCoreModules() |
|
224
|
|
|
{ |
|
225
|
|
|
foreach ($this->getConfig('modules') as $moduleInfos) { |
|
226
|
|
|
$moduleName = $moduleInfos['name']; |
|
227
|
|
|
$moduleEnabled = $moduleInfos['enabled']; |
|
228
|
|
|
|
|
229
|
|
|
if (empty($moduleName) || $moduleEnabled === false) { |
|
230
|
|
|
continue; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
$this->loadModule($moduleName); |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
protected function loadAllAppModules() |
|
238
|
|
|
{ |
|
239
|
|
|
$tree = $this->modules->getLoadTree(); |
|
240
|
|
|
|
|
241
|
|
|
foreach ($tree as $firstLine) { |
|
242
|
|
|
foreach ($firstLine as $secondLine) { |
|
243
|
|
|
foreach ($secondLine as $moduleName) { |
|
244
|
|
|
$this->loadModule($moduleName); |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
protected function loadModule($moduleName) |
|
251
|
|
|
{ |
|
252
|
|
|
$this->notifyAction('load_module_'.$moduleName); |
|
253
|
|
|
$this->modules->getModule($moduleName)->runModule(); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
protected function runCliFile() |
|
257
|
|
|
{ |
|
258
|
|
|
if (PHP_SAPI !== 'cli') { |
|
259
|
|
|
return; |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
$opt = getopt('f:'); |
|
263
|
|
|
if (!isset($opt['f'])) { |
|
264
|
|
|
throw new Exception('Error: No file specified.'); |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
$file = $opt['f']; |
|
268
|
|
|
if (!file_exists(CLI_DIR.$file.'.php')) { |
|
269
|
|
|
throw new Exception('File to execute not found.'); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
$fctExecuteFile = function() use ($file) { |
|
273
|
|
|
require_once(CLI_DIR.$file.'.php'); |
|
274
|
|
|
}; |
|
275
|
|
|
|
|
276
|
|
|
$this->notifyAction('run_cli_file'); |
|
277
|
|
|
$fctExecuteFile(); |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
|