Conditions | 19 |
Paths | 1440 |
Total Lines | 134 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 | // 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 | |||
194 |