Refactored Configuration
This commit is contained in:
parent
5eeb06805a
commit
5555e79327
11 changed files with 247 additions and 46 deletions
15
.idea/php.xml
generated
15
.idea/php.xml
generated
|
@ -12,16 +12,17 @@
|
||||||
</component>
|
</component>
|
||||||
<component name="PhpIncludePathManager">
|
<component name="PhpIncludePathManager">
|
||||||
<include_path>
|
<include_path>
|
||||||
<path value="$USER_HOME$/phar" />
|
<path value="/var/ncc/packages/com.symfony.filesystem=v7.1.5" />
|
||||||
<path value="/var/ncc/packages/com.symfony.uid=v7.1.5" />
|
|
||||||
<path value="/var/ncc/packages/com.symfony.filesystem=v7.1.2" />
|
|
||||||
<path value="/var/ncc/packages/com.symfony.polyfill_ctype=v1.31.0" />
|
<path value="/var/ncc/packages/com.symfony.polyfill_ctype=v1.31.0" />
|
||||||
<path value="/var/ncc/packages/com.symfony.polyfill_mbstring=v1.31.0" />
|
<path value="/var/ncc/packages/com.symfony.polyfill_mbstring=v1.31.0" />
|
||||||
<path value="/var/ncc/packages/com.symfony.process=v7.1.3" />
|
<path value="/var/ncc/packages/com.symfony.polyfill_uuid=v1.31.0" />
|
||||||
<path value="/var/ncc/packages/com.symfony.yaml=v7.1.4" />
|
<path value="/var/ncc/packages/com.symfony.process=v7.1.5" />
|
||||||
|
<path value="/var/ncc/packages/com.symfony.uid=v7.1.5" />
|
||||||
|
<path value="/var/ncc/packages/com.symfony.yaml=v7.1.5" />
|
||||||
<path value="/var/ncc/packages/net.nosial.configlib=1.1.0" />
|
<path value="/var/ncc/packages/net.nosial.configlib=1.1.0" />
|
||||||
<path value="/var/ncc/packages/net.nosial.loglib=1.1.0" />
|
<path value="/var/ncc/packages/net.nosial.loglib=1.1.1" />
|
||||||
<path value="/var/ncc/packages/net.nosial.optslib=1.1.0" />
|
<path value="/var/ncc/packages/net.nosial.optslib=1.1.2" />
|
||||||
|
<path value="$USER_HOME$/phar" />
|
||||||
<path value="/usr/share/ncc" />
|
<path value="/usr/share/ncc" />
|
||||||
</include_path>
|
</include_path>
|
||||||
</component>
|
</component>
|
||||||
|
|
|
@ -69,7 +69,7 @@ abstract class CacheLayer
|
||||||
{
|
{
|
||||||
if (self::$instance === null)
|
if (self::$instance === null)
|
||||||
{
|
{
|
||||||
$engine = Configuration::getConfiguration()['cache']['engine'];
|
$engine = Configuration::getCacheConfiguration()->getEngine();
|
||||||
|
|
||||||
if ($engine === 'redis')
|
if ($engine === 'redis')
|
||||||
{
|
{
|
||||||
|
|
|
@ -22,10 +22,10 @@ class MemcachedCacheLayer extends CacheLayer
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->memcached = new Memcached();
|
$this->memcached = new Memcached();
|
||||||
$this->memcached->addServer(Configuration::getConfiguration()['cache']['host'], (int)Configuration::getConfiguration()['cache']['port']);
|
$this->memcached->addServer(Configuration::getCacheConfiguration()->getHost(), Configuration::getCacheConfiguration()->getPort());
|
||||||
if(Configuration::getConfiguration()['cache']['username'] !== null || Configuration::getConfiguration()['cache']['password'] !== null)
|
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
|
try
|
||||||
{
|
{
|
||||||
$this->redis->connect(Configuration::getConfiguration()['cache']['host'], (int)Configuration::getConfiguration()['cache']['port']);
|
$this->redis->connect(Configuration::getCacheConfiguration()->getHost(), Configuration::getCacheConfiguration()->getPort());
|
||||||
if (Configuration::getConfiguration()['cache']['password'] !== null)
|
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)
|
catch (RedisException $e)
|
||||||
|
|
|
@ -30,7 +30,7 @@ class InitializeCommand implements CliCommandInterface
|
||||||
|
|
||||||
Log::info('net.nosial.socialbox', 'Initializing Socialbox...');
|
Log::info('net.nosial.socialbox', 'Initializing Socialbox...');
|
||||||
|
|
||||||
if(Configuration::getConfiguration()['cache']['enabled'])
|
if(Configuration::getCacheConfiguration()->isEnabled())
|
||||||
{
|
{
|
||||||
Log::verbose('net.nosial.socialbox', 'Clearing cache layer...');
|
Log::verbose('net.nosial.socialbox', 'Clearing cache layer...');
|
||||||
CacheLayer::getInstance()->clear();
|
CacheLayer::getInstance()->clear();
|
||||||
|
|
|
@ -2,43 +2,75 @@
|
||||||
|
|
||||||
namespace Socialbox\Classes;
|
namespace Socialbox\Classes;
|
||||||
|
|
||||||
|
use Socialbox\Classes\Configuration\CacheConfiguration;
|
||||||
|
use Socialbox\Classes\Configuration\DatabaseConfiguration;
|
||||||
|
|
||||||
class Configuration
|
class Configuration
|
||||||
{
|
{
|
||||||
private static ?array $configuration = null;
|
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
|
public static function getConfiguration(): array
|
||||||
{
|
{
|
||||||
if(self::$configuration === null)
|
if(self::$configuration === null)
|
||||||
{
|
{
|
||||||
$config = new \ConfigLib\Configuration('socialbox');
|
self::initializeConfiguration();
|
||||||
|
|
||||||
// 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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::$configuration;
|
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';
|
comment 'Table for housing registered peers under this network';
|
||||||
|
|
||||||
create index registered_peers_registered_index
|
create index registered_peers_registered_index
|
||||||
on registered_peers (registered)
|
on registered_peers (created)
|
||||||
comment 'The Index for the reigstered column of the peer';
|
comment 'The Index for the reigstered column of the peer';
|
||||||
|
|
||||||
|
|
34
src/Socialbox/Enums/Flags/PeerFlags.php
Normal file
34
src/Socialbox/Enums/Flags/PeerFlags.php
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Socialbox\Enums\Flags;
|
||||||
|
|
||||||
|
enum PeerFlags : string
|
||||||
|
{
|
||||||
|
// Administrative Flags
|
||||||
|
case ADMIN = 'ADMIN';
|
||||||
|
case MODERATOR = 'MODERATOR';
|
||||||
|
|
||||||
|
// General Flags
|
||||||
|
case VERIFIED = 'VERIFIED';
|
||||||
|
|
||||||
|
// Verification Flags
|
||||||
|
case VER_SET_PASSWORD = 'VER_SET_PASSWORD';
|
||||||
|
case VER_SET_OTP = 'VER_SET_OTP';
|
||||||
|
case VER_SOLVE_IMAGE_CAPTCHA = 'VER_SOLVE_IMAGE_CAPTCHA';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether the flag is public. Public flags can be seen by other peers.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isPublic(): bool
|
||||||
|
{
|
||||||
|
return match($this)
|
||||||
|
{
|
||||||
|
self::VER_SET_PASSWORD,
|
||||||
|
self::VER_SET_OTP,
|
||||||
|
self::VER_SOLVE_IMAGE_CAPTCHA => false,
|
||||||
|
default => true,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,6 +7,7 @@
|
||||||
use InvalidArgumentException;
|
use InvalidArgumentException;
|
||||||
use PDO;
|
use PDO;
|
||||||
use PDOException;
|
use PDOException;
|
||||||
|
use Socialbox\Classes\Configuration;
|
||||||
use Socialbox\Classes\Cryptography;
|
use Socialbox\Classes\Cryptography;
|
||||||
use Socialbox\Classes\Database;
|
use Socialbox\Classes\Database;
|
||||||
use Socialbox\Enums\SessionState;
|
use Socialbox\Enums\SessionState;
|
||||||
|
@ -107,7 +108,8 @@
|
||||||
$data['created'] = new DateTime($data['created']);
|
$data['created'] = new DateTime($data['created']);
|
||||||
$data['last_request'] = new DateTime($data['last_request']);
|
$data['last_request'] = new DateTime($data['last_request']);
|
||||||
|
|
||||||
return SessionRecord::fromArray($data);
|
$sessionRecord = SessionRecord::fromArray($data);
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (PDOException | DateMalformedStringException $e)
|
catch (PDOException | DateMalformedStringException $e)
|
||||||
{
|
{
|
||||||
|
@ -120,6 +122,7 @@
|
||||||
*
|
*
|
||||||
* @param string $uuid The UUID of the session to update.
|
* @param string $uuid The UUID of the session to update.
|
||||||
* @return void
|
* @return void
|
||||||
|
* @throws DatabaseOperationException
|
||||||
*/
|
*/
|
||||||
public static function updateAuthenticatedPeer(string $uuid): void
|
public static function updateAuthenticatedPeer(string $uuid): void
|
||||||
{
|
{
|
||||||
|
@ -141,6 +144,7 @@
|
||||||
*
|
*
|
||||||
* @param string $uuid The UUID of the session to be updated.
|
* @param string $uuid The UUID of the session to be updated.
|
||||||
* @return void
|
* @return void
|
||||||
|
* @throws DatabaseOperationException
|
||||||
*/
|
*/
|
||||||
public static function updateLastRequest(string $uuid): void
|
public static function updateLastRequest(string $uuid): void
|
||||||
{
|
{
|
||||||
|
@ -164,6 +168,7 @@
|
||||||
* @param string $uuid The unique identifier of the session to update.
|
* @param string $uuid The unique identifier of the session to update.
|
||||||
* @param SessionState $state The new state to be set for the session.
|
* @param SessionState $state The new state to be set for the session.
|
||||||
* @return void No return value.
|
* @return void No return value.
|
||||||
|
* @throws DatabaseOperationException
|
||||||
*/
|
*/
|
||||||
public static function updateState(string $uuid, SessionState $state): void
|
public static function updateState(string $uuid, SessionState $state): void
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue