Conditions | 17 |
Paths | 720 |
Total Lines | 120 |
Code Lines | 69 |
Lines | 0 |
Ratio | 0 % |
Changes | 10 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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 | |||
186 |