]> git.mxchange.org Git - friendica.git/blob - src/Factory/DBFactory.php
5970541c105d19eb544cd11c5e01816f63c96c0f
[friendica.git] / src / Factory / DBFactory.php
1 <?php
2
3 namespace Friendica\Factory;
4
5 use Friendica\Core\Config\Cache;
6 use Friendica\Database;
7
8 class DBFactory
9 {
10         public static function init(Cache\ConfigCache $configCache, array $server)
11         {
12                 if (Database\DBA::connected()) {
13                         return;
14                 }
15
16                 $db_host = $configCache->get('database', 'hostname');
17                 $db_user = $configCache->get('database', 'username');
18                 $db_pass = $configCache->get('database', 'password');
19                 $db_data = $configCache->get('database', 'database');
20                 $charset = $configCache->get('database', 'charset');
21
22                 // Use environment variables for mysql if they are set beforehand
23                 if (!empty($server['MYSQL_HOST'])
24                         && !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
25                         && $server['MYSQL_PASSWORD'] !== false
26                         && !empty($server['MYSQL_DATABASE']))
27                 {
28                         $db_host = $server['MYSQL_HOST'];
29                         if (!empty($server['MYSQL_PORT'])) {
30                                 $db_host .= ':' . $server['MYSQL_PORT'];
31                         }
32                         if (!empty($server['MYSQL_USERNAME'])) {
33                                 $db_user = $server['MYSQL_USERNAME'];
34                         } else {
35                                 $db_user = $server['MYSQL_USER'];
36                         }
37                         $db_pass = (string) $server['MYSQL_PASSWORD'];
38                         $db_data = $server['MYSQL_DATABASE'];
39                 }
40
41                 if (Database\DBA::connect($configCache, $db_host, $db_user, $db_pass, $db_data, $charset)) {
42                         // Loads DB_UPDATE_VERSION constant
43                         Database\DBStructure::definition($configCache->get('system', 'basepath'), false);
44                 }
45
46                 unset($db_host, $db_user, $db_pass, $db_data, $charset);
47         }
48 }