4) Adding Factories to other entrypoints

This commit is contained in:
Philipp Holzer 2019-02-03 22:46:50 +01:00
parent 4af0119b73
commit 1e0e1674f2
No known key found for this signature in database
GPG key ID: 517BE60E2CE5C8A5
20 changed files with 100 additions and 58 deletions

View file

@ -33,8 +33,10 @@
*/
use Friendica\App;
use Friendica\Core\Config;
use Friendica\Factory;
use Friendica\Util\BasePath;
use Friendica\Util\ExAuth;
use Friendica\Util\LoggerFactory;
if (sizeof($_SERVER["argv"]) == 0) {
die();
@ -52,9 +54,12 @@ chdir($directory);
require dirname(__DIR__) . '/vendor/autoload.php';
$logger = LoggerFactory::create('auth_ejabberd');
$basedir = BasePath::create(dirname(__DIR__));
$configLoader = new Config\ConfigCacheLoader($basedir);
$config = Factory\ConfigFactory::createCache($configLoader);
$logger = Factory\LoggerFactory::create('auth_ejabberd', $config);
$a = new App(dirname(__DIR__), $logger);
$a = new App($config, $logger);
if ($a->getMode()->isNormal()) {
$oAuth = new ExAuth();

View file

@ -3,11 +3,16 @@
require dirname(__DIR__) . '/vendor/autoload.php';
use Friendica\Util\LoggerFactory;
use Friendica\Core\Config;
use Friendica\Factory;
use Friendica\Util\BasePath;
$logger = LoggerFactory::create('console');
$basedir = BasePath::create(dirname(__DIR__));
$configLoader = new Config\ConfigCacheLoader($basedir);
$config = Factory\ConfigFactory::createCache($configLoader);
$logger = Factory\LoggerFactory::create('console', $config);
$a = new Friendica\App(dirname(__DIR__), $logger);
$a = new Friendica\App($config, $logger);
\Friendica\BaseObject::setApp($a);
(new Friendica\Core\Console($argv))->execute();

View file

@ -11,7 +11,8 @@ use Friendica\App;
use Friendica\Core\Config;
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\Util\LoggerFactory;
use Friendica\Factory;
use Friendica\Util\BasePath;
// Get options
$shortopts = 'f';
@ -32,9 +33,12 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
require dirname(__DIR__) . '/vendor/autoload.php';
$logger = LoggerFactory::create('daemon');
$basedir = BasePath::create(dirname(__DIR__));
$configLoader = new Config\ConfigCacheLoader($basedir);
$config = Factory\ConfigFactory::createCache($configLoader);
$logger = Factory\LoggerFactory::create('daemon', $config);
$a = new App(dirname(__DIR__), $logger);
$a = new App($config, $logger);
if ($a->getMode()->isInstall()) {
die("Friendica isn't properly installed yet.\n");

View file

@ -4,11 +4,13 @@
* @file bin/worker.php
* @brief Starts the background processing
*/
use Friendica\App;
use Friendica\Core\Config;
use Friendica\Core\Worker;
use Friendica\Core\Update;
use Friendica\Util\LoggerFactory;
use Friendica\Core\Worker;
use Friendica\Factory;
use Friendica\Util\BasePath;
// Get options
$shortopts = 'sn';
@ -29,12 +31,15 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
require dirname(__DIR__) . '/vendor/autoload.php';
$logger = LoggerFactory::create('worker');
$basedir = BasePath::create(dirname(__DIR__));
$configLoader = new Config\ConfigCacheLoader($basedir);
$config = Factory\ConfigFactory::createCache($configLoader);
$logger = Factory\LoggerFactory::create('worker', $config);
$a = new App(dirname(__DIR__), $logger);
$a = new App($config, $logger);
// Check the database structure and possibly fixes it
Update::check(true);
Update::check($a->getBasePath(), true);
// Quit when in maintenance
if (!$a->getMode()->has(App\Mode::MAINTENANCEDISABLED)) {

View file

@ -916,7 +916,7 @@ function admin_page_summary(App $a)
}
if (Config::get('system', 'dbupdate', DBStructure::UPDATE_NOT_CHECKED) == DBStructure::UPDATE_NOT_CHECKED) {
DBStructure::update(false, true);
DBStructure::update($a->getBasePath(), false, true);
}
if (Config::get('system', 'dbupdate') == DBStructure::UPDATE_FAILED) {
$showwarning = true;
@ -1725,7 +1725,7 @@ function admin_page_dbsync(App $a)
}
if (($a->argc > 2) && (intval($a->argv[2]) || ($a->argv[2] === 'check'))) {
$retval = DBStructure::update(false, true);
$retval = DBStructure::update($a->getBasePath(), false, true);
if ($retval === '') {
$o .= L10n::t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "<br />";
Config::set('database', 'last_successful_update', DB_UPDATE_VERSION);

View file

@ -1288,7 +1288,7 @@ class App
$this->module = 'maintenance';
} else {
$this->checkURL();
Core\Update::check(false);
Core\Update::check($this->basePath, false);
Core\Addon::loadAddons();
Core\Hook::loadHooks();
}

View file

@ -6,7 +6,9 @@ namespace Friendica;
require_once 'boot.php';
use Friendica\Util\LoggerFactory;
use Friendica\Core\Config;
use Friendica\Factory;
use Friendica\Util\BasePath;
/**
* Basic object
@ -28,8 +30,11 @@ class BaseObject
public static function getApp()
{
if (empty(self::$app)) {
$logger = $logger = LoggerFactory::create('app');
self::$app = new App(dirname(__DIR__), $logger);
$basedir = BasePath::create(dirname(__DIR__));
$configLoader = new Config\ConfigCacheLoader($basedir);
$config = Factory\ConfigFactory::createCache($configLoader);
$logger = Factory\LoggerFactory::create('app', $config);
self::$app = new App($config, $logger);
}
return self::$app;

View file

@ -158,7 +158,7 @@ HELP;
$installer->resetChecks();
if (!$installer->installDatabase()) {
if (!$installer->installDatabase($a->getBasePath())) {
$errorMessage = $this->extractErrors($installer->getChecks());
throw new RuntimeException($errorMessage);
}

View file

@ -61,17 +61,19 @@ HELP;
Core\Config::load();
$a = get_app();
switch ($this->getArgument(0)) {
case "dryrun":
$output = DBStructure::update(true, false);
$output = DBStructure::update($a->getBasePath(), true, false);
break;
case "update":
$force = $this->getOption(['f', 'force'], false);
$output = Update::run($force, true, false);
$output = Update::run($a->getBasePath(), $force, true, false);
break;
case "dumpsql":
ob_start();
DBStructure::printStructure();
DBStructure::printStructure($a->getBasePath());
$output = ob_get_clean();
break;
case "toinnodb":

View file

@ -2,8 +2,8 @@
namespace Friendica\Core\Console;
use Friendica\Core\L10n;
use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Core\Update;
/**
@ -56,7 +56,7 @@ HELP;
}
echo L10n::t('Check for pending update actions.') . "\n";
Update::run(true, true, false);
Update::run($a->getBasePath(), true, true, false);
echo L10n::t('Done.') . "\n";
echo L10n::t('Execute pending post updates.') . "\n";

View file

@ -168,12 +168,14 @@ class Installer
/***
* Installs the DB-Scheme for Friendica
*
* @param string $basePath The base path of this application
*
* @return bool true if the installation was successful, otherwise false
* @throws Exception
*/
public function installDatabase()
public function installDatabase($basePath)
{
$result = DBStructure::update(false, true, true);
$result = DBStructure::update($basePath, false, true, true);
if ($result) {
$txt = L10n::t('You may need to import the file "database.sql" manually using phpmyadmin or mysql.') . EOL;

View file

@ -14,10 +14,11 @@ class Update
/**
* @brief Function to check if the Database structure needs an update.
*
* @param string $basePath The base path of this application
* @param boolean $via_worker boolean Is the check run via the worker?
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function check($via_worker)
public static function check($basePath, $via_worker)
{
if (!DBA::connected()) {
return;
@ -38,7 +39,7 @@ class Update
if ($build < DB_UPDATE_VERSION) {
// When we cannot execute the database update via the worker, we will do it directly
if (!Worker::add(PRIORITY_CRITICAL, 'DBUpdate') && $via_worker) {
self::run();
self::run($basePath);
}
}
}
@ -46,6 +47,7 @@ class Update
/**
* Automatic database updates
*
* @param string $basePath The base path of this application
* @param bool $force Force the Update-Check even if the lock is set
* @param bool $verbose Run the Update-Check verbose
* @param bool $sendMail Sends a Mail to the administrator in case of success/failure
@ -53,7 +55,7 @@ class Update
* @return string Empty string if the update is successful, error messages otherwise
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function run($force = false, $verbose = false, $sendMail = true)
public static function run($basePath, $force = false, $verbose = false, $sendMail = true)
{
// In force mode, we release the dbupdate lock first
// Necessary in case of an stuck update
@ -91,7 +93,7 @@ class Update
}
// update the structure in one call
$retval = DBStructure::update($verbose, true);
$retval = DBStructure::update($basePath, $verbose, true);
if ($retval) {
if ($sendMail) {
self::updateFailed(
@ -125,7 +127,7 @@ class Update
}
}
} elseif ($force) {
DBStructure::update($verbose, true);
DBStructure::update($basePath, $verbose, true);
}
return '';

View file

@ -2,7 +2,7 @@
namespace Friendica\Database;
use Friendica\Core\Config\ConfigCache;
use Friendica\Core\Config\IConfigCache;
use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Util\DateTimeFormat;
@ -32,7 +32,7 @@ class DBA
public static $connected = false;
/**
* @var ConfigCache
* @var IConfigCache
*/
private static $config;
private static $server_info = '';
@ -1031,7 +1031,7 @@ class DBA
* This process must only be started once, since the value is cached.
*/
private static function buildRelationData() {
$definition = DBStructure::definition();
$definition = DBStructure::definition(self::$config->get('system', 'basepath'));
foreach ($definition AS $table => $structure) {
foreach ($structure['fields'] AS $field => $field_struct) {

View file

@ -74,9 +74,9 @@ class DBStructure
return L10n::t('Errors encountered performing database changes: ') . $message . EOL;
}
public static function printStructure()
public static function printStructure($basePath)
{
$database = self::definition(false);
$database = self::definition($basePath, false);
echo "-- ------------------------------------------\n";
echo "-- " . FRIENDICA_PLATFORM . " " . FRIENDICA_VERSION . " (" . FRIENDICA_CODENAME, ")\n";
@ -98,14 +98,15 @@ class DBStructure
*
* @see config/dbstructure.config.php
* @param boolean $with_addons_structure Whether to tack on addons additional tables
* @param string $basePath The base path of this application
* @return array
* @throws Exception
*/
public static function definition($basepath, $with_addons_structure = true)
public static function definition($basePath, $with_addons_structure = true)
{
if (!self::$definition) {
$filename = $basepath . '/config/dbstructure.config.php';
$filename = $basePath . '/config/dbstructure.config.php';
if (!is_readable($filename)) {
throw new Exception('Missing database structure config file config/dbstructure.config.php');
@ -246,6 +247,7 @@ class DBStructure
/**
* Updates DB structure and returns eventual errors messages
*
* @param string $basePath The base path of this application
* @param bool $verbose
* @param bool $action Whether to actually apply the update
* @param bool $install Is this the initial update during the installation?
@ -254,7 +256,7 @@ class DBStructure
* @return string Empty string if the update is successful, error messages otherwise
* @throws Exception
*/
public static function update($verbose, $action, $install = false, array $tables = null, array $definition = null)
public static function update($basePath, $verbose, $action, $install = false, array $tables = null, array $definition = null)
{
if ($action && !$install) {
Config::set('system', 'maintenance', 1);
@ -283,7 +285,7 @@ class DBStructure
// Get the definition
if (is_null($definition)) {
$definition = self::definition();
$definition = self::definition($basePath);
}
// MySQL >= 5.7.4 doesn't support the IGNORE keyword in ALTER TABLE statements

View file

@ -6,6 +6,11 @@ use Friendica\Core\Config;
class ConfigFactory
{
/**
* @param Config\ConfigCacheLoader $loader The Config Cache loader (INI/config/.htconfig)
*
* @return Config\ConfigCache
*/
public static function createCache(Config\ConfigCacheLoader $loader)
{
$configCache = new Config\ConfigCache();
@ -17,6 +22,7 @@ class ConfigFactory
/**
* @param string $type The adapter type
* @param Config\IConfigCache $config The config cache of this adapter
*
* @return Config\IConfigAdapter
*/
public static function createConfig($type, $config)
@ -32,6 +38,7 @@ class ConfigFactory
* @param string $type The adapter type
* @param int $uid The UID of the current user
* @param Config\IPConfigCache $config The config cache of this adapter
*
* @return Config\IPConfigAdapter
*/
public static function createPConfig($type, $uid, $config)

View file

@ -7,15 +7,15 @@
namespace Friendica\Model;
use Friendica\BaseObject;
use Friendica\Core\System;
use Friendica\Core\StorageManager;
use Friendica\Core\System;
use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
use Friendica\Model\Storage\IStorage;
use Friendica\Object\Image;
use Friendica\Util\Security;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Mimetype;
use Friendica\Util\Security;
/**
* Class to handle attach dabatase table
@ -31,7 +31,7 @@ class Attach extends BaseObject
*/
private static function getFields()
{
$allfields = DBStructure::definition(false);
$allfields = DBStructure::definition(self::getApp()->getBasePath(), false);
$fields = array_keys($allfields['attach']['fields']);
array_splice($fields, array_search('data', $fields), 1);
return $fields;

View file

@ -103,7 +103,7 @@ class Install extends BaseModule
return;
}
self::$installer->installDatabase();
self::$installer->installDatabase($a->getBasePath());
break;
}

View file

@ -5,12 +5,13 @@
*/
namespace Friendica\Worker;
use Friendica\BaseObject;
use Friendica\Core\Update;
class DBUpdate
class DBUpdate extends BaseObject
{
public static function execute()
{
Update::run();
Update::run(self::getApp()->getBasePath());
}
}

View file

@ -36,7 +36,9 @@ abstract class DatabaseTest extends MockedTest
$this->markTestSkipped('Please set the MYSQL_* environment variables to your test database credentials.');
}
DBA::connect(getenv('MYSQL_HOST'),
DBA::connect(
__DIR__,
getenv('MYSQL_HOST'),
getenv('MYSQL_USERNAME'),
getenv('MYSQL_PASSWORD'),
getenv('MYSQL_DATABASE'));

View file

@ -10,8 +10,8 @@ use Friendica\Core\Config;
use Friendica\Core\PConfig;
use Friendica\Core\Protocol;
use Friendica\Core\System;
use Friendica\Factory\LoggerFactory;
use Friendica\Network\HTTPException;
use Friendica\Util\LoggerFactory;
use Monolog\Handler\TestHandler;
require_once __DIR__ . '/../../include/api.php';