Moving from DBA to Database

This commit is contained in:
Philipp Holzer 2019-06-07 00:10:45 +02:00
parent 5e4ace271b
commit 082634adbc
No known key found for this signature in database
GPG Key ID: D8365C3D36B77D90
11 changed files with 1778 additions and 1211 deletions

View File

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

View File

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

View File

@ -12,6 +12,7 @@ use Friendica\Core\Config\Cache\IConfigCache;
use Friendica\Core\Config\Configuration; use Friendica\Core\Config\Configuration;
use Friendica\Core\Hook; use Friendica\Core\Hook;
use Friendica\Core\Theme; use Friendica\Core\Theme;
use Friendica\Database\Database;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Model\Profile; use Friendica\Model\Profile;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
@ -122,6 +123,11 @@ class App
*/ */
private $profiler; private $profiler;
/**
* @var Database The Friendica database connection
*/
private $database;
/** /**
* Returns the current config cache of this node * Returns the current config cache of this node
* *
@ -193,6 +199,14 @@ class App
return $this->router; return $this->router;
} }
/**
* @return Database
*/
public function getDatabase()
{
return $this->database;
}
/** /**
* Register a stylesheet file path to be included in the <head> tag of every page. * Register a stylesheet file path to be included in the <head> tag of every page.
* Inclusion is done in App->initHead(). * Inclusion is done in App->initHead().
@ -232,6 +246,7 @@ class App
/** /**
* @brief App constructor. * @brief App constructor.
* *
* @param Database $database The Friendica Database
* @param Configuration $config The Configuration * @param Configuration $config The Configuration
* @param App\Mode $mode The mode of this Friendica app * @param App\Mode $mode The mode of this Friendica app
* @param App\Router $router The router of this Friendica app * @param App\Router $router The router of this Friendica app
@ -242,10 +257,11 @@ class App
* *
* @throws Exception if the Basepath is not usable * @throws Exception if the Basepath is not usable
*/ */
public function __construct(Configuration $config, App\Mode $mode, App\Router $router, BaseURL $baseURL, LoggerInterface $logger, Profiler $profiler, $isBackend = true) public function __construct(Database $database, Configuration $config, App\Mode $mode, App\Router $router, BaseURL $baseURL, LoggerInterface $logger, Profiler $profiler, $isBackend = true)
{ {
BaseObject::setApp($this); BaseObject::setApp($this);
$this->database = $database;
$this->config = $config; $this->config = $config;
$this->mode = $mode; $this->mode = $mode;
$this->router = $router; $this->router = $router;

File diff suppressed because it is too large Load Diff

1642
src/Database/Database.php Normal file

File diff suppressed because it is too large Load Diff

View File

@ -17,14 +17,11 @@ class DBFactory
* @param Profiler $profiler The profiler * @param Profiler $profiler The profiler
* @param array $server The $_SERVER variables * @param array $server The $_SERVER variables
* *
* @return Database\Database
* @throws \Exception if connection went bad * @throws \Exception if connection went bad
*/ */
public static function init(Cache\IConfigCache $configCache, Profiler $profiler, array $server) public static function init(Cache\IConfigCache $configCache, Profiler $profiler, array $server)
{ {
if (Database\DBA::connected()) {
return;
}
$db_host = $configCache->get('database', 'hostname'); $db_host = $configCache->get('database', 'hostname');
$db_user = $configCache->get('database', 'username'); $db_user = $configCache->get('database', 'username');
$db_pass = $configCache->get('database', 'password'); $db_pass = $configCache->get('database', 'password');
@ -50,11 +47,15 @@ class DBFactory
$db_data = $server['MYSQL_DATABASE']; $db_data = $server['MYSQL_DATABASE'];
} }
if (Database\DBA::connect($configCache, $profiler, new VoidLogger(), $db_host, $db_user, $db_pass, $db_data, $charset)) { $database = new Database\Database($configCache, $profiler, new VoidLogger(), $db_host, $db_user, $db_pass, $db_data, $charset);
if ($database->connected()) {
// Loads DB_UPDATE_VERSION constant // Loads DB_UPDATE_VERSION constant
Database\DBStructure::definition($configCache->get('system', 'basepath'), false); Database\DBStructure::definition($configCache->get('system', 'basepath'), false);
} }
unset($db_host, $db_user, $db_pass, $db_data, $charset); unset($db_host, $db_user, $db_pass, $db_data, $charset);
return $database;
} }
} }

View File

@ -3,7 +3,6 @@
namespace Friendica\Factory; namespace Friendica\Factory;
use Friendica\App; use Friendica\App;
use Friendica\Database\DBA;
use Friendica\Factory; use Friendica\Factory;
use Friendica\Util\BasePath; use Friendica\Util\BasePath;
use Friendica\Util\BaseURL; use Friendica\Util\BaseURL;
@ -30,15 +29,14 @@ class DependencyFactory
$configLoader = new Config\ConfigFileLoader($basePath, $mode); $configLoader = new Config\ConfigFileLoader($basePath, $mode);
$configCache = Factory\ConfigFactory::createCache($configLoader); $configCache = Factory\ConfigFactory::createCache($configLoader);
$profiler = Factory\ProfilerFactory::create($configCache); $profiler = Factory\ProfilerFactory::create($configCache);
Factory\DBFactory::init($configCache, $profiler, $_SERVER); $database = Factory\DBFactory::init($configCache, $profiler, $_SERVER);
$config = Factory\ConfigFactory::createConfig($configCache); $config = Factory\ConfigFactory::createConfig($configCache);
// needed to call PConfig::init() // needed to call PConfig::init()
Factory\ConfigFactory::createPConfig($configCache); Factory\ConfigFactory::createPConfig($configCache);
$logger = Factory\LoggerFactory::create($channel, $config, $profiler); $logger = Factory\LoggerFactory::create($channel, $database, $config, $profiler);
DBA::setLogger($logger);
Factory\LoggerFactory::createDev($channel, $config, $profiler); Factory\LoggerFactory::createDev($channel, $config, $profiler);
$baseURL = new BaseURL($config, $_SERVER); $baseURL = new BaseURL($config, $_SERVER);
return new App($config, $mode, $router, $baseURL, $logger, $profiler, $isBackend); return new App($database, $config, $mode, $router, $baseURL, $logger, $profiler, $isBackend);
} }
} }

View File

@ -4,6 +4,7 @@ namespace Friendica\Factory;
use Friendica\Core\Config\Configuration; use Friendica\Core\Config\Configuration;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Database\Database;
use Friendica\Network\HTTPException\InternalServerErrorException; use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Util\Introspection; use Friendica\Util\Introspection;
use Friendica\Util\Logger\Monolog\DevelopHandler; use Friendica\Util\Logger\Monolog\DevelopHandler;
@ -47,10 +48,11 @@ class LoggerFactory
* @throws \Exception * @throws \Exception
* @throws InternalServerErrorException * @throws InternalServerErrorException
*/ */
public static function create($channel, Configuration $config, Profiler $profiler) public static function create($channel, Database $database, Configuration $config, Profiler $profiler)
{ {
if (empty($config->get('system', 'debugging', false))) { if (empty($config->get('system', 'debugging', false))) {
$logger = new VoidLogger(); $logger = new VoidLogger();
$database->setLogger($logger);
Logger::init($logger); Logger::init($logger);
return $logger; return $logger;
} }
@ -101,6 +103,7 @@ class LoggerFactory
$logger = new ProfilerLogger($logger, $profiler); $logger = new ProfilerLogger($logger, $profiler);
} }
$database->setLogger($logger);
Logger::init($logger); Logger::init($logger);
return $logger; return $logger;

View File

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

View File

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

View File

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