Browse Source

Moving from DBA to Database

pull/7312/head
Philipp Holzer 1 month ago
parent
commit
082634adbc
No account linked to committer's email address

+ 1
- 1
bin/daemon.php View File

@@ -144,7 +144,7 @@ if (!$foreground) {
144 144
 	file_put_contents($pidfile, $pid);
145 145
 
146 146
 	// We lose the database connection upon forking
147
-	Factory\DBFactory::init($a->getConfigCache(), $a->getProfiler(), $_SERVER);
147
+	$a->getDatabase()->reconnect();
148 148
 }
149 149
 
150 150
 Config::set('system', 'worker_daemon_mode', true);

+ 1
- 1
include/dba.php View File

@@ -18,7 +18,7 @@ function q($sql) {
18 18
 	$args = func_get_args();
19 19
 	unset($args[0]);
20 20
 
21
-	if (!DBA::$connected) {
21
+	if (!DBA::connected()) {
22 22
 		return false;
23 23
 	}
24 24
 

+ 17
- 1
src/App.php View File

@@ -12,6 +12,7 @@ use Friendica\Core\Config\Cache\IConfigCache;
12 12
 use Friendica\Core\Config\Configuration;
13 13
 use Friendica\Core\Hook;
14 14
 use Friendica\Core\Theme;
15
+use Friendica\Database\Database;
15 16
 use Friendica\Database\DBA;
16 17
 use Friendica\Model\Profile;
17 18
 use Friendica\Network\HTTPException;
@@ -122,6 +123,11 @@ class App
122 123
 	 */
123 124
 	private $profiler;
124 125
 
126
+	/**
127
+	 * @var Database The Friendica database connection
128
+	 */
129
+	private $database;
130
+
125 131
 	/**
126 132
 	 * Returns the current config cache of this node
127 133
 	 *
@@ -193,6 +199,14 @@ class App
193 199
 		return $this->router;
194 200
 	}
195 201
 
202
+	/**
203
+	 * @return Database
204
+	 */
205
+	public function getDatabase()
206
+	{
207
+		return $this->database;
208
+	}
209
+
196 210
 	/**
197 211
 	 * Register a stylesheet file path to be included in the <head> tag of every page.
198 212
 	 * Inclusion is done in App->initHead().
@@ -232,6 +246,7 @@ class App
232 246
 	/**
233 247
 	 * @brief App constructor.
234 248
 	 *
249
+	 * @param Database $database The Friendica Database
235 250
 	 * @param Configuration    $config    The Configuration
236 251
 	 * @param App\Mode         $mode      The mode of this Friendica app
237 252
 	 * @param App\Router       $router    The router of this Friendica app
@@ -242,10 +257,11 @@ class App
242 257
 	 *
243 258
 	 * @throws Exception if the Basepath is not usable
244 259
 	 */
245
-	public function __construct(Configuration $config, App\Mode $mode, App\Router $router, BaseURL $baseURL, LoggerInterface $logger, Profiler $profiler, $isBackend = true)
260
+	public function __construct(Database $database, Configuration $config, App\Mode $mode, App\Router $router, BaseURL $baseURL, LoggerInterface $logger, Profiler $profiler, $isBackend = true)
246 261
 	{
247 262
 		BaseObject::setApp($this);
248 263
 
264
+		$this->database = $database;
249 265
 		$this->config   = $config;
250 266
 		$this->mode     = $mode;
251 267
 		$this->router   = $router;

+ 93
- 1186
src/Database/DBA.php
File diff suppressed because it is too large
View File


+ 1642
- 0
src/Database/Database.php
File diff suppressed because it is too large
View File


+ 6
- 5
src/Factory/DBFactory.php View File

@@ -17,14 +17,11 @@ class DBFactory
17 17
 	 * @param Profiler           $profiler    The profiler
18 18
 	 * @param array              $server      The $_SERVER variables
19 19
 	 *
20
+	 * @return Database\Database
20 21
 	 * @throws \Exception if connection went bad
21 22
 	 */
22 23
 	public static function init(Cache\IConfigCache $configCache, Profiler $profiler, array $server)
23 24
 	{
24
-		if (Database\DBA::connected()) {
25
-			return;
26
-		}
27
-
28 25
 		$db_host = $configCache->get('database', 'hostname');
29 26
 		$db_user = $configCache->get('database', 'username');
30 27
 		$db_pass = $configCache->get('database', 'password');
@@ -50,11 +47,15 @@ class DBFactory
50 47
 			$db_data = $server['MYSQL_DATABASE'];
51 48
 		}
52 49
 
53
-		if (Database\DBA::connect($configCache, $profiler, new VoidLogger(), $db_host, $db_user, $db_pass, $db_data, $charset)) {
50
+		$database = new Database\Database($configCache, $profiler, new VoidLogger(), $db_host, $db_user, $db_pass, $db_data, $charset);
51
+
52
+		if ($database->connected()) {
54 53
 			// Loads DB_UPDATE_VERSION constant
55 54
 			Database\DBStructure::definition($configCache->get('system', 'basepath'), false);
56 55
 		}
57 56
 
58 57
 		unset($db_host, $db_user, $db_pass, $db_data, $charset);
58
+
59
+		return $database;
59 60
 	}
60 61
 }

+ 3
- 5
src/Factory/DependencyFactory.php View File

@@ -3,7 +3,6 @@
3 3
 namespace Friendica\Factory;
4 4
 
5 5
 use Friendica\App;
6
-use Friendica\Database\DBA;
7 6
 use Friendica\Factory;
8 7
 use Friendica\Util\BasePath;
9 8
 use Friendica\Util\BaseURL;
@@ -30,15 +29,14 @@ class DependencyFactory
30 29
 		$configLoader = new Config\ConfigFileLoader($basePath, $mode);
31 30
 		$configCache = Factory\ConfigFactory::createCache($configLoader);
32 31
 		$profiler = Factory\ProfilerFactory::create($configCache);
33
-		Factory\DBFactory::init($configCache, $profiler, $_SERVER);
32
+		$database = Factory\DBFactory::init($configCache, $profiler, $_SERVER);
34 33
 		$config = Factory\ConfigFactory::createConfig($configCache);
35 34
 		// needed to call PConfig::init()
36 35
 		Factory\ConfigFactory::createPConfig($configCache);
37
-		$logger = Factory\LoggerFactory::create($channel, $config, $profiler);
38
-		DBA::setLogger($logger);
36
+		$logger = Factory\LoggerFactory::create($channel, $database, $config, $profiler);
39 37
 		Factory\LoggerFactory::createDev($channel, $config, $profiler);
40 38
 		$baseURL = new BaseURL($config, $_SERVER);
41 39
 
42
-		return new App($config, $mode, $router, $baseURL, $logger, $profiler, $isBackend);
40
+		return new App($database, $config, $mode, $router, $baseURL, $logger, $profiler, $isBackend);
43 41
 	}
44 42
 }

+ 4
- 1
src/Factory/LoggerFactory.php View File

@@ -4,6 +4,7 @@ namespace Friendica\Factory;
4 4
 
5 5
 use Friendica\Core\Config\Configuration;
6 6
 use Friendica\Core\Logger;
7
+use Friendica\Database\Database;
7 8
 use Friendica\Network\HTTPException\InternalServerErrorException;
8 9
 use Friendica\Util\Introspection;
9 10
 use Friendica\Util\Logger\Monolog\DevelopHandler;
@@ -47,10 +48,11 @@ class LoggerFactory
47 48
 	 * @throws \Exception
48 49
 	 * @throws InternalServerErrorException
49 50
 	 */
50
-	public static function create($channel, Configuration $config, Profiler $profiler)
51
+	public static function create($channel, Database $database, Configuration $config, Profiler $profiler)
51 52
 	{
52 53
 		if (empty($config->get('system', 'debugging', false))) {
53 54
 			$logger = new VoidLogger();
55
+			$database->setLogger($logger);
54 56
 			Logger::init($logger);
55 57
 			return $logger;
56 58
 		}
@@ -101,6 +103,7 @@ class LoggerFactory
101 103
 			$logger = new ProfilerLogger($logger, $profiler);
102 104
 		}
103 105
 
106
+		$database->setLogger($logger);
104 107
 		Logger::init($logger);
105 108
 
106 109
 		return $logger;

+ 3
- 3
tests/include/ApiTest.php View File

@@ -55,12 +55,12 @@ class ApiTest extends DatabaseTest
55 55
 		$configLoader = new ConfigFileLoader($basePath, $mode);
56 56
 		$configCache = Factory\ConfigFactory::createCache($configLoader);
57 57
 		$profiler = Factory\ProfilerFactory::create($configCache);
58
-		Factory\DBFactory::init($configCache, $profiler, $_SERVER);
58
+		$database = Factory\DBFactory::init($configCache, $profiler, $_SERVER);
59 59
 		$config = Factory\ConfigFactory::createConfig($configCache);
60 60
 		Factory\ConfigFactory::createPConfig($configCache);
61
-		$logger = Factory\LoggerFactory::create('test', $config, $profiler);
61
+		$logger = Factory\LoggerFactory::create('test', $database, $config, $profiler);
62 62
 		$baseUrl = new BaseURL($config, $_SERVER);
63
-		$this->app = new App($config, $mode, $router, $baseUrl, $logger, $profiler, false);
63
+		$this->app = new App($database, $config, $mode, $router, $baseUrl, $logger, $profiler, false);
64 64
 
65 65
 		parent::setUp();
66 66
 

+ 3
- 3
tests/src/Database/DBATest.php View File

@@ -20,12 +20,12 @@ class DBATest extends DatabaseTest
20 20
 		$configLoader = new ConfigFileLoader($basePath, $mode);
21 21
 		$configCache = Factory\ConfigFactory::createCache($configLoader);
22 22
 		$profiler = Factory\ProfilerFactory::create($configCache);
23
-		Factory\DBFactory::init($configCache, $profiler, $_SERVER);
23
+		$database = Factory\DBFactory::init($configCache, $profiler, $_SERVER);
24 24
 		$config = Factory\ConfigFactory::createConfig($configCache);
25 25
 		Factory\ConfigFactory::createPConfig($configCache);
26
-		$logger = Factory\LoggerFactory::create('test', $config, $profiler);
26
+		$logger = Factory\LoggerFactory::create('test', $database, $config, $profiler);
27 27
 		$baseUrl = new BaseURL($config, $_SERVER);
28
-		$this->app = new App($config, $mode, $router, $baseUrl, $logger, $profiler, false);
28
+		$this->app = new App($database, $config, $mode, $router, $baseUrl, $logger, $profiler, false);
29 29
 
30 30
 		parent::setUp();
31 31
 

+ 3
- 3
tests/src/Database/DBStructureTest.php View File

@@ -20,12 +20,12 @@ class DBStructureTest extends DatabaseTest
20 20
 		$configLoader = new ConfigFileLoader($basePath, $mode);
21 21
 		$configCache = Factory\ConfigFactory::createCache($configLoader);
22 22
 		$profiler = Factory\ProfilerFactory::create($configCache);
23
-		Factory\DBFactory::init($configCache, $profiler, $_SERVER);
23
+		$database = Factory\DBFactory::init($configCache, $profiler, $_SERVER);
24 24
 		$config = Factory\ConfigFactory::createConfig($configCache);
25 25
 		Factory\ConfigFactory::createPConfig($configCache);
26
-		$logger = Factory\LoggerFactory::create('test', $config, $profiler);
26
+		$logger = Factory\LoggerFactory::create('test', $database, $config, $profiler);
27 27
 		$baseUrl = new BaseURL($config, $_SERVER);
28
-		$this->app = new App($config, $mode, $router, $baseUrl, $logger, $profiler, false);
28
+		$this->app = new App($database, $config, $mode, $router, $baseUrl, $logger, $profiler, false);
29 29
 
30 30
 		parent::setUp();
31 31
 	}

Loading…
Cancel
Save