|
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
|
|
|
// set home directory for project |
|
39
|
|
|
$f3->set('HOMEDIR', realpath($f3->get('SERVER.DOCUMENT_ROOT') . '/../')); |
|
40
|
|
|
|
|
41
|
|
|
// make sure directories are full, not relative path |
|
42
|
|
|
foreach (['LOGS', 'TEMP', 'UPLOADS'] as $key) { |
|
43
|
|
|
$dir = $f3->get($key); |
|
44
|
|
|
if (!empty($dir)) { |
|
45
|
|
|
$dir = realpath($dir); |
|
46
|
|
|
$f3->set($key, $dir . '/'); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// these take multiple paths |
|
51
|
|
|
$language = $f3->get('LANGUAGE'); |
|
52
|
|
|
foreach (['LOCALES', 'UI'] as $key) { |
|
53
|
|
|
$paths = $f3->get($key); |
|
54
|
|
|
// remove any invalid dirs |
|
55
|
|
|
if (!empty($paths)) { |
|
56
|
|
|
$dirs = $f3->split($paths); |
|
57
|
|
|
foreach ($dirs as $k => $dir) { |
|
58
|
|
|
if (empty($dir)) { |
|
59
|
|
|
unset($dirs[$k]); |
|
60
|
|
|
continue; |
|
61
|
|
|
} |
|
62
|
|
|
// switch for different language templates |
|
63
|
|
|
$langDir = ''; |
|
64
|
|
|
if ('UI' == $key) { |
|
65
|
|
|
$langDir = 'templates/' . $language . '/' . $dir; |
|
66
|
|
|
$dir = 'templates/en/' . $dir; |
|
67
|
|
|
if (!file_exists($langDir)) { |
|
68
|
|
|
unset($langDir); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
if (!empty($langDir)) { |
|
72
|
|
|
$dirs[$k] = realpath($langDir) . '/' . ';' . realpath($dir) . '/'; |
|
73
|
|
|
} else { |
|
74
|
|
|
$dirs[$k] = realpath($dir) . '/'; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
$f3->set($key, join(';', $dirs)); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// enable full logging if not production |
|
82
|
|
|
$logfile = $f3->get('log.file'); |
|
83
|
|
|
if (empty($logfile)) { |
|
84
|
|
|
$f3->set('log.file', '/dev/null'); |
|
85
|
|
|
} elseif ('production' !== $f3->get('app.env')) { |
|
86
|
|
|
ini_set('log_errors', 'On'); |
|
87
|
|
|
$logfile = $f3->get('LOGS') . $logfile; |
|
88
|
|
|
$f3->set('logfile', $logfile); |
|
89
|
|
|
ini_set('error_log', $logfile); |
|
90
|
|
|
ini_set('error_reporting', -1); |
|
91
|
|
|
ini_set('ignore_repeated_errors', 'On'); |
|
92
|
|
|
ini_set('ignore_repeated_source', 'On'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// parse params for http-style dsn |
|
96
|
|
|
// setup database connection params |
|
97
|
|
|
// @see http://fatfreeframework.com/databases |
|
98
|
|
|
$httpDSN = $f3->get('db.dsn_http'); |
|
99
|
|
|
if (!empty($httpDSN)) { |
|
100
|
|
|
$dbParams = $f3->get('db'); |
|
101
|
|
|
$params = \FFMVC\Helpers\DB::instance()->parseHttpDsn($httpDSN); |
|
102
|
|
|
$params['dsn'] = \FFMVC\Helpers\DB::instance()->createDbDsn($params); |
|
103
|
|
|
$dbParams = array_merge($dbParams, $params); |
|
104
|
|
|
$f3->set('db', $dbParams); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// setup outgoing email server for php mail command |
|
108
|
|
|
ini_set('SMTP', $f3->get('email.host')); |
|
109
|
|
|
ini_set('sendmail_from', $f3->get('email.from')); |
|
110
|
|
|
ini_set('smtp_port', $f3->get('email.port')); |
|
111
|
|
|
ini_set('user', $f3->get('email.user')); |
|
112
|
|
|
ini_set('password', $f3->get('email.pass')); |
|
113
|
|
|
|
|
114
|
|
|
if (empty($f3->get('CLI'))) { |
|
115
|
|
|
return; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
// log errors if run on command line |
|
119
|
|
|
ini_set('display_errors', 'On'); |
|
120
|
|
|
ini_set('error_log', 'On'); |
|
121
|
|
|
ini_set('html_errors', 'Off'); |
|
122
|
|
|
|
|
123
|
|
|
// set default error handler output for CLI mode |
|
124
|
|
|
$f3->set('ONERROR', function ($f3) { |
|
125
|
|
|
$e = $f3->get('ERROR'); |
|
126
|
|
|
|
|
127
|
|
|
// detailed error notifications because it's not public |
|
128
|
|
|
$errorMessage = sprintf("Trace: %s\n\nException %d: %s\n%s\n", |
|
129
|
|
|
print_r($f3->trace(null, false), 1), $e['code'], $e['status'], $e['text'] |
|
130
|
|
|
); |
|
131
|
|
|
|
|
132
|
|
|
echo $errorMessage; |
|
133
|
|
|
|
|
134
|
|
|
if (\Registry::exists('logger')) { |
|
135
|
|
|
$logger = \Registry::get('logger'); |
|
136
|
|
|
if (is_object($logger)) { |
|
137
|
|
|
$logger->write($errorMessage, $f3->get('log.date')); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
}); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* tasks for the application once run. |
|
145
|
|
|
* |
|
146
|
|
|
* @return void |
|
147
|
|
|
*/ |
|
148
|
|
|
public static function finish() |
|
149
|
|
|
{ |
|
150
|
|
|
// log script execution time if debugging |
|
151
|
|
|
$f3 = \Base::instance(); |
|
152
|
|
|
$debug = $f3->get('DEBUG'); |
|
153
|
|
|
|
|
154
|
|
|
if (\Registry::exists('logger')) { |
|
155
|
|
|
$logger = \Registry::get('logger'); |
|
156
|
|
|
} else { |
|
157
|
|
|
$logger = null; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
if (!empty($logger) && is_object($logger) && $debug || 'production' !== $f3->get('app.env')) { |
|
161
|
|
|
|
|
162
|
|
|
// log database transactions if level 3 |
|
163
|
|
|
$db = \Registry::get('db'); |
|
164
|
|
|
|
|
165
|
|
|
if (3 <= $debug && |
|
166
|
|
|
method_exists($logger, 'write') && |
|
167
|
|
|
method_exists($db, 'log')) { |
|
168
|
|
|
$logger->write($db->log(), $f3->get('log.date')); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$execution_time = round(microtime(true) - $f3->get('TIME'), 3); |
|
172
|
|
|
$params = $f3->get('PARAMS'); |
|
173
|
|
|
$params = is_array($params) && !empty($params[0]) ? $params[0] : ''; |
|
174
|
|
|
$logger->write('Script ' . $params . ' executed in ' . $execution_time . ' seconds using ' . |
|
175
|
|
|
round(memory_get_usage() / 1024 / 1024, 2) . '/' . |
|
176
|
|
|
round(memory_get_peak_usage() / 1024 / 1024, 2) . ' MB memory/peak', $f3->get('log.date')); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
// http://php.net/manual/en/function.ob-end-flush.php |
|
180
|
|
|
while (ob_get_level()) { |
|
181
|
|
|
@ob_end_flush(); |
|
182
|
|
|
@flush(); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
} |
|
186
|
|
|
|