From acf52a9783c1c56ca740ef903388ab254043340d Mon Sep 17 00:00:00 2001 From: Philipp Date: Wed, 26 Jul 2023 23:02:09 +0200 Subject: [PATCH] Added a lot of constants :-) --- src/Core/Cache/Enum/Type.php | 35 ------------------- src/Core/Cache/Factory/Cache.php | 2 +- src/Core/Cache/Type/MemcacheCache.php | 2 +- src/Core/Hooks/Util/StrategiesFileManager.php | 5 +++ .../Type/AbstractKeyValueStorage.php | 2 ++ .../Type/DBKeyValueStorage.php | 1 + src/Core/Lock/Enum/Type.php | 4 +-- src/Core/Lock/Factory/Lock.php | 26 +++++++------- src/Core/Logger/Type/AbstractLogger.php | 2 ++ src/Core/Logger/Type/StreamLogger.php | 2 ++ src/Core/Logger/Type/SyslogLogger.php | 2 ++ .../PConfig/Type/AbstractPConfigValues.php | 2 ++ src/Core/PConfig/Type/JitPConfig.php | 2 ++ src/Core/PConfig/Type/PreloadPConfig.php | 2 ++ src/Core/Session/Factory/Session.php | 4 +-- static/strategies.config.php | 23 ++++++------ 16 files changed, 51 insertions(+), 65 deletions(-) delete mode 100644 src/Core/Cache/Enum/Type.php diff --git a/src/Core/Cache/Enum/Type.php b/src/Core/Cache/Enum/Type.php deleted file mode 100644 index 98ed9e41f7..0000000000 --- a/src/Core/Cache/Enum/Type.php +++ /dev/null @@ -1,35 +0,0 @@ -. - * - */ - -namespace Friendica\Core\Cache\Enum; - -/** - * Enumeration for cache types - */ -abstract class Type -{ - const APCU = 'apcu'; - const REDIS = 'redis'; - const ARRAY = 'array'; - const MEMCACHE = 'memcache'; - const DATABASE = 'database'; - const MEMCACHED = 'memcached'; -} diff --git a/src/Core/Cache/Factory/Cache.php b/src/Core/Cache/Factory/Cache.php index 000233df7f..189c574aac 100644 --- a/src/Core/Cache/Factory/Cache.php +++ b/src/Core/Cache/Factory/Cache.php @@ -42,7 +42,7 @@ class Cache /** * @var string The default cache if nothing set */ - const DEFAULT_TYPE = Enum\Type::DATABASE; + const DEFAULT_TYPE = Type\DatabaseCache::NAME; /** @var ICanCreateInstances */ protected $instanceCreator; /** @var IManageConfigValues */ diff --git a/src/Core/Cache/Type/MemcacheCache.php b/src/Core/Cache/Type/MemcacheCache.php index fccaff6c00..e8d3b07c8e 100644 --- a/src/Core/Cache/Type/MemcacheCache.php +++ b/src/Core/Cache/Type/MemcacheCache.php @@ -33,7 +33,7 @@ use Memcache; */ class MemcacheCache extends AbstractCache implements ICanCacheInMemory { - const NAME = 'memcached'; + const NAME = 'memcache'; use CompareSetTrait; use CompareDeleteTrait; diff --git a/src/Core/Hooks/Util/StrategiesFileManager.php b/src/Core/Hooks/Util/StrategiesFileManager.php index d6651ac981..860166fb64 100644 --- a/src/Core/Hooks/Util/StrategiesFileManager.php +++ b/src/Core/Hooks/Util/StrategiesFileManager.php @@ -30,6 +30,11 @@ use Friendica\Core\Hooks\Exceptions\HookConfigException; */ class StrategiesFileManager { + /** + * The default hook-file-key of strategies + * -> it's an empty string to cover empty/missing config values + */ + const STRATEGY_DEFAULT_KEY = ''; const STATIC_DIR = 'static'; const CONFIG_NAME = 'strategies'; diff --git a/src/Core/KeyValueStorage/Type/AbstractKeyValueStorage.php b/src/Core/KeyValueStorage/Type/AbstractKeyValueStorage.php index 6b16665276..5d1b1f9d45 100644 --- a/src/Core/KeyValueStorage/Type/AbstractKeyValueStorage.php +++ b/src/Core/KeyValueStorage/Type/AbstractKeyValueStorage.php @@ -28,6 +28,8 @@ use Friendica\Core\KeyValueStorage\Capability\IManageKeyValuePairs; */ abstract class AbstractKeyValueStorage implements IManageKeyValuePairs { + const NAME = ''; + /** {@inheritDoc} */ public function get(string $key) { diff --git a/src/Core/KeyValueStorage/Type/DBKeyValueStorage.php b/src/Core/KeyValueStorage/Type/DBKeyValueStorage.php index d31f3c1ced..cc152f0c99 100644 --- a/src/Core/KeyValueStorage/Type/DBKeyValueStorage.php +++ b/src/Core/KeyValueStorage/Type/DBKeyValueStorage.php @@ -30,6 +30,7 @@ use Friendica\Database\Database; */ class DBKeyValueStorage extends AbstractKeyValueStorage { + const NAME = 'database'; const DB_KEY_VALUE_TABLE = 'key-value'; /** @var Database */ diff --git a/src/Core/Lock/Enum/Type.php b/src/Core/Lock/Enum/Type.php index 9e6e9194df..3a623b6372 100644 --- a/src/Core/Lock/Enum/Type.php +++ b/src/Core/Lock/Enum/Type.php @@ -21,7 +21,7 @@ namespace Friendica\Core\Lock\Enum; -use Friendica\Core\Cache\Enum\Type as CacheType; +use Friendica\Core\Cache\Type\DatabaseCache; /** * Enumeration for lock types @@ -30,6 +30,6 @@ use Friendica\Core\Cache\Enum\Type as CacheType; */ abstract class Type { - const DATABASE = CacheType::DATABASE; + const DATABASE = DatabaseCache::NAME; const SEMAPHORE = 'semaphore'; } diff --git a/src/Core/Lock/Factory/Lock.php b/src/Core/Lock/Factory/Lock.php index bc4650c97b..a60bc5a96c 100644 --- a/src/Core/Lock/Factory/Lock.php +++ b/src/Core/Lock/Factory/Lock.php @@ -23,10 +23,10 @@ namespace Friendica\Core\Lock\Factory; use Friendica\Core\Cache\Factory\Cache; use Friendica\Core\Cache\Capability\ICanCacheInMemory; -use Friendica\Core\Cache\Enum; +use Friendica\Core\Cache\Type as CacheType; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Lock\Capability\ICanLock; -use Friendica\Core\Lock\Type; +use Friendica\Core\Lock\Type as LockType; use Friendica\Database\Database; use Psr\Log\LoggerInterface; @@ -78,20 +78,20 @@ class Lock try { switch ($lock_type) { - case Enum\Type::MEMCACHE: - case Enum\Type::MEMCACHED: - case Enum\Type::REDIS: - case Enum\Type::APCU: + case CacheType\MemcacheCache::NAME: + case CacheType\MemcachedCache::NAME: + case CacheType\RedisCache::NAME: + case CacheType\APCuCache::NAME: $cache = $this->cacheFactory->createLocal($lock_type); if ($cache instanceof ICanCacheInMemory) { - return new Type\CacheLock($cache); + return new LockType\CacheLock($cache); } else { throw new \Exception(sprintf('Incompatible cache driver \'%s\' for lock used', $lock_type)); } case 'database': - return new Type\DatabaseLock($this->dba); + return new LockType\DatabaseLock($this->dba); case 'semaphore': - return new Type\SemaphoreLock(); + return new LockType\SemaphoreLock(); default: return self::useAutoDriver(); } @@ -116,7 +116,7 @@ class Lock // 1. Try to use Semaphores for - local - locking if (function_exists('sem_get')) { try { - return new Type\SemaphoreLock(); + return new LockType\SemaphoreLock(); } catch (\Exception $exception) { $this->logger->warning('Using Semaphore driver for locking failed.', ['exception' => $exception]); } @@ -124,11 +124,11 @@ class Lock // 2. Try to use Cache Locking (don't use the DB-Cache Locking because it works different!) $cache_type = $this->config->get('system', 'cache_driver', 'database'); - if ($cache_type != Enum\Type::DATABASE) { + if ($cache_type != CacheType\DatabaseCache::NAME) { try { $cache = $this->cacheFactory->createLocal($cache_type); if ($cache instanceof ICanCacheInMemory) { - return new Type\CacheLock($cache); + return new LockType\CacheLock($cache); } } catch (\Exception $exception) { $this->logger->warning('Using Cache driver for locking failed.', ['exception' => $exception]); @@ -136,6 +136,6 @@ class Lock } // 3. Use Database Locking as a Fallback - return new Type\DatabaseLock($this->dba); + return new LockType\DatabaseLock($this->dba); } } diff --git a/src/Core/Logger/Type/AbstractLogger.php b/src/Core/Logger/Type/AbstractLogger.php index e592ee86d6..7de0e41605 100644 --- a/src/Core/Logger/Type/AbstractLogger.php +++ b/src/Core/Logger/Type/AbstractLogger.php @@ -38,6 +38,8 @@ use Psr\Log\LogLevel; */ abstract class AbstractLogger implements LoggerInterface { + const NAME = ''; + /** * The output channel of this logger * @var string diff --git a/src/Core/Logger/Type/StreamLogger.php b/src/Core/Logger/Type/StreamLogger.php index e09a320473..8cadd8cc72 100644 --- a/src/Core/Logger/Type/StreamLogger.php +++ b/src/Core/Logger/Type/StreamLogger.php @@ -32,6 +32,8 @@ use Psr\Log\LogLevel; */ class StreamLogger extends AbstractLogger { + const NAME = 'stream'; + /** * The minimum loglevel at which this logger will be triggered * @var string diff --git a/src/Core/Logger/Type/SyslogLogger.php b/src/Core/Logger/Type/SyslogLogger.php index fb8cb97ae7..556351a7e4 100644 --- a/src/Core/Logger/Type/SyslogLogger.php +++ b/src/Core/Logger/Type/SyslogLogger.php @@ -32,6 +32,8 @@ use Psr\Log\LogLevel; */ class SyslogLogger extends AbstractLogger { + const NAME = 'syslog'; + const IDENT = 'Friendica'; /** @var int The default syslog flags */ diff --git a/src/Core/PConfig/Type/AbstractPConfigValues.php b/src/Core/PConfig/Type/AbstractPConfigValues.php index 39be8f4e74..e567cbaed9 100644 --- a/src/Core/PConfig/Type/AbstractPConfigValues.php +++ b/src/Core/PConfig/Type/AbstractPConfigValues.php @@ -34,6 +34,8 @@ use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues; */ abstract class AbstractPConfigValues implements IManagePersonalConfigValues { + const NAME = ''; + /** * @var Cache */ diff --git a/src/Core/PConfig/Type/JitPConfig.php b/src/Core/PConfig/Type/JitPConfig.php index c5c20577ef..b851015e79 100644 --- a/src/Core/PConfig/Type/JitPConfig.php +++ b/src/Core/PConfig/Type/JitPConfig.php @@ -33,6 +33,8 @@ use Friendica\Core\PConfig\ValueObject; */ class JitPConfig extends AbstractPConfigValues { + const NAME = 'jit'; + /** * @var array Array of already loaded db values (even if there was no value) */ diff --git a/src/Core/PConfig/Type/PreloadPConfig.php b/src/Core/PConfig/Type/PreloadPConfig.php index b3b709286a..6ae028c5ec 100644 --- a/src/Core/PConfig/Type/PreloadPConfig.php +++ b/src/Core/PConfig/Type/PreloadPConfig.php @@ -32,6 +32,8 @@ use Friendica\Core\PConfig\ValueObject; */ class PreloadPConfig extends AbstractPConfigValues { + const NAME = 'preload'; + /** @var array */ private $config_loaded; diff --git a/src/Core/Session/Factory/Session.php b/src/Core/Session/Factory/Session.php index 239b050e77..e12dbd9af6 100644 --- a/src/Core/Session/Factory/Session.php +++ b/src/Core/Session/Factory/Session.php @@ -22,8 +22,8 @@ namespace Friendica\Core\Session\Factory; use Friendica\App; -use Friendica\Core\Cache\Enum; use Friendica\Core\Cache\Factory\Cache; +use Friendica\Core\Cache\Type\DatabaseCache; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Core\Session\Capability\IHandleSessions; use Friendica\Core\Session\Type; @@ -74,7 +74,7 @@ class Session $cache = $cacheFactory->createDistributed(); // In case we're using the db as cache driver, use the native db session, not the cache - if ($config->get('system', 'cache_driver') === Enum\Type::DATABASE) { + if ($config->get('system', 'cache_driver') === DatabaseCache::NAME) { $handler = new Handler\Database($dba, $logger, $server); } else { $handler = new Handler\Cache($cache, $logger); diff --git a/static/strategies.config.php b/static/strategies.config.php index 8df0c04788..18872dc1d4 100644 --- a/static/strategies.config.php +++ b/static/strategies.config.php @@ -20,6 +20,7 @@ */ use Friendica\Core\Cache; +use Friendica\Core\Hooks\Util\StrategiesFileManager; use Friendica\Core\Logger\Type; use Friendica\Core\KeyValueStorage; use Friendica\Core\PConfig; @@ -27,22 +28,22 @@ use Psr\Log; return [ Log\LoggerInterface::class => [ - Log\NullLogger::class => [''], - Type\SyslogLogger::class => ['syslog'], - Type\StreamLogger::class => ['stream'], + Log\NullLogger::class => [StrategiesFileManager::STRATEGY_DEFAULT_KEY], + Type\SyslogLogger::class => [Type\SyslogLogger::NAME], + Type\StreamLogger::class => [Type\StreamLogger::NAME], ], Cache\Capability\ICanCache::class => [ - Cache\Type\APCuCache::class => ['apcu'], - Cache\Type\DatabaseCache::class => ['database', ''], - Cache\Type\MemcacheCache::class => ['memcache'], - Cache\Type\MemcachedCache::class => ['memcached'], - Cache\Type\RedisCache::class => ['redis'], + Cache\Type\DatabaseCache::class => [Cache\Type\DatabaseCache::NAME, StrategiesFileManager::STRATEGY_DEFAULT_KEY], + Cache\Type\APCuCache::class => [Cache\Type\APCuCache::NAME], + Cache\Type\MemcacheCache::class => [Cache\Type\MemcacheCache::NAME], + Cache\Type\MemcachedCache::class => [Cache\Type\MemcachedCache::NAME], + Cache\Type\RedisCache::class => [Cache\Type\RedisCache::NAME], ], KeyValueStorage\Capability\IManageKeyValuePairs::class => [ - KeyValueStorage\Type\DBKeyValueStorage::class => ['database', ''], + KeyValueStorage\Type\DBKeyValueStorage::class => [KeyValueStorage\Type\DBKeyValueStorage::NAME, StrategiesFileManager::STRATEGY_DEFAULT_KEY], ], PConfig\Capability\IManagePersonalConfigValues::class => [ - PConfig\Type\JitPConfig::class => ['jit'], - PConfig\Type\PreloadPConfig::class => ['preload', ''], + PConfig\Type\JitPConfig::class => [PConfig\Type\JitPConfig::NAME], + PConfig\Type\PreloadPConfig::class => [PConfig\Type\PreloadPConfig::NAME, StrategiesFileManager::STRATEGY_DEFAULT_KEY], ], ];