1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FFMVC; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Main App Class |
7
|
|
|
* |
8
|
|
|
* This should be included and run by every app upon initialisation and termination |
9
|
|
|
* It sets up the base environment for the app - should not create objects |
10
|
|
|
* |
11
|
|
|
* @author Vijay Mahrra <[email protected]> |
12
|
|
|
* @copyright (c) Copyright 2016 Vijay Mahrra |
13
|
|
|
* @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html) |
14
|
|
|
*/ |
15
|
|
|
class App extends \Prefab |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* setup the base application environment. |
19
|
|
|
* |
20
|
|
|
* @return void |
21
|
|
|
*/ |
22
|
|
|
public static function start() |
23
|
|
|
{ |
24
|
|
|
$f3 = \Base::instance(); |
25
|
|
|
|
26
|
|
|
// http://php.net/manual/en/function.ob-start.php |
27
|
|
|
ob_start(); |
28
|
|
|
|
29
|
|
|
// read config and overrides |
30
|
|
|
// @see http://fatfreeframework.com/framework-variables#configuration-files |
31
|
|
|
$f3->config('config/default.ini'); |
32
|
|
|
|
33
|
|
|
// by default this file does not exist, you must create it and include your local settings |
34
|
|
|
if (file_exists('config/config.ini')) { |
35
|
|
|
$f3->config('config/config.ini'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
// hive vars we are setting |
39
|
|
|
$hive = []; |
40
|
|
|
|
41
|
|
|
// set home directory for project |
42
|
|
|
$hive['HOMEDIR'] = realpath($f3->get('SERVER.DOCUMENT_ROOT') . '/../'); |
43
|
|
|
|
44
|
|
|
// make sure directories are full, not relative path |
45
|
|
|
foreach (['LOGS', 'TEMP', 'UPLOADS'] as $key) { |
46
|
|
|
$dir = $f3->get($key); |
47
|
|
|
if (!empty($dir)) { |
48
|
|
|
$hive[$key] = realpath($dir) . '/'; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// these take multiple paths |
53
|
|
|
$language = substr($f3->get('LANGUAGE'),0,2); |
54
|
|
|
foreach (['LOCALES', 'UI'] as $key) { |
55
|
|
|
$paths = $f3->get($key); |
56
|
|
|
// remove any invalid dirs |
57
|
|
|
if (!empty($paths)) { |
58
|
|
|
$dirs = $f3->split($paths); |
59
|
|
|
foreach ($dirs as $k => $dir) { |
60
|
|
|
if (empty($dir)) { |
61
|
|
|
unset($dirs[$k]); |
62
|
|
|
continue; |
63
|
|
|
} |
64
|
|
|
// switch for different language templates |
65
|
|
|
$langDir = ''; |
66
|
|
|
if ('UI' == $key) { |
67
|
|
|
$langDir = 'templates/' . $language . '/' . $dir; |
68
|
|
|
if (!file_exists($langDir)) { |
69
|
|
|
unset($langDir); |
70
|
|
|
} |
71
|
|
|
$dir = 'templates/en/' . $dir; |
72
|
|
|
} |
73
|
|
|
$dirs[$k] = empty($langDir) ? realpath($dir) . '/' |
74
|
|
|
: realpath($langDir) . '/' . ';' . realpath($dir) . '/'; |
75
|
|
|
} |
76
|
|
|
$hive[$key] = join(';', $dirs); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// enable full logging if not production |
81
|
|
|
$ini = []; |
82
|
|
|
$hive['log'] = $f3->get('log'); |
83
|
|
|
if (empty($hive['log']['file'])) { |
84
|
|
|
$hive['log']['file'] = '/dev/null'; |
85
|
|
|
} elseif ('production' !== $f3->get('app.env')) { |
86
|
|
|
$ini = array_merge($ini, [ |
87
|
|
|
'log_errors' => 'On', |
88
|
|
|
'error_log' => $hive['log']['file'], |
89
|
|
|
'error_reporting' => -1, |
90
|
|
|
'ignore_repeated_errors' => 'On', |
91
|
|
|
'ignore_repeated_source' => 'On', |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// parse params for http-style dsn |
96
|
|
|
// setup database connection params |
97
|
|
|
// @see http://fatfreeframework.com/databases |
98
|
|
|
$db = $f3->get('db'); |
99
|
|
|
if (!empty($db['dsn_http'])) { |
100
|
|
|
$dbParams = $db; |
101
|
|
|
$params = \FFMVC\Helpers\DB::instance()->parseHttpDsn($db['dsn_http']); |
102
|
|
|
$params['dsn'] = \FFMVC\Helpers\DB::instance()->createDbDsn($params); |
103
|
|
|
$dbParams = array_merge($dbParams, $params); |
104
|
|
|
$hive['db'] = $dbParams; |
105
|
|
|
} |
106
|
|
|
// setup outgoing email server for php mail command |
107
|
|
|
$email = $f3->get('email'); // email settings |
108
|
|
|
$ini = array_merge($ini, [ |
109
|
|
|
'SMTP' => $email['host'], |
110
|
|
|
'sendmail_from' => $email['from'], |
111
|
|
|
'smtp_port' => $email['port'], |
112
|
|
|
'user' => $email['user'], |
113
|
|
|
'password' => $email['pass'], |
114
|
|
|
]); |
115
|
|
|
|
116
|
|
|
// multiple-set of $hive array to $f3 hive |
117
|
|
|
$f3->mset($hive); |
118
|
|
|
// non-CLI mode ends here |
119
|
|
|
if (empty($f3->get('CLI'))) { |
120
|
|
|
// set ini settings |
121
|
|
|
foreach ($ini as $value => $setting) { |
122
|
|
|
ini_set($value, $setting); |
123
|
|
|
} |
124
|
|
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// set ini settings |
128
|
|
|
// log errors if run on command line |
129
|
|
|
foreach (array_merge($ini, [ |
130
|
|
|
'display_errors' => 'On', |
131
|
|
|
'error_log' => 'On', |
132
|
|
|
'html_errors' => 'Off', |
133
|
|
|
]) as $value => $setting) { |
134
|
|
|
ini_set($value, $setting); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// set default error handler output for CLI mode |
138
|
|
|
$f3->set('ONERROR', function ($f3) { |
139
|
|
|
$error = $f3->get('ERROR'); |
140
|
|
|
|
141
|
|
|
// detailed error notifications because it's not public |
142
|
|
|
$errorMessage = sprintf("Trace: %s\n\nException %d: %s\n%s\n", |
143
|
|
|
print_r($f3->trace(null, false), 1), $error['code'], $error['status'], $error['text'] |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
echo $errorMessage; |
147
|
|
|
|
148
|
|
|
if (\Registry::exists('logger')) { |
149
|
|
|
$logger = \Registry::get('logger'); |
150
|
|
|
if (is_object($logger)) { |
151
|
|
|
$logger->write($errorMessage, $f3->get('log.date')); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
}); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* tasks for the application once run. |
159
|
|
|
* |
160
|
|
|
* @return void |
161
|
|
|
*/ |
162
|
|
|
public static function finish() |
163
|
|
|
{ |
164
|
|
|
// log script execution time if debugging |
165
|
|
|
$f3 = \Base::instance(); |
166
|
|
|
$debug = $f3->get('DEBUG'); |
167
|
|
|
$logger = \Registry::exists('logger') ? \Registry::get('logger') : null; |
168
|
|
|
|
169
|
|
|
if ('production' !== $f3->get('app.env') || !empty($logger) && $debug) { |
170
|
|
|
|
171
|
|
|
// log database transactions if level 3 |
172
|
|
|
$db = \Registry::exists('db') ? \Registry::get('db') : null; |
173
|
|
|
if (3 <= $debug && |
174
|
|
|
method_exists($logger, 'write') && |
175
|
|
|
!empty($db) && is_object($db) && method_exists($db, 'log')) { |
176
|
|
|
$logger->write($db->log(), $f3->get('log.date')); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
$execution_time = round(microtime(true) - $f3->get('TIME'), 3); |
180
|
|
|
$params = $f3->get('PARAMS'); |
181
|
|
|
$params = is_array($params) && !empty($params[0]) ? $params[0] : ''; |
182
|
|
|
$logger->write('Script ' . $params . ' executed in ' . $execution_time . ' seconds using ' . |
183
|
|
|
round(memory_get_usage() / 1024 / 1024, 2) . '/' . |
184
|
|
|
round(memory_get_peak_usage() / 1024 / 1024, 2) . ' MB memory/peak', $f3->get('log.date')); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
// http://php.net/manual/en/function.ob-end-flush.php |
188
|
|
|
while (ob_get_level()) { |
189
|
|
|
ob_end_flush(); |
190
|
|
|
flush(); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|