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