Refactored Configuration
This commit is contained in:
parent
5eeb06805a
commit
5555e79327
11 changed files with 247 additions and 46 deletions
|
@ -22,10 +22,10 @@ class MemcachedCacheLayer extends CacheLayer
|
|||
}
|
||||
|
||||
$this->memcached = new Memcached();
|
||||
$this->memcached->addServer(Configuration::getConfiguration()['cache']['host'], (int)Configuration::getConfiguration()['cache']['port']);
|
||||
if(Configuration::getConfiguration()['cache']['username'] !== null || Configuration::getConfiguration()['cache']['password'] !== null)
|
||||
$this->memcached->addServer(Configuration::getCacheConfiguration()->getHost(), Configuration::getCacheConfiguration()->getPort());
|
||||
if(Configuration::getCacheConfiguration()->getUsername() !== null || Configuration::getCacheConfiguration()->getPassword() !== null)
|
||||
{
|
||||
$this->memcached->setSaslAuthData(Configuration::getConfiguration()['cache']['username'], Configuration::getConfiguration()['cache']['password']);
|
||||
$this->memcached->setSaslAuthData(Configuration::getCacheConfiguration()->getUsername(), Configuration::getCacheConfiguration()->getPassword());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -26,15 +26,15 @@ class RedisCacheLayer extends CacheLayer
|
|||
|
||||
try
|
||||
{
|
||||
$this->redis->connect(Configuration::getConfiguration()['cache']['host'], (int)Configuration::getConfiguration()['cache']['port']);
|
||||
if (Configuration::getConfiguration()['cache']['password'] !== null)
|
||||
$this->redis->connect(Configuration::getCacheConfiguration()->getHost(), Configuration::getCacheConfiguration()->getPort());
|
||||
if (Configuration::getCacheConfiguration()->getPassword() !== null)
|
||||
{
|
||||
$this->redis->auth(Configuration::getConfiguration()['cache']['password']);
|
||||
$this->redis->auth(Configuration::getCacheConfiguration()->getPassword());
|
||||
}
|
||||
|
||||
if (Configuration::getConfiguration()['cache']['database'] !== 0)
|
||||
if (Configuration::getCacheConfiguration()->getDatabase() !== null)
|
||||
{
|
||||
$this->redis->select((int)Configuration::getConfiguration()['cache']['database']);
|
||||
$this->redis->select(Configuration::getCacheConfiguration()->getDatabase());
|
||||
}
|
||||
}
|
||||
catch (RedisException $e)
|
||||
|
|
|
@ -30,7 +30,7 @@ class InitializeCommand implements CliCommandInterface
|
|||
|
||||
Log::info('net.nosial.socialbox', 'Initializing Socialbox...');
|
||||
|
||||
if(Configuration::getConfiguration()['cache']['enabled'])
|
||||
if(Configuration::getCacheConfiguration()->isEnabled())
|
||||
{
|
||||
Log::verbose('net.nosial.socialbox', 'Clearing cache layer...');
|
||||
CacheLayer::getInstance()->clear();
|
||||
|
|
|
@ -2,43 +2,75 @@
|
|||
|
||||
namespace Socialbox\Classes;
|
||||
|
||||
use Socialbox\Classes\Configuration\CacheConfiguration;
|
||||
use Socialbox\Classes\Configuration\DatabaseConfiguration;
|
||||
|
||||
class Configuration
|
||||
{
|
||||
private static ?array $configuration = null;
|
||||
private static ?DatabaseConfiguration $databaseConfiguration = null;
|
||||
private static ?CacheConfiguration $cacheConfiguration = null;
|
||||
|
||||
private static function initializeConfiguration(): void
|
||||
{
|
||||
$config = new \ConfigLib\Configuration('socialbox');
|
||||
|
||||
// False by default, requires the user to enable it.
|
||||
$config->setDefault('instance.enabled', false);
|
||||
|
||||
$config->setDefault('security.display_internal_exceptions', false);
|
||||
|
||||
$config->setDefault('database.host', '127.0.0.1');
|
||||
$config->setDefault('database.port', 3306);
|
||||
$config->setDefault('database.username', 'root');
|
||||
$config->setDefault('database.password', 'root');
|
||||
$config->setDefault('database.name', 'test');
|
||||
|
||||
$config->setDefault('cache.enabled', false);
|
||||
$config->setDefault('cache.engine', 'redis');
|
||||
$config->setDefault('cache.host', '127.0.0.1');
|
||||
$config->setDefault('cache.port', 6379);
|
||||
$config->setDefault('cache.username', null);
|
||||
$config->setDefault('cache.password', null);
|
||||
$config->setDefault('cache.database', 0);
|
||||
$config->setDefault('cache.sessions.enabled', true);
|
||||
$config->setDefault('cache.sessions.ttl', 3600);
|
||||
$config->setDefault('cache.sessions.max', 1000);
|
||||
|
||||
$config->save();
|
||||
|
||||
self::$configuration = $config->getConfiguration();
|
||||
self::$databaseConfiguration = self::$configuration['database'];
|
||||
self::$cacheConfiguration = self::$configuration['cache'];
|
||||
}
|
||||
|
||||
public static function getConfiguration(): array
|
||||
{
|
||||
if(self::$configuration === null)
|
||||
{
|
||||
$config = new \ConfigLib\Configuration('socialbox');
|
||||
|
||||
// False by default, requires the user to enable it.
|
||||
$config->setDefault('instance.enabled', false);
|
||||
|
||||
$config->setDefault('security.display_internal_exceptions', false);
|
||||
|
||||
$config->setDefault('database.host', '127.0.0.1');
|
||||
$config->setDefault('database.port', 3306);
|
||||
$config->setDefault('database.username', 'root');
|
||||
$config->setDefault('database.password', 'root');
|
||||
$config->setDefault('database.name', 'test');
|
||||
|
||||
$config->setDefault('cache.enabled', false);
|
||||
$config->setDefault('cache.engine', 'redis');
|
||||
$config->setDefault('cache.host', '127.0.0.1');
|
||||
$config->setDefault('cache.port', 6379);
|
||||
$config->setDefault('cache.username', null);
|
||||
$config->setDefault('cache.password', null);
|
||||
$config->setDefault('cache.database', 0);
|
||||
$config->setDefault('cache.variables.enabled', true);
|
||||
$config->setDefault('cache.variables.ttl', 3600);
|
||||
$config->setDefault('cache.variables.max', 1000);
|
||||
|
||||
$config->save();
|
||||
|
||||
self::$configuration = $config->getConfiguration();
|
||||
self::initializeConfiguration();
|
||||
}
|
||||
|
||||
return self::$configuration;
|
||||
}
|
||||
|
||||
public static function getDatabaseConfiguration(): DatabaseConfiguration
|
||||
{
|
||||
if(self::$databaseConfiguration === null)
|
||||
{
|
||||
self::initializeConfiguration();
|
||||
}
|
||||
|
||||
return self::$databaseConfiguration;
|
||||
}
|
||||
|
||||
public static function getCacheConfiguration(): CacheConfiguration
|
||||
{
|
||||
if(self::$cacheConfiguration === null)
|
||||
{
|
||||
self::initializeConfiguration();
|
||||
}
|
||||
|
||||
return self::$cacheConfiguration;
|
||||
}
|
||||
}
|
83
src/Socialbox/Classes/Configuration/CacheConfiguration.php
Normal file
83
src/Socialbox/Classes/Configuration/CacheConfiguration.php
Normal file
|
@ -0,0 +1,83 @@
|
|||
<?php
|
||||
|
||||
namespace Socialbox\Classes\Configuration;
|
||||
|
||||
class CacheConfiguration
|
||||
{
|
||||
private bool $enabled;
|
||||
private string $engine;
|
||||
private string $host;
|
||||
private int $port;
|
||||
private ?string $username;
|
||||
private ?string $password;
|
||||
private ?int $database;
|
||||
|
||||
private bool $sessionsEnabled;
|
||||
private int $sessionsTtl;
|
||||
private int $sessionsMax;
|
||||
|
||||
public function __construct(array $data)
|
||||
{
|
||||
$this->enabled = (bool)$data['enabled'];
|
||||
$this->engine = (string)$data['engine'];
|
||||
$this->host = (string)$data['host'];
|
||||
$this->port = (int)$data['port'];
|
||||
$this->username = $data['username'] ? (string)$data['username'] : null;
|
||||
$this->password = $data['password'] ? (string)$data['password'] : null;
|
||||
$this->database = $data['database'] ? (int)$data['database'] : null;
|
||||
|
||||
$this->sessionsEnabled = (bool)$data['sessions.enabled'];
|
||||
$this->sessionsTtl = (int)$data['sessions.ttl'];
|
||||
$this->sessionsMax = (int)$data['sessions.max'];
|
||||
}
|
||||
|
||||
public function isEnabled(): bool
|
||||
{
|
||||
return $this->enabled;
|
||||
}
|
||||
|
||||
public function getEngine(): string
|
||||
{
|
||||
return $this->engine;
|
||||
}
|
||||
|
||||
public function getHost(): string
|
||||
{
|
||||
return $this->host;
|
||||
}
|
||||
|
||||
public function getPort(): int
|
||||
{
|
||||
return $this->port;
|
||||
}
|
||||
|
||||
public function getUsername(): ?string
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
public function getPassword(): ?string
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
public function getDatabase(): ?int
|
||||
{
|
||||
return $this->database;
|
||||
}
|
||||
|
||||
public function isSessionsEnabled(): bool
|
||||
{
|
||||
return $this->sessionsEnabled;
|
||||
}
|
||||
|
||||
public function getSessionsTtl(): int
|
||||
{
|
||||
return $this->sessionsTtl;
|
||||
}
|
||||
|
||||
public function getSessionsMax(): int
|
||||
{
|
||||
return $this->sessionsMax;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace Socialbox\Classes\Configuration;
|
||||
|
||||
class DatabaseConfiguration
|
||||
{
|
||||
private string $host;
|
||||
private int $port;
|
||||
private string $username;
|
||||
private ?string $password;
|
||||
private string $name;
|
||||
|
||||
public function __construct(array $data)
|
||||
{
|
||||
$this->host = (string)$data['host'];
|
||||
$this->port = (int)$data['port'];
|
||||
$this->username = (string)$data['username'];
|
||||
$this->password = $data['password'] ? (string)$data['password'] : null;
|
||||
$this->name = (string)$data['name'];
|
||||
}
|
||||
|
||||
public function getHost(): string
|
||||
{
|
||||
return $this->host;
|
||||
}
|
||||
|
||||
public function getPort(): int
|
||||
{
|
||||
return $this->port;
|
||||
}
|
||||
|
||||
public function getUsername(): string
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
public function getPassword(): ?string
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
}
|
|
@ -15,6 +15,6 @@ create table registered_peers
|
|||
comment 'Table for housing registered peers under this network';
|
||||
|
||||
create index registered_peers_registered_index
|
||||
on registered_peers (registered)
|
||||
on registered_peers (created)
|
||||
comment 'The Index for the reigstered column of the peer';
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue