]> git.mxchange.org Git - friendica.git/blob - src/Factory/DBFactory.php
Merge pull request #6987 from tobiasd/20190408-dbupdateemail
[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 string             $basePath    The basepath of the application
15          * @param Cache\IConfigCache $configCache The configuration cache
16          * @param Profiler           $profiler    The profiler
17          * @param array              $server      The $_SERVER variables
18          *
19          * @throws \Exception if connection went bad
20          *
21          * @todo refactor basedir during https://github.com/friendica/friendica/issues/6720
22          */
23         public static function init($basePath, Cache\IConfigCache $configCache, Profiler $profiler, array $server)
24         {
25                 if (Database\DBA::connected()) {
26                         return;
27                 }
28
29                 $db_host = $configCache->get('database', 'hostname');
30                 $db_user = $configCache->get('database', 'username');
31                 $db_pass = $configCache->get('database', 'password');
32                 $db_data = $configCache->get('database', 'database');
33                 $charset = $configCache->get('database', 'charset');
34
35                 // Use environment variables for mysql if they are set beforehand
36                 if (!empty($server['MYSQL_HOST'])
37                         && !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
38                         && $server['MYSQL_PASSWORD'] !== false
39                         && !empty($server['MYSQL_DATABASE']))
40                 {
41                         $db_host = $server['MYSQL_HOST'];
42                         if (!empty($server['MYSQL_PORT'])) {
43                                 $db_host .= ':' . $server['MYSQL_PORT'];
44                         }
45                         if (!empty($server['MYSQL_USERNAME'])) {
46                                 $db_user = $server['MYSQL_USERNAME'];
47                         } else {
48                                 $db_user = $server['MYSQL_USER'];
49                         }
50                         $db_pass = (string) $server['MYSQL_PASSWORD'];
51                         $db_data = $server['MYSQL_DATABASE'];
52                 }
53
54                 if (Database\DBA::connect($basePath, $configCache, $profiler, $db_host, $db_user, $db_pass, $db_data, $charset)) {
55                         // Loads DB_UPDATE_VERSION constant
56                         Database\DBStructure::definition($basePath, false);
57                 }
58
59                 unset($db_host, $db_user, $db_pass, $db_data, $charset);
60         }
61 }