1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Suricate; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Suricate - Another micro PHP framework |
9
|
|
|
* |
10
|
|
|
* @author Mathieu LESNIAK <[email protected]> |
11
|
|
|
* @copyright 2013-2019 Mathieu LESNIAK |
12
|
|
|
* @version 0.2.0 |
13
|
|
|
* @package Suricate |
14
|
|
|
* |
15
|
|
|
* @method static \Suricate\App App($newInstance = false) Get instance of App service |
16
|
|
|
* @method static \Suricate\Cache Cache($nezwInstance = false) Get instance of Cache service |
17
|
|
|
* @method static \Suricate\CacheMemcache CacheMemcache($newInstance = false) Get instance of CacheMemcache service |
18
|
|
|
* @method static \Suricate\CacheMemcached CacheMemcached($newInstance = false) Get instance of CacheMemcached service |
19
|
|
|
* @method static \Suricate\CacheApc CacheApc($newInstance = false) Get instance of CacheApc service |
20
|
|
|
* @method static \Suricate\CacheFile CacheFile($newInstance = false) Get instance of CacheFile service |
21
|
|
|
* @method static \Suricate\Curl Curl($newInstance = false) Get instance of Curl service |
22
|
|
|
* @method static \Suricate\Database Database($newInstance = false) Get instance of Database service |
23
|
|
|
* @method static \Suricate\Error Error($newInstance = false) Get instance of Error service |
24
|
|
|
* @method static \Suricate\EventDispatcher EventDispatcher($newInstance = false) Get instance of EventDispatcher service |
25
|
|
|
* @method static \Suricate\I18n I18n($newInstance = false) Get instance of I18n service |
26
|
|
|
* @method static \Suricate\Logger Logger($newInstance = false) Get instance of Logger service |
27
|
|
|
* @method static \Suricate\Request Request($newInstance = false) Get instance of Request service |
28
|
|
|
* @method static \Suricate\Request Response($newInstance = false) Get instance of Request/Response service |
29
|
|
|
* @method static \Suricate\Router Router($newInstance = false) Get instance of Router service |
30
|
|
|
* @method static \Suricate\Session Session($newInstance = false) Get instance of Session service |
31
|
|
|
* @method static \Suricate\SessionNative SessionNative($newInstance = false) Get instance of Session service |
32
|
|
|
* @method static \Suricate\SessionCookie SessionCookie($newInstance = false) Get instance of Session service |
33
|
|
|
* @method static \Suricate\SessionMemcache SessionMemcache($newInstance = false) Get instance of Session service |
34
|
|
|
*/ |
35
|
|
|
|
36
|
|
|
class Suricate |
37
|
|
|
{ |
38
|
|
|
const VERSION = '0.2.4'; |
39
|
|
|
|
40
|
|
|
const CONF_DIR = '/conf/'; |
41
|
|
|
|
42
|
|
|
private $config = []; |
43
|
|
|
private $configFile = []; |
44
|
|
|
|
45
|
|
|
private $useAutoloader = false; |
46
|
|
|
|
47
|
|
|
private static $servicesContainer; |
48
|
|
|
private static $servicesRepository; |
49
|
|
|
|
50
|
|
|
private $servicesList = [ |
51
|
|
|
'App' => '\Suricate\App', |
52
|
|
|
'Cache' => '\Suricate\Cache', |
53
|
|
|
'CacheMemcache' => '\Suricate\Cache\Memcache', |
54
|
|
|
'CacheMemcached' => '\Suricate\Cache\Memcached', |
55
|
|
|
'CacheApc' => '\Suricate\Cache\Apc', |
56
|
|
|
'CacheFile' => '\Suricate\Cache\File', |
57
|
|
|
'Curl' => '\Suricate\Curl', |
58
|
|
|
'Database' => '\Suricate\Database', |
59
|
|
|
'Error' => '\Suricate\Error', |
60
|
|
|
'EventDispatcher' => '\Suricate\EventDispatcher', |
61
|
|
|
'I18n' => '\Suricate\I18n', |
62
|
|
|
'Logger' => '\Suricate\Logger', |
63
|
|
|
'Request' => '\Suricate\Request', |
64
|
|
|
'Response' => '\Suricate\Request', |
65
|
|
|
'Router' => '\Suricate\Router', |
66
|
|
|
'Session' => '\Suricate\Session', |
67
|
|
|
'SessionNative' => '\Suricate\Session\Native', |
68
|
|
|
'SessionCookie' => '\Suricate\Session\Cookie', |
69
|
|
|
'SessionMemcache' => '\Suricate\Session\Memcache' |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Suricate contructor |
74
|
|
|
* |
75
|
|
|
* @param array $paths Application paths |
76
|
|
|
* @param string|array|null $configFile path of configuration file(s) |
77
|
|
|
* |
78
|
|
|
* @SuppressWarnings(PHPMD.StaticAccess) |
79
|
|
|
*/ |
80
|
8 |
|
public function __construct($paths = [], $configFile = null) |
81
|
|
|
{ |
82
|
8 |
|
if ($configFile !== null) { |
83
|
6 |
|
$this->setConfigFile($configFile); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
// Load helpers |
87
|
8 |
|
require_once __DIR__ . DIRECTORY_SEPARATOR . 'Helper.php'; |
88
|
|
|
|
89
|
8 |
|
$this->loadConfig(); |
90
|
8 |
|
$this->setAppPaths($paths); |
91
|
|
|
|
92
|
8 |
|
if ($this->useAutoloader) { |
93
|
|
|
// Configure autoloader |
94
|
|
|
require_once __DIR__ . DIRECTORY_SEPARATOR . 'AutoLoader.php'; |
95
|
|
|
AutoLoader::register(); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// Define error handler |
99
|
8 |
|
set_exception_handler(['\Suricate\Error', 'handleException']); |
100
|
8 |
|
set_error_handler(['\Suricate\Error', 'handleError']); |
101
|
8 |
|
register_shutdown_function(['\Suricate\Error', 'handleShutdownError']); |
102
|
|
|
|
103
|
8 |
|
self::$servicesRepository = new Container(); |
104
|
|
|
|
105
|
8 |
|
$this->initServices(); |
106
|
8 |
|
} |
107
|
|
|
|
108
|
1 |
|
public function getConfig() |
109
|
|
|
{ |
110
|
1 |
|
return $this->config; |
111
|
|
|
} |
112
|
|
|
|
113
|
8 |
|
private function setAppPaths($paths = []) |
114
|
|
|
{ |
115
|
8 |
|
foreach ($paths as $key => $value) { |
116
|
|
|
$this->config['App']['path.' . $key] = realpath($value); |
117
|
|
|
} |
118
|
|
|
|
119
|
8 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
/** |
122
|
|
|
* Initialize Framework services |
123
|
|
|
* @return null |
124
|
|
|
*/ |
125
|
8 |
|
private function initServices() |
126
|
|
|
{ |
127
|
8 |
|
self::$servicesRepository->setWarehouse($this->servicesList); |
128
|
|
|
|
129
|
8 |
|
self::$servicesRepository['Request']->parse(); |
130
|
8 |
|
if (isset($this->config['App']['locale'])) { |
131
|
|
|
$this->config['I18n'] = [ |
132
|
|
|
'locale' => $this->config['App']['locale'] |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
// first sync, && init, dependency to Suricate::request |
136
|
8 |
|
self::$servicesContainer = clone self::$servicesRepository; |
137
|
|
|
|
138
|
8 |
|
foreach (array_keys($this->servicesList) as $serviceName) { |
139
|
8 |
|
if (isset($this->config[$serviceName])) { |
140
|
8 |
|
self::$servicesRepository[$serviceName]->configure( |
141
|
8 |
|
$this->config[$serviceName] |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
TODO : remove sync in service creation |
146
|
|
|
*/ |
147
|
8 |
|
self::$servicesContainer = clone self::$servicesRepository; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
8 |
|
if (isset($this->config['Constants'])) { |
152
|
|
|
foreach ( |
153
|
1 |
|
$this->config['Constants'] |
154
|
|
|
as $constantName => $constantValue |
155
|
|
|
) { |
156
|
1 |
|
$constantName = strtoupper($constantName); |
157
|
1 |
|
define($constantName, $constantValue); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
// final sync, repository is complete |
162
|
8 |
|
self::$servicesContainer = clone self::$servicesRepository; |
163
|
8 |
|
} |
164
|
|
|
|
165
|
1 |
|
public function hasService(string $serviceName): bool |
166
|
|
|
{ |
167
|
1 |
|
return isset(self::$servicesContainer[$serviceName]); |
168
|
|
|
} |
169
|
|
|
|
170
|
6 |
|
private function setConfigFile($configFile) |
171
|
|
|
{ |
172
|
6 |
|
foreach ((array) $configFile as $file) { |
173
|
6 |
|
if (is_file($file)) { |
174
|
6 |
|
$this->configFile[] = $file; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
6 |
|
return $this; |
179
|
|
|
} |
180
|
|
|
/** |
181
|
|
|
* Load framework configuration from ini file |
182
|
|
|
* @return null |
183
|
|
|
*/ |
184
|
8 |
|
private function loadConfig() |
185
|
|
|
{ |
186
|
8 |
|
$userConfig = []; |
187
|
8 |
|
if (count($this->configFile)) { |
188
|
6 |
|
$userConfig = []; |
189
|
6 |
|
foreach ($this->configFile as $configFile) { |
190
|
6 |
|
$userConfig = array_merge_recursive( |
191
|
6 |
|
$userConfig, |
192
|
6 |
|
(array) parse_ini_file($configFile, true, INI_SCANNER_TYPED) |
193
|
|
|
); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
// Advanced ini parsing, split key with '.' into subarrays |
197
|
6 |
|
foreach ($userConfig as $section => $configData) { |
198
|
5 |
|
foreach ($configData as $name => $value) { |
199
|
5 |
|
if (stripos($name, '.') !== false) { |
200
|
|
|
$subkeys = explode('.', $name); |
201
|
|
|
unset($userConfig[$section][$name]); |
202
|
|
|
$str = |
203
|
|
|
"['" . implode("']['", $subkeys) . "'] = \$value;"; |
204
|
|
|
eval("\$userConfig[\$section]" . $str); |
|
|
|
|
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
210
|
8 |
|
foreach ($this->getDefaultConfig() as $context => $directives) { |
211
|
8 |
|
if (isset($userConfig[$context])) { |
212
|
|
|
$this->config[$context] = array_merge( |
213
|
|
|
$directives, |
214
|
|
|
$userConfig[$context] |
215
|
|
|
); |
216
|
|
|
unset($userConfig[$context]); |
217
|
|
|
} else { |
218
|
8 |
|
$this->config[$context] = $directives; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|
222
|
8 |
|
$this->config = array_merge($this->config, $userConfig); |
223
|
|
|
|
224
|
8 |
|
$this->configureAppMode(); |
225
|
8 |
|
} |
226
|
|
|
|
227
|
8 |
|
private function configureAppMode() |
228
|
|
|
{ |
229
|
8 |
|
$errorReporting = true; |
230
|
8 |
|
$errorDumpContext = true; |
231
|
8 |
|
$logLevel = Logger::LOGLEVEL_WARN; |
232
|
8 |
|
$logFile = 'php://stdout'; |
233
|
|
|
|
234
|
8 |
|
if (isset($this->config['App']['mode'])) { |
235
|
|
|
switch ($this->config['App']['mode']) { |
236
|
|
|
case App::DEVELOPMENT_MODE: |
237
|
|
|
$errorReporting = true; |
238
|
|
|
$errorDumpContext = true; |
239
|
|
|
$logLevel = Logger::LOGLEVEL_INFO; |
240
|
|
|
$logFile = 'php://stdout'; |
241
|
|
|
break; |
242
|
|
|
case App::DEBUG_MODE: |
243
|
|
|
$errorReporting = true; |
244
|
|
|
$errorDumpContext = true; |
245
|
|
|
$logLevel = Logger::LOGLEVEL_DEBUG; |
246
|
|
|
$logFile = 'php://stdout'; |
247
|
|
|
break; |
248
|
|
|
case App::PRELIVE_MODE: |
249
|
|
|
$errorReporting = true; |
250
|
|
|
$errorDumpContext = false; |
251
|
|
|
$logLevel = Logger::LOGLEVEL_WARN; |
252
|
|
|
$logFile = 'php://stderr'; |
253
|
|
|
break; |
254
|
|
|
case App::PRODUCTION_MODE: |
255
|
|
|
$errorReporting = false; |
256
|
|
|
$errorDumpContext = false; |
257
|
|
|
$logLevel = Logger::LOGLEVEL_WARN; |
258
|
|
|
$logFile = 'php://stderr'; |
259
|
|
|
break; |
260
|
|
|
} |
261
|
|
|
} |
262
|
8 |
|
if (isset($this->config['Logger']['level'])) { |
263
|
|
|
$logLevel = $this->config['Logger']['level']; |
264
|
|
|
} |
265
|
8 |
|
if (isset($this->config['Logger']['logfile'])) { |
266
|
|
|
$logFile = $this->config['Logger']['logfile']; |
267
|
|
|
} |
268
|
8 |
|
if (isset($this->config['Error']['report'])) { |
269
|
|
|
$errorReporting = $this->config['Error']['report']; |
270
|
|
|
} |
271
|
8 |
|
if (isset($this->config['Error']['dumpContext'])) { |
272
|
|
|
$errorDumpContext = $this->config['Error']['dumpContext']; |
273
|
|
|
} |
274
|
|
|
|
275
|
8 |
|
$this->config['Logger']['level'] = $logLevel; |
276
|
8 |
|
$this->config['Logger']['logfile'] = $logFile; |
277
|
8 |
|
$this->config['Error']['report'] = $errorReporting; |
278
|
8 |
|
$this->config['Error']['dumpContext'] = $errorDumpContext; |
279
|
8 |
|
} |
280
|
|
|
/** |
281
|
|
|
* Default setup template |
282
|
|
|
* @return array setup |
283
|
|
|
*/ |
284
|
8 |
|
private function getDefaultConfig() |
285
|
|
|
{ |
286
|
|
|
return [ |
287
|
8 |
|
'Router' => [], |
288
|
|
|
'Logger' => [ |
289
|
|
|
'enabled' => true |
290
|
|
|
], |
291
|
|
|
'App' => ['base_uri' => '/'] |
292
|
|
|
]; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
public function run() |
296
|
|
|
{ |
297
|
|
|
self::$servicesContainer['Router']->doRouting(); |
298
|
|
|
} |
299
|
|
|
|
300
|
19 |
|
public static function __callStatic($name, $arguments) |
301
|
|
|
{ |
302
|
19 |
|
if (isset($arguments[0]) && $arguments[0] === true) { |
303
|
1 |
|
return clone self::$servicesRepository[$name]; |
304
|
|
|
} |
305
|
|
|
|
306
|
19 |
|
return self::$servicesContainer[$name]; |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|