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