]> git.mxchange.org Git - friendica.git/blob - src/Factory/DBFactory.php
c1a7965013e84a14dff775c365a7d11e2f3a9a85
[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 use Friendica\Util\Profiler;
8
9 class DBFactory
10 {
11         /**
12          * Initialize the DBA connection
13          *
14          * @param Cache\IConfigCache $configCache The configuration cache
15          * @param Profiler          $profiler    The profiler
16          * @param array             $server      The $_SERVER variables
17          *
18          * @throws \Exception if connection went bad
19          */
20         public static function init(Cache\IConfigCache $configCache, Profiler $profiler, array $server)
21         {
22                 if (Database\DBA::connected()) {
23                         return;
24                 }
25
26                 $db_host = $configCache->get('database', 'hostname');
27                 $db_user = $configCache->get('database', 'username');
28                 $db_pass = $configCache->get('database', 'password');
29                 $db_data = $configCache->get('database', 'database');
30                 $charset = $configCache->get('database', 'charset');
31
32                 // Use environment variables for mysql if they are set beforehand
33                 if (!empty($server['MYSQL_HOST'])
34                         && !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
35                         && $server['MYSQL_PASSWORD'] !== false
36                         && !empty($server['MYSQL_DATABASE']))
37                 {
38                         $db_host = $server['MYSQL_HOST'];
39                         if (!empty($server['MYSQL_PORT'])) {
40                                 $db_host .= ':' . $server['MYSQL_PORT'];
41                         }
42                         if (!empty($server['MYSQL_USERNAME'])) {
43                                 $db_user = $server['MYSQL_USERNAME'];
44                         } else {
45                                 $db_user = $server['MYSQL_USER'];
46                         }
47                         $db_pass = (string) $server['MYSQL_PASSWORD'];
48                         $db_data = $server['MYSQL_DATABASE'];
49                 }
50
51                 if (Database\DBA::connect($configCache, $profiler, $db_host, $db_user, $db_pass, $db_data, $charset)) {
52                         // Loads DB_UPDATE_VERSION constant
53                         Database\DBStructure::definition($configCache->get('system', 'basepath'), false);
54                 }
55
56                 unset($db_host, $db_user, $db_pass, $db_data, $charset);
57         }
58 }