Skip to content

Commit 9f7616f

Browse files
committed
Moving part of the configuration to env variables
1 parent ac46d12 commit 9f7616f

File tree

10 files changed

+118
-74
lines changed

10 files changed

+118
-74
lines changed

.env.example

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ DB_DEBUG=true
1616
# Logs SQL and Errors
1717
DB_LOG_FOLDER=/var/www/db_logs
1818

19+
## DataBase Modules DB_NAME_MODULE && DB_TABLE_PREFIX_MODULE
20+
DB_NAME_MODULE_TEXTURE_PROVIDER=texture-provider
21+
DB_NAME_MODULE_ITEM_SHOP=ItemShop
22+
DB_NAME_MODULE_VOTE_REWARDS=VoteRewards
23+
DB_NAME_MODULE_LUCK_PERMS=LuckPerms
24+
DB_TABLE_PREFIX_MODULE_LUCK_PERMS=luckperms_
25+
DB_NAME_MODULE_LITE_BANS=LiteBans
26+
DB_TABLE_PREFIX_MODULE_LITE_BANS=litebans_
27+
1928
BEARER_TOKEN=null
2029
PRIVATE_API_KEY=
2130

@@ -49,4 +58,8 @@ TEXTURE_COLLECTION_PATH=collection
4958
TEXTURE_EXTENSTION=png
5059

5160
LEGACY_DIGEST=false
52-
MAX_SIZE_BYTES=2M
61+
MAX_SIZE_BYTES=2M
62+
SKIN_SIZE=64,64|64,32
63+
CAPE_SIZE=64,32
64+
SKIN_SIZE_HD=128,64|128,128|256,128|256,256|512,256|512,512|1024,512|1024,1024
65+
CAPE_SIZE_HD=128,64|256,128|512,256|1024,512

psalm.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?xml version="1.0"?>
22
<psalm
3-
errorLevel="1"
3+
errorLevel="7"
44
resolveFromConfigFile="true"
55
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
66
xmlns="https://getpsalm.org/schema/config"
77
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
8+
xmlns:xi="http://www.w3.org/2001/XInclude"
89
findUnusedBaselineEntry="true"
910
findUnusedCode="true"
1011
>
@@ -23,5 +24,6 @@
2324
<UnusedClass errorLevel="suppress" />
2425
<UnusedVariable errorLevel="suppress" />
2526
<DeprecatedClass errorLevel="suppress" />
27+
<MissingOverrideAttribute errorLevel="suppress" />
2628
</issueHandlers>
2729
</psalm>

src/Configs/MainConfig.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,8 @@ class MainConfig
3636
];
3737
/** @var array<string, array<string, string|array<string, string>>> */
3838
public const array MODULES = [
39-
'ItemShop' => [
40-
'DB_NAME' => 'ItemShop',
41-
'prefix' => ''
42-
],
43-
'VoteRewards' => [
44-
'DB_NAME' => 'VoteRewards',
45-
'prefix' => '',
46-
],
47-
'LuckPerms' => [
48-
'DB_NAME' => 'LuckPerms',
49-
'prefix' => 'luckperms_',
50-
],
51-
'LiteBans' => [
52-
'DB_NAME' => 'LiteBans',
53-
'prefix' => 'litebans_',
54-
],
5539
'TextureProvider' => [
5640
/** Driver Connect Database */
57-
'DB_NAME' => 'site',
5841
'table_user' => [
5942
'TABLE_NAME' => 'users',
6043
/**

src/Configs/TextureConfig.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/DB/Connector.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,20 @@ public function __get(string $database): DriverPDOInterface
2929
}
3030
private function getConnect(string $database): DriverPDOInterface
3131
{
32-
$module = [];
33-
if (empty($database) || $database == Main::DB_NAME()) $database = Main::DB_NAME();
32+
if (empty($database) || $database == Main::DB_NAME()) $DB_NAME = Main::DB_NAME();
3433
else {
3534
try {
3635
/** @psalm-suppress RedundantFunctionCall */
37-
$database = strtolower(Main::DB_PREFIX_SERVERS() . Main::getServerWithoutDefault($database));
36+
$DB_NAME = strtolower(Main::DB_PREFIX_SERVERS() . Main::getServerWithoutDefault($database));
3837
} catch (ServerNotFoundException) {
39-
$modules_keys_lower_case = array_change_key_case(MainConfig::MODULES);
40-
$key_exists = array_key_exists(strtolower($database), $modules_keys_lower_case);
41-
if ($key_exists === true) {
42-
$module = $modules_keys_lower_case[strtolower($database)];
43-
$database = $module['DB_NAME'];
44-
} else {
45-
throw new ServerNotFoundException($database);
46-
//$database = MainConfig::DB_NAME;
38+
$DB_NAME = Main::DB_NAME_MODULE($database);
39+
try {
40+
$DB_TABLE_PREFIX = Main::DB_TABLE_PREFIX_MODULE($database);
41+
} catch (\RuntimeException) {
4742
}
4843
}
4944
}
50-
if (array_key_exists($database, $this->database)) return $this->database[$database];
51-
return new self::$driver($database, $module['prefix'] ?? '');
45+
if (array_key_exists($DB_NAME, $this->database)) return $this->database[$DB_NAME];
46+
return $this->database[$DB_NAME] = new self::$driver($DB_NAME, $DB_TABLE_PREFIX ?? '');
5247
}
5348
}

src/DB/SingletonConnector.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,25 +47,20 @@ public function __wakeup()
4747
}
4848
private static function getConnect(string $database): DriverPDOInterface
4949
{
50-
$module = [];
51-
if (empty($database) || $database == Main::DB_NAME()) $database = Main::DB_NAME();
50+
if (empty($database) || $database == Main::DB_NAME()) $DB_NAME = Main::DB_NAME();
5251
else {
5352
try {
5453
/** @psalm-suppress RedundantFunctionCall */
55-
$database = strtolower(Main::DB_PREFIX_SERVERS() . Main::getServerWithoutDefault($database));
54+
$DB_NAME = strtolower(Main::DB_PREFIX_SERVERS() . Main::getServerWithoutDefault($database));
5655
} catch (ServerNotFoundException) {
57-
$modules_keys_lower_case = array_change_key_case(MainConfig::MODULES);
58-
$key_exists = array_key_exists(strtolower($database), $modules_keys_lower_case);
59-
if ($key_exists === true) {
60-
$module = $modules_keys_lower_case[strtolower($database)];
61-
$database = $module['DB_NAME'];
62-
} else {
63-
throw new ServerNotFoundException($database);
64-
//$database = MainConfig::DB_NAME;
56+
$DB_NAME = Main::DB_NAME_MODULE($database);
57+
try {
58+
$DB_TABLE_PREFIX = Main::DB_TABLE_PREFIX_MODULE($database);
59+
} catch (\RuntimeException) {
6560
}
6661
}
6762
}
68-
if (array_key_exists($database, self::$database)) return self::$database[$database];
69-
return self::$database[$database] = new self::$driver($database, $module['prefix'] ?? '');
63+
if (array_key_exists($DB_NAME, self::$database)) return self::$database[$DB_NAME];
64+
return self::$database[$DB_NAME] = new self::$driver($DB_NAME, $DB_TABLE_PREFIX ?? '');
7065
}
7166
}

src/Main.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Microwin7\PHPUtils\Utils\Path;
66
use Microwin7\PHPUtils\DB\SubDBTypeEnum;
77
use Microwin7\PHPUtils\Configs\MainConfig;
8+
use function Microwin7\PHPUtils\serializeEnvName;
89
use function Microwin7\PHPUtils\str_ends_with_slash;
910
use Microwin7\PHPUtils\Exceptions\ServerNotFoundException;
1011
use Microwin7\PHPUtils\Exceptions\AliasServerNotFoundException;
@@ -158,6 +159,16 @@ public static function DB_NAME(): string
158159
{
159160
return getenv()[__FUNCTION__] ?? self::DB_NAME;
160161
}
162+
public static function DB_NAME_MODULE(string $name_module): string
163+
{
164+
$ENV = serializeEnvName(__FUNCTION__, $name_module);
165+
return getenv()[$ENV] ?? throw new \RuntimeException("ENV: $ENV not found");
166+
}
167+
public static function DB_TABLE_PREFIX_MODULE(string $name_module): string
168+
{
169+
$ENV = serializeEnvName(__FUNCTION__, $name_module);
170+
return getenv()[$ENV] ?? throw new \RuntimeException("ENV: $ENV not found");
171+
}
161172
public static function DB_USER(): string
162173
{
163174
return getenv()[__FUNCTION__] ?? self::DB_USER;

src/Rules/Regex.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Regex
2222
public const string MD5 = self::UUID_NO_DASH;
2323
public const string SHA1 = '/^[0-9a-f]{40}$/';
2424
public const string SHA256 = '/^[0-9a-f]{64}$/';
25+
public const string ENV_NAME = '/^([A-Z0-9\_]+)=(.*?)$/';
2526

2627
/**
2728
* @throws \InvalidArgumentException

src/Utils/Texture.php

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Microwin7\PHPUtils\Utils\Path;
66
use Microwin7\PHPUtils\Rules\Regex;
7-
use Microwin7\PHPUtils\Configs\TextureConfig;
87
use function Microwin7\PHPUtils\convertToBytes;
98
use function Microwin7\PHPUtils\ar_slash_string;
109
use function Microwin7\PHPUtils\str_ends_with_slash;
@@ -14,7 +13,7 @@
1413
use Microwin7\PHPUtils\Contracts\Texture\Enum\ResponseTypeEnum;
1514
use Microwin7\PHPUtils\Contracts\Texture\Enum\TextureStorageTypeEnum;
1615

17-
class Texture extends TextureConfig
16+
class Texture
1817
{
1918
/**
2019
* Для вызова, сохранения, проверок
@@ -49,6 +48,33 @@ class Texture extends TextureConfig
4948
ZgAAAAxJREFUeAFjGAV4AQABIAABL3HDQQAAAABJRU5ErkJggg==";
5049
private const string SKIN_DEFAULT_SHA256 = '98805f6ab41575b7ff4af11b70c074773c5bcc210f2429f6b5513150d746e4cd';
5150
private const string CAPE_DEFAULT_SHA256 = 'f2072fdfff5302b7c13672e54fdc8895dc75b3f675be3a43245de6894f971e38';
51+
/** list<array{0: int, 1: int}> */
52+
private const array SKIN_SIZE = [
53+
[64, 64],
54+
[64, 32]
55+
];
56+
/** list<array{0: int, 1: int}> */
57+
private const array CAPE_SIZE = [
58+
[64, 32]
59+
];
60+
/** list<array{0: int, 1: int}> */
61+
private const array SKIN_SIZE_HD = [
62+
[128, 64],
63+
[128, 128],
64+
[256, 128],
65+
[256, 256],
66+
[512, 256],
67+
[512, 512],
68+
[1024, 512],
69+
[1024, 1024]
70+
];
71+
/** list<array{0: int, 1: int}> */
72+
private const array CAPE_SIZE_HD = [
73+
[128, 64],
74+
[256, 128],
75+
[512, 256],
76+
[1024, 512]
77+
];
5278

5379
/** BASE DIR */
5480
public static function STORAGE_DIR(): string
@@ -98,30 +124,52 @@ public static function PATH(ResponseTypeEnum|TextureStorageTypeEnum $type, strin
98124
$extension ??= self::EXTENSTION();
99125
return self::TEXTURE_STORAGE_FULL_PATH($type, $size) . $login . $extension;
100126
}
101-
/** @return array<array{w: int, h: int}> */
127+
/** @return array<array-key, object{width: int, height: int}> */
102128
public static function SIZE(ResponseTypeEnum $type = ResponseTypeEnum::SKIN): array
103129
{
104130
return match ($type) {
105-
ResponseTypeEnum::SKIN => parent::SKIN_SIZE,
106-
ResponseTypeEnum::CAPE => parent::CAPE_SIZE,
131+
ResponseTypeEnum::SKIN => self::parseSizeEnvArray('SKIN_SIZE'),
132+
ResponseTypeEnum::CAPE => self::parseSizeEnvArray('CAPE_SIZE'),
107133
default => throw new \InvalidArgumentException(sprintf('Un-supported texture size type: %s', $type->name))
108134
};
109135
}
110-
/** @return array<array{w: int, h: int}> */
136+
/** @return array<array-key, object{width: int, height: int}> */
111137
public static function SIZE_WITH_HD(ResponseTypeEnum $type = ResponseTypeEnum::SKIN): array
112138
{
113139
return match ($type) {
114-
ResponseTypeEnum::SKIN => parent::SKIN_SIZE_HD,
115-
ResponseTypeEnum::CAPE => parent::CAPE_SIZE_HD,
140+
ResponseTypeEnum::SKIN => self::parseSizeEnvArray('SKIN_SIZE_HD'),
141+
ResponseTypeEnum::CAPE => self::parseSizeEnvArray('CAPE_SIZE_HD'),
116142
default => throw new \InvalidArgumentException(sprintf('Un-supported texture size type: %s', $type->name))
117143
};
118144
}
145+
/**
146+
* @param "SKIN_SIZE"|"CAPE_SIZE"|"SKIN_SIZE_HD"|"CAPE_SIZE_HD"
147+
* @return array<array-key, object{width: int, height: int}>
148+
*/
149+
public static function parseSizeEnvArray(string $env_name): array
150+
{
151+
// putenv('SKIN_SIZE=128,64|128,128|256,128|256');
152+
try {
153+
$ENV = getenv($env_name);
154+
if ($ENV === false || empty($ENV)) new \RuntimeException("ENV $env_name empty");
155+
return array_map(
156+
fn(string $pair) =>
157+
(object)array_combine(
158+
['width', 'height'],
159+
array_map('intval', explode(',', $pair))
160+
),
161+
explode('|', $ENV)
162+
);
163+
} catch (\ValueError | \RuntimeException) {
164+
return array_map(fn(array $item) => (object) ['width' => $item[0], 'height' => $item[1]], self::{$env_name});
165+
}
166+
}
119167
/** @throws TextureSizeException */
120168
public static function validateSize(int $width, int $height, ResponseTypeEnum $type = ResponseTypeEnum::SKIN): true
121169
{
122170
$valid_size = false;
123171
foreach (self::SIZE($type) as $value) {
124-
if ($value['w'] == $width && $value['h'] == $height) {
172+
if ($value->width == $width && $value->height == $height) {
125173
$valid_size = true;
126174
}
127175
}
@@ -132,7 +180,7 @@ public static function validateHDSize(int $width, int $height, ResponseTypeEnum
132180
{
133181
$valid_size = false;
134182
foreach (self::SIZE_WITH_HD($type) as $value) {
135-
if ($value['w'] == $width && $value['h'] == $height) {
183+
if ($value->width == $width && $value->height == $height) {
136184
$valid_size = true;
137185
}
138186
}

src/functions.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,18 @@ function convertToBytes(string $size): int
139139
default => (int) $size
140140
};
141141
}
142+
/**
143+
* @throws \RuntimeException
144+
* @throws \InvalidArgumentException
145+
*/
146+
function serializeEnvName(string $prefix, string $name): string
147+
{
148+
if ($name === '') return $name;
149+
// Add `_` before capital letters, but not in the middle of a group of capital letters
150+
$name = preg_replace('/(?<!_)([A-Z])(?=[a-z]|\d|$)/', '_$1', $name);
151+
if ($name === null) throw new \RuntimeException("Cannot serialize this env name: $name");
152+
// Make sure there is `_` at the beginning
153+
if ($name[0] !== '_') $name = '_' . $name;
154+
\Microwin7\PHPUtils\Rules\Regex::valid_with_pattern($prefix, \Microwin7\PHPUtils\Rules\Regex::ENV_NAME);
155+
return $prefix . strtoupper($name);
156+
}

0 commit comments

Comments
 (0)