]> git.mxchange.org Git - friendica.git/blob - src/Factory/DBFactory.php
Adding basepath, urlpath, hostname and ssl_policy to installation
[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 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         public static function init(Cache\IConfigCache $configCache, Profiler $profiler, array $server)
22         {
23                 if (Database\DBA::connected()) {
24                         return;
25                 }
26
27                 $db_host = $configCache->get('database', 'hostname');
28                 $db_user = $configCache->get('database', 'username');
29                 $db_pass = $configCache->get('database', 'password');
30                 $db_data = $configCache->get('database', 'database');
31                 $charset = $configCache->get('database', 'charset');
32
33                 // Use environment variables for mysql if they are set beforehand
34                 if (!empty($server['MYSQL_HOST'])
35                         && !empty($server['MYSQL_USERNAME'] || !empty($server['MYSQL_USER']))
36                         && $server['MYSQL_PASSWORD'] !== false
37                         && !empty($server['MYSQL_DATABASE']))
38                 {
39                         $db_host = $server['MYSQL_HOST'];
40                         if (!empty($server['MYSQL_PORT'])) {
41                                 $db_host .= ':' . $server['MYSQL_PORT'];
42                         }
43                         if (!empty($server['MYSQL_USERNAME'])) {
44                                 $db_user = $server['MYSQL_USERNAME'];
45                         } else {
46                                 $db_user = $server['MYSQL_USER'];
47                         }
48                         $db_pass = (string) $server['MYSQL_PASSWORD'];
49                         $db_data = $server['MYSQL_DATABASE'];
50                 }
51
52                 if (Database\DBA::connect($configCache, $profiler, new VoidLogger(), $db_host, $db_user, $db_pass, $db_data, $charset)) {
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 }