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\App;
use Friendica\Core\Config;
use Friendica\Factory;
use Friendica\Util\BasePath;
use Friendica\Util\ExAuth; use Friendica\Util\ExAuth;
use Friendica\Util\LoggerFactory;
if (sizeof($_SERVER["argv"]) == 0) { if (sizeof($_SERVER["argv"]) == 0) {
die(); die();
@ -52,9 +54,12 @@ chdir($directory);
require dirname(__DIR__) . '/vendor/autoload.php'; 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()) { if ($a->getMode()->isNormal()) {
$oAuth = new ExAuth(); $oAuth = new ExAuth();

View File

@ -3,11 +3,16 @@
require dirname(__DIR__) . '/vendor/autoload.php'; 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); \Friendica\BaseObject::setApp($a);
(new Friendica\Core\Console($argv))->execute(); (new Friendica\Core\Console($argv))->execute();

View File

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

View File

@ -4,11 +4,13 @@
* @file bin/worker.php * @file bin/worker.php
* @brief Starts the background processing * @brief Starts the background processing
*/ */
use Friendica\App; use Friendica\App;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\Worker;
use Friendica\Core\Update; use Friendica\Core\Update;
use Friendica\Util\LoggerFactory; use Friendica\Core\Worker;
use Friendica\Factory;
use Friendica\Util\BasePath;
// Get options // Get options
$shortopts = 'sn'; $shortopts = 'sn';
@ -29,12 +31,15 @@ if (!file_exists("boot.php") && (sizeof($_SERVER["argv"]) != 0)) {
require dirname(__DIR__) . '/vendor/autoload.php'; 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 // Check the database structure and possibly fixes it
Update::check(true); Update::check($a->getBasePath(), true);
// Quit when in maintenance // Quit when in maintenance
if (!$a->getMode()->has(App\Mode::MAINTENANCEDISABLED)) { 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) { 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) { if (Config::get('system', 'dbupdate') == DBStructure::UPDATE_FAILED) {
$showwarning = true; $showwarning = true;
@ -1725,7 +1725,7 @@ function admin_page_dbsync(App $a)
} }
if (($a->argc > 2) && (intval($a->argv[2]) || ($a->argv[2] === 'check'))) { 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 === '') { if ($retval === '') {
$o .= L10n::t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "<br />"; $o .= L10n::t("Database structure update %s was successfully applied.", DB_UPDATE_VERSION) . "<br />";
Config::set('database', 'last_successful_update', DB_UPDATE_VERSION); Config::set('database', 'last_successful_update', DB_UPDATE_VERSION);

View File

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

View File

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

View File

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

View File

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

View File

@ -2,8 +2,8 @@
namespace Friendica\Core\Console; namespace Friendica\Core\Console;
use Friendica\Core\L10n;
use Friendica\Core\Config; use Friendica\Core\Config;
use Friendica\Core\L10n;
use Friendica\Core\Update; use Friendica\Core\Update;
/** /**
@ -56,7 +56,7 @@ HELP;
} }
echo L10n::t('Check for pending update actions.') . "\n"; 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('Done.') . "\n";
echo L10n::t('Execute pending post updates.') . "\n"; echo L10n::t('Execute pending post updates.') . "\n";

View File

@ -168,12 +168,14 @@ class Installer
/*** /***
* Installs the DB-Scheme for Friendica * 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 * @return bool true if the installation was successful, otherwise false
* @throws Exception * @throws Exception
*/ */
public function installDatabase() public function installDatabase($basePath)
{ {
$result = DBStructure::update(false, true, true); $result = DBStructure::update($basePath, false, true, true);
if ($result) { if ($result) {
$txt = L10n::t('You may need to import the file "database.sql" manually using phpmyadmin or mysql.') . EOL; $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. * @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? * @param boolean $via_worker boolean Is the check run via the worker?
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/ */
public static function check($via_worker) public static function check($basePath, $via_worker)
{ {
if (!DBA::connected()) { if (!DBA::connected()) {
return; return;
@ -38,7 +39,7 @@ class Update
if ($build < DB_UPDATE_VERSION) { if ($build < DB_UPDATE_VERSION) {
// When we cannot execute the database update via the worker, we will do it directly // When we cannot execute the database update via the worker, we will do it directly
if (!Worker::add(PRIORITY_CRITICAL, 'DBUpdate') && $via_worker) { if (!Worker::add(PRIORITY_CRITICAL, 'DBUpdate') && $via_worker) {
self::run(); self::run($basePath);
} }
} }
} }
@ -46,14 +47,15 @@ class Update
/** /**
* Automatic database updates * Automatic database updates
* *
* @param bool $force Force the Update-Check even if the lock is set * @param string $basePath The base path of this application
* @param bool $verbose Run the Update-Check verbose * @param bool $force Force the Update-Check even if the lock is set
* @param bool $sendMail Sends a Mail to the administrator in case of success/failure * @param bool $verbose Run the Update-Check verbose
* @param bool $sendMail Sends a Mail to the administrator in case of success/failure
* *
* @return string Empty string if the update is successful, error messages otherwise * @return string Empty string if the update is successful, error messages otherwise
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @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 // In force mode, we release the dbupdate lock first
// Necessary in case of an stuck update // Necessary in case of an stuck update
@ -91,7 +93,7 @@ class Update
} }
// update the structure in one call // update the structure in one call
$retval = DBStructure::update($verbose, true); $retval = DBStructure::update($basePath, $verbose, true);
if ($retval) { if ($retval) {
if ($sendMail) { if ($sendMail) {
self::updateFailed( self::updateFailed(
@ -125,7 +127,7 @@ class Update
} }
} }
} elseif ($force) { } elseif ($force) {
DBStructure::update($verbose, true); DBStructure::update($basePath, $verbose, true);
} }
return ''; return '';

View File

@ -2,7 +2,7 @@
namespace Friendica\Database; namespace Friendica\Database;
use Friendica\Core\Config\ConfigCache; use Friendica\Core\Config\IConfigCache;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Core\System; use Friendica\Core\System;
use Friendica\Util\DateTimeFormat; use Friendica\Util\DateTimeFormat;
@ -32,7 +32,7 @@ class DBA
public static $connected = false; public static $connected = false;
/** /**
* @var ConfigCache * @var IConfigCache
*/ */
private static $config; private static $config;
private static $server_info = ''; private static $server_info = '';
@ -1031,7 +1031,7 @@ class DBA
* This process must only be started once, since the value is cached. * This process must only be started once, since the value is cached.
*/ */
private static function buildRelationData() { private static function buildRelationData() {
$definition = DBStructure::definition(); $definition = DBStructure::definition(self::$config->get('system', 'basepath'));
foreach ($definition AS $table => $structure) { foreach ($definition AS $table => $structure) {
foreach ($structure['fields'] AS $field => $field_struct) { 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; 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 "-- ------------------------------------------\n";
echo "-- " . FRIENDICA_PLATFORM . " " . FRIENDICA_VERSION . " (" . FRIENDICA_CODENAME, ")\n"; echo "-- " . FRIENDICA_PLATFORM . " " . FRIENDICA_VERSION . " (" . FRIENDICA_CODENAME, ")\n";
@ -98,14 +98,15 @@ class DBStructure
* *
* @see config/dbstructure.config.php * @see config/dbstructure.config.php
* @param boolean $with_addons_structure Whether to tack on addons additional tables * @param boolean $with_addons_structure Whether to tack on addons additional tables
* @param string $basePath The base path of this application
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
public static function definition($basepath, $with_addons_structure = true) public static function definition($basePath, $with_addons_structure = true)
{ {
if (!self::$definition) { if (!self::$definition) {
$filename = $basepath . '/config/dbstructure.config.php'; $filename = $basePath . '/config/dbstructure.config.php';
if (!is_readable($filename)) { if (!is_readable($filename)) {
throw new Exception('Missing database structure config file config/dbstructure.config.php'); throw new Exception('Missing database structure config file config/dbstructure.config.php');
@ -246,15 +247,16 @@ class DBStructure
/** /**
* Updates DB structure and returns eventual errors messages * Updates DB structure and returns eventual errors messages
* *
* @param bool $verbose * @param string $basePath The base path of this application
* @param bool $action Whether to actually apply the update * @param bool $verbose
* @param bool $install Is this the initial update during the installation? * @param bool $action Whether to actually apply the update
* @param array $tables An array of the database tables * @param bool $install Is this the initial update during the installation?
* @param array $definition An array of the definition tables * @param array $tables An array of the database tables
* @param array $definition An array of the definition tables
* @return string Empty string if the update is successful, error messages otherwise * @return string Empty string if the update is successful, error messages otherwise
* @throws Exception * @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) { if ($action && !$install) {
Config::set('system', 'maintenance', 1); Config::set('system', 'maintenance', 1);
@ -283,7 +285,7 @@ class DBStructure
// Get the definition // Get the definition
if (is_null($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 // 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 class ConfigFactory
{ {
/**
* @param Config\ConfigCacheLoader $loader The Config Cache loader (INI/config/.htconfig)
*
* @return Config\ConfigCache
*/
public static function createCache(Config\ConfigCacheLoader $loader) public static function createCache(Config\ConfigCacheLoader $loader)
{ {
$configCache = new Config\ConfigCache(); $configCache = new Config\ConfigCache();
@ -17,6 +22,7 @@ class ConfigFactory
/** /**
* @param string $type The adapter type * @param string $type The adapter type
* @param Config\IConfigCache $config The config cache of this adapter * @param Config\IConfigCache $config The config cache of this adapter
*
* @return Config\IConfigAdapter * @return Config\IConfigAdapter
*/ */
public static function createConfig($type, $config) public static function createConfig($type, $config)
@ -32,6 +38,7 @@ class ConfigFactory
* @param string $type The adapter type * @param string $type The adapter type
* @param int $uid The UID of the current user * @param int $uid The UID of the current user
* @param Config\IPConfigCache $config The config cache of this adapter * @param Config\IPConfigCache $config The config cache of this adapter
*
* @return Config\IPConfigAdapter * @return Config\IPConfigAdapter
*/ */
public static function createPConfig($type, $uid, $config) public static function createPConfig($type, $uid, $config)

View File

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

View File

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

View File

@ -5,12 +5,13 @@
*/ */
namespace Friendica\Worker; namespace Friendica\Worker;
use Friendica\BaseObject;
use Friendica\Core\Update; use Friendica\Core\Update;
class DBUpdate class DBUpdate extends BaseObject
{ {
public static function execute() 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.'); $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_USERNAME'),
getenv('MYSQL_PASSWORD'), getenv('MYSQL_PASSWORD'),
getenv('MYSQL_DATABASE')); getenv('MYSQL_DATABASE'));

View File

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