Remove DependencyFactory

- Include all necessary classes in `dependencies.config.php`
- Add DI-reference to BaseObject (acts as a global registry)
- Refactor all static "init()" methods to use the global registry

- Refactor Logging for Worker-Logger a little bit
This commit is contained in:
Philipp Holzer 2019-07-21 20:24:16 +02:00
parent 8b344141da
commit 6c2cf494b5
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
21 changed files with 188 additions and 280 deletions

View file

@ -8,6 +8,9 @@
*/
namespace Friendica\Core;
use Friendica\BaseObject;
use Friendica\Core\Config\Configuration;
/**
* @brief Arbitrary system configuration storage
*
@ -15,23 +18,8 @@ namespace Friendica\Core;
* If we ever would decide to return exactly the variable type as entered,
* we will have fun with the additional features. :-)
*/
class Config
class Config extends BaseObject
{
/**
* @var Config\Configuration
*/
private static $config;
/**
* Initialize the config
*
* @param Config\Configuration $config
*/
public static function init(Config\Configuration $config)
{
self::$config = $config;
}
/**
* @brief Loads all configuration values of family into a cached storage.
*
@ -41,7 +29,7 @@ class Config
*/
public static function load($cat = "config")
{
self::$config->load($cat);
self::getClass(Configuration::class)->load($cat);
}
/**
@ -57,7 +45,7 @@ class Config
*/
public static function get($cat, $key, $default_value = null, $refresh = false)
{
return self::$config->get($cat, $key, $default_value, $refresh);
return self::getClass(Configuration::class)->get($cat, $key, $default_value, $refresh);
}
/**
@ -75,7 +63,7 @@ class Config
*/
public static function set($cat, $key, $value)
{
return self::$config->set($cat, $key, $value);
return self::getClass(Configuration::class)->set($cat, $key, $value);
}
/**
@ -88,6 +76,6 @@ class Config
*/
public static function delete($cat, $key)
{
return self::$config->delete($cat, $key);
return self::getClass(Configuration::class)->delete($cat, $key);
}
}

View file

@ -7,10 +7,9 @@ namespace Friendica\Core;
use DOMDocument;
use Exception;
use Friendica\Core\Config\Cache\ConfigCache;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\Database\DBStructure;
use Friendica\Object\Image;
use Friendica\Util\Logger\VoidLogger;
use Friendica\Util\Network;
use Friendica\Util\Profiler;
use Friendica\Util\Strings;
@ -600,9 +599,9 @@ class Installer
*/
public function checkDB(ConfigCache $configCache, Profiler $profiler)
{
$database = new Database($configCache, $profiler, new VoidLogger());
DBA::reconnect();
if ($database->connected()) {
if (DBA::connected()) {
if (DBStructure::existsTable('user')) {
$this->addCheck(L10n::t('Database already in use.'), false, true, '');

View file

@ -4,29 +4,15 @@
*/
namespace Friendica\Core;
use Friendica\BaseObject;
use Friendica\Core\L10n\L10n as L10nClass;
/**
* Provide Language, Translation, and Localization functions to the application
* Localization can be referred to by the numeronym L10N (as in: "L", followed by ten more letters, and then "N").
*/
class L10n
class L10n extends BaseObject
{
/**
* @var L10nClass
*/
private static $l10n;
/**
* Initializes the L10n static wrapper with the instance
*
* @param L10nClass $l10n The l10n class
*/
public static function init(L10nClass $l10n)
{
self::$l10n = $l10n;
}
/**
* Returns the current language code
*
@ -34,7 +20,7 @@ class L10n
*/
public static function getCurrentLang()
{
return self::$l10n->getCurrentLang();
return self::getClass(L10nClass::class)->getCurrentLang();
}
/**
@ -52,7 +38,7 @@ class L10n
*/
public static function pushLang($lang)
{
self::$l10n->pushLang($lang);
self::getClass(L10nClass::class)->pushLang($lang);
}
/**
@ -60,7 +46,7 @@ class L10n
*/
public static function popLang()
{
self::$l10n->popLang();
self::getClass(L10nClass::class)->popLang();
}
/**
@ -81,7 +67,7 @@ class L10n
*/
public static function t($s, ...$vars)
{
return self::$l10n->t($s, ...$vars);
return self::getClass(L10nClass::class)->t($s, ...$vars);
}
/**
@ -105,7 +91,7 @@ class L10n
*/
public static function tt(string $singular, string $plural, int $count)
{
return self::$l10n->tt($singular, $plural, $count);
return self::getClass(L10nClass::class)->tt($singular, $plural, $count);
}
/**
@ -121,7 +107,7 @@ class L10n
*/
public static function getAvailableLanguages()
{
return self::$l10n::getAvailableLanguages();
return L10nClass::getAvailableLanguages();
}
/**
@ -132,7 +118,7 @@ class L10n
*/
public static function getDay($s)
{
return self::$l10n->getDay($s);
return self::getClass(L10nClass::class)->getDay($s);
}
/**
@ -143,6 +129,6 @@ class L10n
*/
public static function getDayShort($s)
{
return self::$l10n->getDayShort($s);
return self::getClass(L10nClass::class)->getDayShort($s);
}
}

View file

@ -59,8 +59,6 @@ class L10n
$this->logger = $logger;
$this->loadTranslationTable(L10n::detectLanguage($config->get('system', 'language', 'en')));
\Friendica\Core\L10n::init($this);
}
/**

View file

@ -4,13 +4,15 @@
*/
namespace Friendica\Core;
use Friendica\BaseObject;
use Friendica\Util\Logger\WorkerLogger;
use Psr\Log\LoggerInterface;
use Psr\Log\LogLevel;
/**
* @brief Logger functions
*/
class Logger
class Logger extends BaseObject
{
/**
* @see Logger::error()
@ -37,6 +39,19 @@ class Logger
*/
const ALL = LogLevel::DEBUG;
/**
* @var LoggerInterface The default Logger type
*/
const TYPE_LOGGER = LoggerInterface::class;
/**
* @var WorkerLogger A specific worker logger type, which can be anabled
*/
const TYPE_WORKER = WorkerLogger::class;
/**
* @var LoggerInterface The current logger type
*/
private static $type = self::TYPE_LOGGER;
/**
* @var array the legacy loglevels
* @deprecated 2019.03 use PSR-3 loglevels
@ -53,33 +68,24 @@ class Logger
];
/**
* @var LoggerInterface A PSR-3 compliant logger instance
*/
private static $logger;
/**
* @var LoggerInterface A PSR-3 compliant logger instance for developing only
*/
private static $devLogger;
/**
* Sets the default logging handler for Friendica.
* Enable additional logging for worker usage
*
* @param LoggerInterface $logger The Logger instance of this Application
* @param string $functionName The worker function, which got called
*
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function init(LoggerInterface $logger)
public static function enableWorker(string $functionName)
{
self::$logger = $logger;
self::$type = self::TYPE_WORKER;
self::getClass(self::$type)->setFunctionName($functionName);
}
/**
* Sets the default dev-logging handler for Friendica.
*
* @param LoggerInterface $logger The Logger instance of this Application
* Disable additional logging for worker usage
*/
public static function setDevLogger(LoggerInterface $logger)
public static function disableWorker()
{
self::$devLogger = $logger;
self::$type = self::TYPE_LOGGER;
}
/**
@ -95,7 +101,7 @@ class Logger
*/
public static function emergency($message, $context = [])
{
self::$logger->emergency($message, $context);
self::getClass(self::$type)->emergency($message, $context);
}
/**
@ -113,7 +119,7 @@ class Logger
*/
public static function alert($message, $context = [])
{
self::$logger->alert($message, $context);
self::getClass(self::$type)->alert($message, $context);
}
/**
@ -130,7 +136,7 @@ class Logger
*/
public static function critical($message, $context = [])
{
self::$logger->critical($message, $context);
self::getClass(self::$type)->critical($message, $context);
}
/**
@ -146,7 +152,7 @@ class Logger
*/
public static function error($message, $context = [])
{
self::$logger->error($message, $context);
self::getClass(self::$type)->error($message, $context);
}
/**
@ -164,7 +170,7 @@ class Logger
*/
public static function warning($message, $context = [])
{
self::$logger->warning($message, $context);
self::getClass(self::$type)->warning($message, $context);
}
/**
@ -179,7 +185,7 @@ class Logger
*/
public static function notice($message, $context = [])
{
self::$logger->notice($message, $context);
self::getClass(self::$type)->notice($message, $context);
}
/**
@ -196,7 +202,7 @@ class Logger
*/
public static function info($message, $context = [])
{
self::$logger->info($message, $context);
self::getClass(self::$type)->info($message, $context);
}
/**
@ -211,7 +217,7 @@ class Logger
*/
public static function debug($message, $context = [])
{
self::$logger->debug($message, $context);
self::getClass(self::$type)->debug($message, $context);
}
/**
@ -225,7 +231,7 @@ class Logger
*/
public static function log($msg, $level = LogLevel::INFO)
{
self::$logger->log($level, $msg);
self::getClass(self::$type)->log($level, $msg);
}
/**
@ -240,10 +246,6 @@ class Logger
*/
public static function devLog($msg, $level = LogLevel::DEBUG)
{
if (!isset(self::$devLogger)) {
return;
}
self::$devLogger->log($level, $msg);
self::getClass('$devLogger')->log($level, $msg);
}
}

View file

@ -8,6 +8,9 @@
*/
namespace Friendica\Core;
use Friendica\BaseObject;
use Friendica\Core\Config\PConfiguration;
/**
* @brief Management of user configuration storage
* Note:
@ -15,23 +18,8 @@ namespace Friendica\Core;
* The PConfig::get() functions return boolean false for keys that are unset,
* and this could lead to subtle bugs.
*/
class PConfig
class PConfig extends BaseObject
{
/**
* @var Config\PConfiguration
*/
private static $config;
/**
* Initialize the config with only the cache
*
* @param Config\PConfiguration $config The configuration cache
*/
public static function init(Config\PConfiguration $config)
{
self::$config = $config;
}
/**
* @brief Loads all configuration values of a user's config family into a cached storage.
*
@ -42,7 +30,7 @@ class PConfig
*/
public static function load(int $uid, string $cat)
{
self::$config->load($uid, $cat);
self::getClass(PConfiguration::class)->load($uid, $cat);
}
/**
@ -59,7 +47,7 @@ class PConfig
*/
public static function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false)
{
return self::$config->get($uid, $cat, $key, $default_value, $refresh);
return self::getClass(PConfiguration::class)->get($uid, $cat, $key, $default_value, $refresh);
}
/**
@ -74,7 +62,7 @@ class PConfig
*/
public static function set(int $uid, string $cat, string $key, $value)
{
return self::$config->set($uid, $cat, $key, $value);
return self::getClass(PConfiguration::class)->set($uid, $cat, $key, $value);
}
/**
@ -88,6 +76,6 @@ class PConfig
*/
public static function delete(int $uid, string $cat, string $key)
{
return self::$config->delete($uid, $cat, $key);
return self::getClass(PConfiguration::class)->delete($uid, $cat, $key);
}
}

View file

@ -8,7 +8,6 @@ use Friendica\BaseObject;
use Friendica\Database\DBA;
use Friendica\Model\Process;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Logger\WorkerLogger;
use Friendica\Util\Network;
/**
@ -378,10 +377,9 @@ class Worker
$argc = count($argv);
$logger = $a->getLogger();
$workerLogger = new WorkerLogger($logger, $funcname);
Logger::enableWorker($funcname);
$workerLogger ->info("Process start.", ['priority' => $queue["priority"], 'id' => $queue["id"]]);
Logger::info("Process start.", ['priority' => $queue["priority"], 'id' => $queue["id"]]);
$stamp = (float)microtime(true);
@ -397,13 +395,13 @@ class Worker
unset($_SESSION);
// Set the workerLogger as new default logger
Logger::init($workerLogger);
if ($method_call) {
call_user_func_array(sprintf('Friendica\Worker\%s::execute', $funcname), $argv);
} else {
$funcname($argv, $argc);
}
Logger::init($logger);
Logger::disableWorker();
unset($a->queue);
@ -423,7 +421,7 @@ class Worker
$rest = round(max(0, $up_duration - (self::$db_duration + self::$lock_duration)), 2);
$exec = round($duration, 2);
$logger->info('Performance:', ['state' => self::$state, 'count' => $dbcount, 'stat' => $dbstat, 'write' => $dbwrite, 'lock' => $dblock, 'total' => $dbtotal, 'rest' => $rest, 'exec' => $exec]);
Logger::info('Performance:', ['state' => self::$state, 'count' => $dbcount, 'stat' => $dbstat, 'write' => $dbwrite, 'lock' => $dblock, 'total' => $dbtotal, 'rest' => $rest, 'exec' => $exec]);
self::$up_start = microtime(true);
self::$db_duration = 0;
@ -433,23 +431,23 @@ class Worker
self::$lock_duration = 0;
if ($duration > 3600) {
$logger->info('Longer than 1 hour.', ['priority' => $queue["priority"], 'id' => $queue["id"], 'duration' => round($duration/60, 3)]);
Logger::info('Longer than 1 hour.', ['priority' => $queue["priority"], 'id' => $queue["id"], 'duration' => round($duration/60, 3)]);
} elseif ($duration > 600) {
$logger->info('Longer than 10 minutes.', ['priority' => $queue["priority"], 'id' => $queue["id"], 'duration' => round($duration/60, 3)]);
Logger::info('Longer than 10 minutes.', ['priority' => $queue["priority"], 'id' => $queue["id"], 'duration' => round($duration/60, 3)]);
} elseif ($duration > 300) {
$logger->info('Longer than 5 minutes.', ['priority' => $queue["priority"], 'id' => $queue["id"], 'duration' => round($duration/60, 3)]);
Logger::info('Longer than 5 minutes.', ['priority' => $queue["priority"], 'id' => $queue["id"], 'duration' => round($duration/60, 3)]);
} elseif ($duration > 120) {
$logger->info('Longer than 2 minutes.', ['priority' => $queue["priority"], 'id' => $queue["id"], 'duration' => round($duration/60, 3)]);
Logger::info('Longer than 2 minutes.', ['priority' => $queue["priority"], 'id' => $queue["id"], 'duration' => round($duration/60, 3)]);
}
$workerLogger->info('Process done.', ['priority' => $queue["priority"], 'id' => $queue["id"], 'duration' => round($duration, 3)]);
Logger::info('Process done.', ['priority' => $queue["priority"], 'id' => $queue["id"], 'duration' => round($duration, 3)]);
$a->getProfiler()->saveLog($a->getLogger(), "ID " . $queue["id"] . ": " . $funcname);
$cooldown = Config::get("system", "worker_cooldown", 0);
if ($cooldown > 0) {
$logger->info('Cooldown.', ['priority' => $queue["priority"], 'id' => $queue["id"], 'cooldown' => $cooldown]);
Logger::info('Cooldown.', ['priority' => $queue["priority"], 'id' => $queue["id"], 'cooldown' => $cooldown]);
sleep($cooldown);
}
}