mirror of https://github.com/friendica/friendica
Initial DB Wrapper
This commit is contained in:
parent
e0911efc87
commit
5275cff11a
|
@ -10,7 +10,6 @@
|
||||||
use Friendica\Core\Config;
|
use Friendica\Core\Config;
|
||||||
use Friendica\Core\Logger;
|
use Friendica\Core\Logger;
|
||||||
use Friendica\Core\Worker;
|
use Friendica\Core\Worker;
|
||||||
use Friendica\Database\DBA;
|
|
||||||
use Friendica\Factory;
|
use Friendica\Factory;
|
||||||
|
|
||||||
// Get options
|
// Get options
|
||||||
|
@ -128,7 +127,7 @@ if (!$foreground) {
|
||||||
// fclose(STDOUT); // file descriptors as we
|
// fclose(STDOUT); // file descriptors as we
|
||||||
// fclose(STDERR); // are running as a daemon.
|
// fclose(STDERR); // are running as a daemon.
|
||||||
|
|
||||||
DBA::disconnect();
|
$a->getDatabase()->disconnect();
|
||||||
|
|
||||||
register_shutdown_function('shutdown');
|
register_shutdown_function('shutdown');
|
||||||
|
|
||||||
|
@ -144,7 +143,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);
|
||||||
|
@ -169,7 +168,7 @@ while (true) {
|
||||||
if ($do_cron) {
|
if ($do_cron) {
|
||||||
// We force a reconnect of the database connection.
|
// We force a reconnect of the database connection.
|
||||||
// This is done to ensure that the connection don't get lost over time.
|
// This is done to ensure that the connection don't get lost over time.
|
||||||
DBA::reconnect();
|
$a->getDatabase()->reconnect();
|
||||||
|
|
||||||
$last_cron = time();
|
$last_cron = time();
|
||||||
}
|
}
|
||||||
|
|
18
src/App.php
18
src/App.php
|
@ -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\InternalServerErrorException;
|
use Friendica\Network\HTTPException\InternalServerErrorException;
|
||||||
|
@ -123,6 +124,19 @@ class App
|
||||||
*/
|
*/
|
||||||
private $logger;
|
private $logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Database The database
|
||||||
|
*/
|
||||||
|
private $database;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Database The database connection of this node
|
||||||
|
*/
|
||||||
|
public function getDatabase()
|
||||||
|
{
|
||||||
|
return $this->database;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Profiler The profiler of this app
|
* @var Profiler The profiler of this app
|
||||||
*/
|
*/
|
||||||
|
@ -238,6 +252,7 @@ class App
|
||||||
* @brief App constructor.
|
* @brief App constructor.
|
||||||
*
|
*
|
||||||
* @param Configuration $config The Configuration
|
* @param Configuration $config The Configuration
|
||||||
|
* @param Database $database The DB instance
|
||||||
* @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
|
||||||
* @param BaseURL $baseURL The full base URL of this Friendica app
|
* @param BaseURL $baseURL The full base URL of this Friendica app
|
||||||
|
@ -247,11 +262,12 @@ 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(Configuration $config, Database $database, App\Mode $mode, App\Router $router, BaseURL $baseURL, LoggerInterface $logger, Profiler $profiler, $isBackend = true)
|
||||||
{
|
{
|
||||||
BaseObject::setApp($this);
|
BaseObject::setApp($this);
|
||||||
|
|
||||||
$this->config = $config;
|
$this->config = $config;
|
||||||
|
$this->database = $database;
|
||||||
$this->mode = $mode;
|
$this->mode = $mode;
|
||||||
$this->router = $router;
|
$this->router = $router;
|
||||||
$this->baseURL = $baseURL;
|
$this->baseURL = $baseURL;
|
||||||
|
|
|
@ -7,6 +7,7 @@ namespace Friendica\Core;
|
||||||
use DOMDocument;
|
use DOMDocument;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Friendica\Core\Config\Cache\IConfigCache;
|
use Friendica\Core\Config\Cache\IConfigCache;
|
||||||
|
use Friendica\Database\Database;
|
||||||
use Friendica\Database\DBA;
|
use Friendica\Database\DBA;
|
||||||
use Friendica\Database\DBStructure;
|
use Friendica\Database\DBStructure;
|
||||||
use Friendica\Object\Image;
|
use Friendica\Object\Image;
|
||||||
|
@ -605,13 +606,15 @@ class Installer
|
||||||
$dbpass = $configCache->get('database', 'password');
|
$dbpass = $configCache->get('database', 'password');
|
||||||
$dbdata = $configCache->get('database', 'database');
|
$dbdata = $configCache->get('database', 'database');
|
||||||
|
|
||||||
if (!DBA::connect($configCache, $profiler, new VoidLogger(), $dbhost, $dbuser, $dbpass, $dbdata)) {
|
$database = new Database($configCache, $profiler, new VoidLogger(), $dbhost, $dbuser, $dbpass, $dbdata);
|
||||||
|
|
||||||
|
if (!$database->connect()) {
|
||||||
$this->addCheck(L10n::t('Could not connect to database.'), false, true, '');
|
$this->addCheck(L10n::t('Could not connect to database.'), false, true, '');
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (DBA::connected()) {
|
if ($database->connected()) {
|
||||||
if (DBStructure::existsTable('user')) {
|
if (DBStructure::existsTable('user')) {
|
||||||
$this->addCheck(L10n::t('Database already in use.'), false, true, '');
|
$this->addCheck(L10n::t('Database already in use.'), false, true, '');
|
||||||
|
|
||||||
|
@ -619,6 +622,8 @@ class Installer
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DBA::init($database);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
1407
src/Database/DBA.php
1407
src/Database/DBA.php
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -16,14 +16,12 @@ 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 create(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');
|
||||||
|
@ -49,11 +47,17 @@ 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);
|
||||||
|
|
||||||
|
Database\DBA::init($database);
|
||||||
|
|
||||||
|
if ($database->connect()) {
|
||||||
// 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,7 @@ 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::create($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);
|
||||||
|
@ -37,6 +37,6 @@ class DependencyFactory
|
||||||
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($config, $database, $mode, $router, $baseURL, $logger, $profiler, $isBackend);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
namespace Friendica\Test;
|
namespace Friendica\Test;
|
||||||
|
|
||||||
use Friendica\App;
|
use Friendica\App;
|
||||||
use Friendica\Database\DBA;
|
use Friendica\Database\Database;
|
||||||
use Friendica\Factory;
|
use Friendica\Factory;
|
||||||
use Friendica\Util\BasePath;
|
use Friendica\Util\BasePath;
|
||||||
use Friendica\Util\Config\ConfigFileLoader;
|
use Friendica\Util\Config\ConfigFileLoader;
|
||||||
|
@ -49,8 +49,7 @@ abstract class DatabaseTest extends MockedTest
|
||||||
|
|
||||||
$profiler = \Mockery::mock(Profiler::class);
|
$profiler = \Mockery::mock(Profiler::class);
|
||||||
|
|
||||||
DBA::connect(
|
$database = new Database($config,
|
||||||
$config,
|
|
||||||
$profiler,
|
$profiler,
|
||||||
new VoidLogger(),
|
new VoidLogger(),
|
||||||
getenv('MYSQL_HOST'),
|
getenv('MYSQL_HOST'),
|
||||||
|
@ -58,11 +57,13 @@ abstract class DatabaseTest extends MockedTest
|
||||||
getenv('MYSQL_PASSWORD'),
|
getenv('MYSQL_PASSWORD'),
|
||||||
getenv('MYSQL_DATABASE'));
|
getenv('MYSQL_DATABASE'));
|
||||||
|
|
||||||
if (!DBA::connected()) {
|
$database->connect();
|
||||||
|
|
||||||
|
if (!$database->connected()) {
|
||||||
$this->markTestSkipped('Could not connect to the database.');
|
$this->markTestSkipped('Could not connect to the database.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->createDefaultDBConnection(DBA::getConnection(), getenv('MYSQL_DATABASE'));
|
return $this->createDefaultDBConnection($database->getConnection(), getenv('MYSQL_DATABASE'));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -55,7 +55,7 @@ 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);
|
Factory\DBFactory::create($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', $config, $profiler);
|
||||||
|
|
|
@ -20,7 +20,7 @@ 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);
|
Factory\DBFactory::create($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', $config, $profiler);
|
||||||
|
|
|
@ -20,7 +20,7 @@ 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);
|
Factory\DBFactory::create($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', $config, $profiler);
|
||||||
|
|
Loading…
Reference in New Issue