instances added god may help

This commit is contained in:
2026-02-23 12:58:19 +01:00
parent 25370a1a55
commit 99db252f60
290 changed files with 9858 additions and 4914 deletions

View File

@@ -4,11 +4,17 @@ namespace MintyPHP\Service\Settings;
class SettingCacheService
{
private static ?array $settings = null;
private ?array $settings = null;
private ?string $cacheFilePath;
public static function get(string $key): ?string
public function __construct(?string $cacheFilePath = null)
{
$settings = self::all();
$this->cacheFilePath = $cacheFilePath;
}
public function get(string $key): ?string
{
$settings = $this->all();
$value = $settings[$key] ?? null;
if ($value === null) {
return null;
@@ -17,26 +23,26 @@ class SettingCacheService
return $value !== '' ? $value : null;
}
public static function all(): array
public function all(): array
{
if (self::$settings !== null) {
return self::$settings;
if ($this->settings !== null) {
return $this->settings;
}
$file = self::filePath();
$file = $this->filePath();
if (!is_file($file)) {
self::$settings = [];
return self::$settings;
$this->settings = [];
return $this->settings;
}
$loaded = include $file;
self::$settings = is_array($loaded) ? $loaded : [];
return self::$settings;
$this->settings = is_array($loaded) ? $loaded : [];
return $this->settings;
}
public static function update(array $values): bool
public function update(array $values): bool
{
$current = self::all();
$current = $this->all();
foreach ($values as $key => $value) {
$settingKey = trim((string) $key);
if ($settingKey === '') {
@@ -44,12 +50,12 @@ class SettingCacheService
}
$current[$settingKey] = $value === null ? null : (string) $value;
}
return self::write($current);
return $this->write($current);
}
public static function write(array $settings): bool
public function write(array $settings): bool
{
$file = self::filePath();
$file = $this->filePath();
$dir = dirname($file);
if (!is_dir($dir) && !@mkdir($dir, 0775, true) && !is_dir($dir)) {
return false;
@@ -60,7 +66,7 @@ class SettingCacheService
return false;
}
$payload = "<?php\nreturn " . self::exportValue($settings) . ";\n";
$payload = "<?php\nreturn " . $this->exportValue($settings) . ";\n";
$written = @file_put_contents($tmp, $payload, LOCK_EX);
if ($written === false) {
@unlink($tmp);
@@ -72,16 +78,19 @@ class SettingCacheService
return false;
}
self::$settings = $settings;
$this->settings = $settings;
return true;
}
public static function filePath(): string
public function filePath(): string
{
if (is_string($this->cacheFilePath) && $this->cacheFilePath !== '') {
return $this->cacheFilePath;
}
return dirname(__DIR__, 3) . '/config/settings.php';
}
private static function exportValue(mixed $value, int $depth = 0): string
private function exportValue(mixed $value, int $depth = 0): string
{
if (!is_array($value)) {
return var_export($value, true);
@@ -99,7 +108,7 @@ class SettingCacheService
$lines[] = $childIndent
. var_export($key, true)
. ' => '
. self::exportValue($item, $depth + 1)
. $this->exportValue($item, $depth + 1)
. ',';
}

View File

@@ -0,0 +1,265 @@
<?php
namespace MintyPHP\Service\Settings;
class SettingGateway
{
public function __construct(private readonly SettingService $settingService)
{
}
public function get(string $key): ?array
{
return $this->settingService->get($key);
}
public function getValue(string $key): ?string
{
return $this->settingService->getValue($key);
}
public function getDefaultTenantId(): ?int
{
return $this->settingService->getDefaultTenantId();
}
public function setDefaultTenantId(?int $tenantId, ?string $description = null): bool
{
return $this->settingService->setDefaultTenantId($tenantId, $description);
}
public function getDefaultRoleId(): ?int
{
return $this->settingService->getDefaultRoleId();
}
public function setDefaultRoleId(?int $roleId, ?string $description = null): bool
{
return $this->settingService->setDefaultRoleId($roleId, $description);
}
public function getDefaultDepartmentId(): ?int
{
return $this->settingService->getDefaultDepartmentId();
}
public function setDefaultDepartmentId(?int $departmentId, ?string $description = null): bool
{
return $this->settingService->setDefaultDepartmentId($departmentId, $description);
}
public function getAppTitle(): ?string
{
return $this->settingService->getAppTitle();
}
public function setAppTitle(?string $title, ?string $description = null): bool
{
return $this->settingService->setAppTitle($title, $description);
}
public function getAppLocale(): ?string
{
return $this->settingService->getAppLocale();
}
public function setAppLocale(?string $locale, ?string $description = null): bool
{
return $this->settingService->setAppLocale($locale, $description);
}
public function getAppTheme(): ?string
{
return $this->settingService->getAppTheme();
}
public function setAppTheme(?string $theme, ?string $description = null): bool
{
return $this->settingService->setAppTheme($theme, $description);
}
public function isUserThemeAllowed(): bool
{
return $this->settingService->isUserThemeAllowed();
}
public function setUserThemeAllowed(bool $allowed, ?string $description = null): bool
{
return $this->settingService->setUserThemeAllowed($allowed, $description);
}
public function isRegistrationEnabled(): bool
{
return $this->settingService->isRegistrationEnabled();
}
public function setRegistrationEnabled(bool $allowed, ?string $description = null): bool
{
return $this->settingService->setRegistrationEnabled($allowed, $description);
}
public function getApiTokenDefaultTtlDays(): int
{
return $this->settingService->getApiTokenDefaultTtlDays();
}
public function setApiTokenDefaultTtlDays(?int $days, ?string $description = null): bool
{
return $this->settingService->setApiTokenDefaultTtlDays($days, $description);
}
public function getApiTokenMaxTtlDays(): int
{
return $this->settingService->getApiTokenMaxTtlDays();
}
public function setApiTokenMaxTtlDays(?int $days, ?string $description = null): bool
{
return $this->settingService->setApiTokenMaxTtlDays($days, $description);
}
public function getUserInactivityDeactivateDays(): int
{
return $this->settingService->getUserInactivityDeactivateDays();
}
public function setUserInactivityDeactivateDays(?int $days, ?string $description = null): bool
{
return $this->settingService->setUserInactivityDeactivateDays($days, $description);
}
public function getUserInactivityDeleteDays(): int
{
return $this->settingService->getUserInactivityDeleteDays();
}
public function setUserInactivityDeleteDays(?int $days, ?string $description = null): bool
{
return $this->settingService->setUserInactivityDeleteDays($days, $description);
}
public function getApiCorsAllowedOrigins(): array
{
return $this->settingService->getApiCorsAllowedOrigins();
}
public function getApiCorsAllowedOriginsText(): string
{
return $this->settingService->getApiCorsAllowedOriginsText();
}
public function setApiCorsAllowedOrigins(?string $rawOrigins, ?string $description = null): bool
{
return $this->settingService->setApiCorsAllowedOrigins($rawOrigins, $description);
}
public function getMicrosoftSharedClientId(): ?string
{
return $this->settingService->getMicrosoftSharedClientId();
}
public function setMicrosoftSharedClientId(?string $clientId, ?string $description = null): bool
{
return $this->settingService->setMicrosoftSharedClientId($clientId, $description);
}
public function getMicrosoftSharedClientSecret(): ?string
{
return $this->settingService->getMicrosoftSharedClientSecret();
}
public function setMicrosoftSharedClientSecret(?string $secret, ?string $description = null): bool
{
return $this->settingService->setMicrosoftSharedClientSecret($secret, $description);
}
public function getMicrosoftAuthority(): string
{
return $this->settingService->getMicrosoftAuthority();
}
public function setMicrosoftAuthority(?string $authority, ?string $description = null): bool
{
return $this->settingService->setMicrosoftAuthority($authority, $description);
}
public function getAppPrimaryColor(): ?string
{
return $this->settingService->getAppPrimaryColor();
}
public function setAppPrimaryColor(?string $color, ?string $description = null): bool
{
return $this->settingService->setAppPrimaryColor($color, $description);
}
public function getSmtpHost(): ?string
{
return $this->settingService->getSmtpHost();
}
public function setSmtpHost(?string $host, ?string $description = null): bool
{
return $this->settingService->setSmtpHost($host, $description);
}
public function getSmtpPort(): ?int
{
return $this->settingService->getSmtpPort();
}
public function setSmtpPort(?int $port, ?string $description = null): bool
{
return $this->settingService->setSmtpPort($port, $description);
}
public function getSmtpUser(): ?string
{
return $this->settingService->getSmtpUser();
}
public function setSmtpUser(?string $user, ?string $description = null): bool
{
return $this->settingService->setSmtpUser($user, $description);
}
public function getSmtpPassword(): ?string
{
return $this->settingService->getSmtpPassword();
}
public function setSmtpPassword(?string $password, ?string $description = null): bool
{
return $this->settingService->setSmtpPassword($password, $description);
}
public function getSmtpSecure(): ?string
{
return $this->settingService->getSmtpSecure();
}
public function setSmtpSecure(?string $secure, ?string $description = null): bool
{
return $this->settingService->setSmtpSecure($secure, $description);
}
public function getSmtpFrom(): ?string
{
return $this->settingService->getSmtpFrom();
}
public function setSmtpFrom(?string $from, ?string $description = null): bool
{
return $this->settingService->setSmtpFrom($from, $description);
}
public function getSmtpFromName(): ?string
{
return $this->settingService->getSmtpFromName();
}
public function setSmtpFromName(?string $fromName, ?string $description = null): bool
{
return $this->settingService->setSmtpFromName($fromName, $description);
}
}

View File

@@ -2,14 +2,22 @@
namespace MintyPHP\Service\Settings;
use MintyPHP\Repository\Settings\SettingRepository;
use MintyPHP\Repository\Tenant\TenantRepository;
use MintyPHP\Repository\Access\RoleRepository;
use MintyPHP\Repository\Org\DepartmentRepository;
use MintyPHP\Repository\Settings\SettingRepository;
use MintyPHP\Repository\Tenant\TenantRepository;
use MintyPHP\Support\Crypto;
class SettingService
{
public function __construct(
private readonly SettingRepository $settingRepository,
private readonly TenantRepository $tenantRepository,
private readonly RoleRepository $roleRepository,
private readonly DepartmentRepository $departmentRepository,
private readonly ThemeConfigService $themeConfigService
) {
}
public const DEFAULT_TENANT_KEY = 'default_tenant_id';
public const DEFAULT_ROLE_KEY = 'default_role_id';
public const DEFAULT_DEPARTMENT_KEY = 'default_department_id';
@@ -43,112 +51,112 @@ class SettingService
private const USER_INACTIVITY_DAYS_MIN = 0;
private const USER_INACTIVITY_DAYS_MAX = 3650;
public static function get(string $key): ?array
public function get(string $key): ?array
{
return SettingRepository::find($key);
return $this->settingRepository->find($key);
}
public static function getValue(string $key): ?string
public function getValue(string $key): ?string
{
return SettingRepository::getValue($key);
return $this->settingRepository->getValue($key);
}
public static function getInt(string $key): ?int
public function getInt(string $key): ?int
{
$value = SettingRepository::getValue($key);
$value = $this->settingRepository->getValue($key);
if ($value === null || $value === '') {
return null;
}
return (int) $value;
}
public static function set(string $key, ?string $value, ?string $description = null): bool
public function set(string $key, ?string $value, ?string $description = null): bool
{
return SettingRepository::set($key, $value, $description);
return $this->settingRepository->set($key, $value, $description);
}
public static function getDefaultTenantId(): ?int
public function getDefaultTenantId(): ?int
{
$id = self::getInt(self::DEFAULT_TENANT_KEY);
$id = $this->getInt(self::DEFAULT_TENANT_KEY);
return $id && $id > 0 ? $id : null;
}
public static function setDefaultTenantId(?int $tenantId, ?string $description = null): bool
public function setDefaultTenantId(?int $tenantId, ?string $description = null): bool
{
$value = $tenantId && $tenantId > 0 ? (string) $tenantId : null;
if ($value !== null) {
$exists = TenantRepository::find($tenantId);
$exists = $this->tenantRepository->find($tenantId);
if (!$exists) {
return false;
}
}
$desc = $description ?? 'setting.default_tenant';
return SettingRepository::set(self::DEFAULT_TENANT_KEY, $value, $desc);
return $this->settingRepository->set(self::DEFAULT_TENANT_KEY, $value, $desc);
}
public static function getDefaultRoleId(): ?int
public function getDefaultRoleId(): ?int
{
$id = self::getInt(self::DEFAULT_ROLE_KEY);
$id = $this->getInt(self::DEFAULT_ROLE_KEY);
return $id && $id > 0 ? $id : null;
}
public static function setDefaultRoleId(?int $roleId, ?string $description = null): bool
public function setDefaultRoleId(?int $roleId, ?string $description = null): bool
{
$value = $roleId && $roleId > 0 ? (string) $roleId : null;
if ($value !== null) {
$exists = RoleRepository::find($roleId);
$exists = $this->roleRepository->find($roleId);
if (!$exists) {
return false;
}
}
$desc = $description ?? 'setting.default_role';
return SettingRepository::set(self::DEFAULT_ROLE_KEY, $value, $desc);
return $this->settingRepository->set(self::DEFAULT_ROLE_KEY, $value, $desc);
}
public static function getDefaultDepartmentId(): ?int
public function getDefaultDepartmentId(): ?int
{
$id = self::getInt(self::DEFAULT_DEPARTMENT_KEY);
$id = $this->getInt(self::DEFAULT_DEPARTMENT_KEY);
return $id && $id > 0 ? $id : null;
}
public static function setDefaultDepartmentId(?int $departmentId, ?string $description = null): bool
public function setDefaultDepartmentId(?int $departmentId, ?string $description = null): bool
{
$value = $departmentId && $departmentId > 0 ? (string) $departmentId : null;
if ($value !== null) {
$exists = DepartmentRepository::find($departmentId);
$exists = $this->departmentRepository->find($departmentId);
if (!$exists) {
return false;
}
}
$desc = $description ?? 'setting.default_department';
return SettingRepository::set(self::DEFAULT_DEPARTMENT_KEY, $value, $desc);
return $this->settingRepository->set(self::DEFAULT_DEPARTMENT_KEY, $value, $desc);
}
public static function getAppTitle(): ?string
public function getAppTitle(): ?string
{
$value = SettingRepository::getValue(self::APP_TITLE_KEY);
$value = $this->settingRepository->getValue(self::APP_TITLE_KEY);
$value = $value !== null ? trim($value) : null;
return $value !== '' ? $value : null;
}
public static function setAppTitle(?string $title, ?string $description = null): bool
public function setAppTitle(?string $title, ?string $description = null): bool
{
$value = $title !== null ? trim($title) : null;
if ($value === '') {
$value = null;
}
$desc = $description ?? 'setting.app_title';
return SettingRepository::set(self::APP_TITLE_KEY, $value, $desc);
return $this->settingRepository->set(self::APP_TITLE_KEY, $value, $desc);
}
public static function getAppLocale(): ?string
public function getAppLocale(): ?string
{
$value = SettingRepository::getValue(self::APP_LOCALE_KEY);
$value = $this->settingRepository->getValue(self::APP_LOCALE_KEY);
$value = $value !== null ? trim((string) $value) : '';
return $value !== '' ? $value : null;
}
public static function setAppLocale(?string $locale, ?string $description = null): bool
public function setAppLocale(?string $locale, ?string $description = null): bool
{
$value = $locale !== null ? trim((string) $locale) : '';
if ($value === '') {
@@ -160,108 +168,108 @@ class SettingService
}
}
$desc = $description ?? 'setting.app_locale';
return SettingRepository::set(self::APP_LOCALE_KEY, $value, $desc);
return $this->settingRepository->set(self::APP_LOCALE_KEY, $value, $desc);
}
public static function getAppTheme(): ?string
public function getAppTheme(): ?string
{
$value = SettingRepository::getValue(self::APP_THEME_KEY);
$value = $this->settingRepository->getValue(self::APP_THEME_KEY);
$value = $value !== null ? strtolower(trim((string) $value)) : '';
if (!in_array($value, self::allowedThemes(), true)) {
if (!in_array($value, $this->allowedThemes(), true)) {
return null;
}
return $value;
}
public static function setAppTheme(?string $theme, ?string $description = null): bool
public function setAppTheme(?string $theme, ?string $description = null): bool
{
$value = $theme !== null ? strtolower(trim((string) $theme)) : '';
if ($value === '' || !in_array($value, self::allowedThemes(), true)) {
if ($value === '' || !in_array($value, $this->allowedThemes(), true)) {
$value = null;
}
$desc = $description ?? 'setting.app_theme';
return SettingRepository::set(self::APP_THEME_KEY, $value, $desc);
return $this->settingRepository->set(self::APP_THEME_KEY, $value, $desc);
}
public static function isUserThemeAllowed(): bool
public function isUserThemeAllowed(): bool
{
$value = SettingRepository::getValue(self::APP_THEME_USER_KEY);
$value = $this->settingRepository->getValue(self::APP_THEME_USER_KEY);
if ($value === null || $value === '') {
return true;
}
return in_array(strtolower($value), ['1', 'true', 'yes', 'on'], true);
}
public static function setUserThemeAllowed(bool $allowed, ?string $description = null): bool
public function setUserThemeAllowed(bool $allowed, ?string $description = null): bool
{
$value = $allowed ? '1' : '0';
$desc = $description ?? 'setting.app_theme_user';
return SettingRepository::set(self::APP_THEME_USER_KEY, $value, $desc);
return $this->settingRepository->set(self::APP_THEME_USER_KEY, $value, $desc);
}
private static function allowedThemes(): array
private function allowedThemes(): array
{
return array_keys(ThemeConfigService::all());
return array_keys($this->themeConfigService->all());
}
public static function isRegistrationEnabled(): bool
public function isRegistrationEnabled(): bool
{
$value = SettingRepository::getValue(self::APP_REGISTRATION_KEY);
$value = $this->settingRepository->getValue(self::APP_REGISTRATION_KEY);
if ($value === null || $value === '') {
return true;
}
return in_array(strtolower($value), ['1', 'true', 'yes', 'on'], true);
}
public static function setRegistrationEnabled(bool $allowed, ?string $description = null): bool
public function setRegistrationEnabled(bool $allowed, ?string $description = null): bool
{
$value = $allowed ? '1' : '0';
$desc = $description ?? 'setting.app_registration';
return SettingRepository::set(self::APP_REGISTRATION_KEY, $value, $desc);
return $this->settingRepository->set(self::APP_REGISTRATION_KEY, $value, $desc);
}
public static function getApiTokenDefaultTtlDays(): int
public function getApiTokenDefaultTtlDays(): int
{
$maxDays = self::getApiTokenMaxTtlDays();
$value = self::getInt(self::API_TOKEN_DEFAULT_TTL_DAYS_KEY);
$maxDays = $this->getApiTokenMaxTtlDays();
$value = $this->getInt(self::API_TOKEN_DEFAULT_TTL_DAYS_KEY);
if ($value !== null && $value >= self::API_TOKEN_TTL_DAYS_MIN && $value <= self::API_TOKEN_TTL_DAYS_HARD_MAX) {
return min($value, $maxDays);
}
return min(self::API_TOKEN_DEFAULT_TTL_DAYS_FALLBACK, $maxDays);
}
public static function setApiTokenDefaultTtlDays(?int $days, ?string $description = null): bool
public function setApiTokenDefaultTtlDays(?int $days, ?string $description = null): bool
{
$value = $days ?? self::API_TOKEN_DEFAULT_TTL_DAYS_FALLBACK;
if ($value < self::API_TOKEN_TTL_DAYS_MIN || $value > self::API_TOKEN_TTL_DAYS_HARD_MAX) {
return false;
}
if ($value > self::getApiTokenMaxTtlDays()) {
if ($value > $this->getApiTokenMaxTtlDays()) {
return false;
}
$desc = $description ?? 'setting.api_token_default_ttl_days';
return SettingRepository::set(self::API_TOKEN_DEFAULT_TTL_DAYS_KEY, (string) $value, $desc);
return $this->settingRepository->set(self::API_TOKEN_DEFAULT_TTL_DAYS_KEY, (string) $value, $desc);
}
public static function getApiTokenMaxTtlDays(): int
public function getApiTokenMaxTtlDays(): int
{
$value = self::getInt(self::API_TOKEN_MAX_TTL_DAYS_KEY);
$value = $this->getInt(self::API_TOKEN_MAX_TTL_DAYS_KEY);
if ($value !== null && $value >= self::API_TOKEN_TTL_DAYS_MIN && $value <= self::API_TOKEN_TTL_DAYS_HARD_MAX) {
return $value;
}
return self::API_TOKEN_MAX_TTL_DAYS_FALLBACK;
}
public static function setApiTokenMaxTtlDays(?int $days, ?string $description = null): bool
public function setApiTokenMaxTtlDays(?int $days, ?string $description = null): bool
{
$value = $days ?? self::API_TOKEN_MAX_TTL_DAYS_FALLBACK;
if ($value < self::API_TOKEN_TTL_DAYS_MIN || $value > self::API_TOKEN_TTL_DAYS_HARD_MAX) {
return false;
}
$currentDefault = self::getInt(self::API_TOKEN_DEFAULT_TTL_DAYS_KEY);
$currentDefault = $this->getInt(self::API_TOKEN_DEFAULT_TTL_DAYS_KEY);
if ($currentDefault !== null && $currentDefault > $value) {
$defaultUpdated = SettingRepository::set(
$defaultUpdated = $this->settingRepository->set(
self::API_TOKEN_DEFAULT_TTL_DAYS_KEY,
(string) $value,
'setting.api_token_default_ttl_days'
@@ -272,26 +280,26 @@ class SettingService
}
$desc = $description ?? 'setting.api_token_max_ttl_days';
return SettingRepository::set(self::API_TOKEN_MAX_TTL_DAYS_KEY, (string) $value, $desc);
return $this->settingRepository->set(self::API_TOKEN_MAX_TTL_DAYS_KEY, (string) $value, $desc);
}
public static function getUserInactivityDeactivateDays(): int
public function getUserInactivityDeactivateDays(): int
{
$value = self::getInt(self::USER_INACTIVITY_DEACTIVATE_DAYS_KEY);
if ($value !== null && self::isValidInactivityDays($value)) {
$value = $this->getInt(self::USER_INACTIVITY_DEACTIVATE_DAYS_KEY);
if ($value !== null && $this->isValidInactivityDays($value)) {
return $value;
}
return self::USER_INACTIVITY_DEACTIVATE_DAYS_FALLBACK;
}
public static function setUserInactivityDeactivateDays(?int $days, ?string $description = null): bool
public function setUserInactivityDeactivateDays(?int $days, ?string $description = null): bool
{
$value = $days ?? self::USER_INACTIVITY_DEACTIVATE_DAYS_FALLBACK;
if (!self::isValidInactivityDays($value)) {
if (!$this->isValidInactivityDays($value)) {
return false;
}
if ($value === 0) {
$deleteUpdated = SettingRepository::set(
$deleteUpdated = $this->settingRepository->set(
self::USER_INACTIVITY_DELETE_DAYS_KEY,
'0',
'setting.user_inactivity_delete_days'
@@ -301,79 +309,79 @@ class SettingService
}
}
$desc = $description ?? 'setting.user_inactivity_deactivate_days';
return SettingRepository::set(self::USER_INACTIVITY_DEACTIVATE_DAYS_KEY, (string) $value, $desc);
return $this->settingRepository->set(self::USER_INACTIVITY_DEACTIVATE_DAYS_KEY, (string) $value, $desc);
}
public static function getUserInactivityDeleteDays(): int
public function getUserInactivityDeleteDays(): int
{
$deactivateDays = self::getUserInactivityDeactivateDays();
$deactivateDays = $this->getUserInactivityDeactivateDays();
if ($deactivateDays <= 0) {
return 0;
}
$value = self::getInt(self::USER_INACTIVITY_DELETE_DAYS_KEY);
if ($value !== null && self::isValidInactivityDays($value)) {
$value = $this->getInt(self::USER_INACTIVITY_DELETE_DAYS_KEY);
if ($value !== null && $this->isValidInactivityDays($value)) {
return $value;
}
return self::USER_INACTIVITY_DELETE_DAYS_FALLBACK;
}
public static function setUserInactivityDeleteDays(?int $days, ?string $description = null): bool
public function setUserInactivityDeleteDays(?int $days, ?string $description = null): bool
{
$value = $days ?? self::USER_INACTIVITY_DELETE_DAYS_FALLBACK;
if (!self::isValidInactivityDays($value)) {
if (!$this->isValidInactivityDays($value)) {
return false;
}
if ($value > 0 && self::getUserInactivityDeactivateDays() <= 0) {
if ($value > 0 && $this->getUserInactivityDeactivateDays() <= 0) {
return false;
}
$desc = $description ?? 'setting.user_inactivity_delete_days';
return SettingRepository::set(self::USER_INACTIVITY_DELETE_DAYS_KEY, (string) $value, $desc);
return $this->settingRepository->set(self::USER_INACTIVITY_DELETE_DAYS_KEY, (string) $value, $desc);
}
public static function getApiCorsAllowedOrigins(): array
public function getApiCorsAllowedOrigins(): array
{
$stored = SettingRepository::getValue(self::API_CORS_ALLOWED_ORIGINS_KEY);
$stored = $this->settingRepository->getValue(self::API_CORS_ALLOWED_ORIGINS_KEY);
if ($stored === null || trim($stored) === '') {
return [];
}
return self::parseCorsOrigins($stored, false) ?? [];
return $this->parseCorsOrigins($stored, false) ?? [];
}
public static function getApiCorsAllowedOriginsText(): string
public function getApiCorsAllowedOriginsText(): string
{
$origins = self::getApiCorsAllowedOrigins();
$origins = $this->getApiCorsAllowedOrigins();
return implode("\n", $origins);
}
public static function setApiCorsAllowedOrigins(?string $rawOrigins, ?string $description = null): bool
public function setApiCorsAllowedOrigins(?string $rawOrigins, ?string $description = null): bool
{
$value = (string) ($rawOrigins ?? '');
if (trim($value) === '') {
return SettingRepository::set(
return $this->settingRepository->set(
self::API_CORS_ALLOWED_ORIGINS_KEY,
null,
$description ?? 'setting.api_cors_allowed_origins'
);
}
$origins = self::parseCorsOrigins($value, true);
$origins = $this->parseCorsOrigins($value, true);
if ($origins === null) {
return false;
}
return SettingRepository::set(
return $this->settingRepository->set(
self::API_CORS_ALLOWED_ORIGINS_KEY,
implode("\n", $origins),
$description ?? 'setting.api_cors_allowed_origins'
);
}
private static function parseCorsOrigins(string $rawOrigins, bool $strict): ?array
private function parseCorsOrigins(string $rawOrigins, bool $strict): ?array
{
$parts = preg_split('/[\r\n,]+/', $rawOrigins) ?: [];
$normalizedOrigins = [];
foreach ($parts as $part) {
$origin = self::normalizeCorsOrigin($part);
$origin = $this->normalizeCorsOrigin($part);
if ($origin === null) {
if ($strict && trim((string) $part) !== '') {
return null;
@@ -385,7 +393,7 @@ class SettingService
return array_keys($normalizedOrigins);
}
private static function normalizeCorsOrigin(string $origin): ?string
private function normalizeCorsOrigin(string $origin): ?string
{
$origin = trim($origin);
if ($origin === '') {
@@ -426,33 +434,33 @@ class SettingService
return $scheme . '://' . $host . $portSuffix;
}
public static function getMicrosoftSharedClientId(): ?string
public function getMicrosoftSharedClientId(): ?string
{
$value = SettingRepository::getValue(self::MICROSOFT_SHARED_CLIENT_ID_KEY);
$value = $this->settingRepository->getValue(self::MICROSOFT_SHARED_CLIENT_ID_KEY);
$value = $value !== null ? trim((string) $value) : '';
return $value !== '' ? $value : null;
}
public static function setMicrosoftSharedClientId(?string $clientId, ?string $description = null): bool
public function setMicrosoftSharedClientId(?string $clientId, ?string $description = null): bool
{
$value = $clientId !== null ? trim((string) $clientId) : '';
if ($value === '') {
$value = null;
}
$desc = $description ?? 'setting.microsoft_shared_client_id';
return SettingRepository::set(self::MICROSOFT_SHARED_CLIENT_ID_KEY, $value, $desc);
return $this->settingRepository->set(self::MICROSOFT_SHARED_CLIENT_ID_KEY, $value, $desc);
}
public static function getMicrosoftSharedClientSecretEncrypted(): ?string
public function getMicrosoftSharedClientSecretEncrypted(): ?string
{
$value = SettingRepository::getValue(self::MICROSOFT_SHARED_CLIENT_SECRET_ENC_KEY);
$value = $this->settingRepository->getValue(self::MICROSOFT_SHARED_CLIENT_SECRET_ENC_KEY);
$value = $value !== null ? trim((string) $value) : '';
return $value !== '' ? $value : null;
}
public static function getMicrosoftSharedClientSecret(): ?string
public function getMicrosoftSharedClientSecret(): ?string
{
$encrypted = self::getMicrosoftSharedClientSecretEncrypted();
$encrypted = $this->getMicrosoftSharedClientSecretEncrypted();
if ($encrypted === null) {
return null;
}
@@ -465,11 +473,11 @@ class SettingService
return $decrypted !== '' ? $decrypted : null;
}
public static function setMicrosoftSharedClientSecret(?string $secret, ?string $description = null): bool
public function setMicrosoftSharedClientSecret(?string $secret, ?string $description = null): bool
{
$value = $secret !== null ? trim((string) $secret) : '';
if ($value === '') {
return SettingRepository::set(
return $this->settingRepository->set(
self::MICROSOFT_SHARED_CLIENT_SECRET_ENC_KEY,
null,
$description ?? 'setting.microsoft_shared_client_secret_enc'
@@ -477,12 +485,12 @@ class SettingService
}
$encrypted = Crypto::encryptString($value);
$desc = $description ?? 'setting.microsoft_shared_client_secret_enc';
return SettingRepository::set(self::MICROSOFT_SHARED_CLIENT_SECRET_ENC_KEY, $encrypted, $desc);
return $this->settingRepository->set(self::MICROSOFT_SHARED_CLIENT_SECRET_ENC_KEY, $encrypted, $desc);
}
public static function getMicrosoftAuthority(): string
public function getMicrosoftAuthority(): string
{
$value = SettingRepository::getValue(self::MICROSOFT_AUTHORITY_KEY);
$value = $this->settingRepository->getValue(self::MICROSOFT_AUTHORITY_KEY);
$value = trim((string) ($value ?? ''));
if ($value === '') {
return 'https://login.microsoftonline.com/common/v2.0';
@@ -490,7 +498,7 @@ class SettingService
return rtrim($value, '/');
}
public static function setMicrosoftAuthority(?string $authority, ?string $description = null): bool
public function setMicrosoftAuthority(?string $authority, ?string $description = null): bool
{
$value = trim((string) ($authority ?? ''));
if ($value === '') {
@@ -500,12 +508,12 @@ class SettingService
return false;
}
$desc = $description ?? 'setting.microsoft_authority';
return SettingRepository::set(self::MICROSOFT_AUTHORITY_KEY, rtrim($value, '/'), $desc);
return $this->settingRepository->set(self::MICROSOFT_AUTHORITY_KEY, rtrim($value, '/'), $desc);
}
public static function getAppPrimaryColor(): ?string
public function getAppPrimaryColor(): ?string
{
$value = SettingRepository::getValue(self::APP_PRIMARY_COLOR_KEY);
$value = $this->settingRepository->getValue(self::APP_PRIMARY_COLOR_KEY);
$value = $value !== null ? strtolower(trim((string) $value)) : '';
if ($value === '') {
return null;
@@ -516,7 +524,7 @@ class SettingService
return $value;
}
public static function setAppPrimaryColor(?string $color, ?string $description = null): bool
public function setAppPrimaryColor(?string $color, ?string $description = null): bool
{
$value = $color !== null ? strtolower(trim((string) $color)) : '';
if ($value !== '' && $value[0] !== '#') {
@@ -533,29 +541,29 @@ class SettingService
return false;
}
$desc = $description ?? 'setting.app_primary_color';
return SettingRepository::set(self::APP_PRIMARY_COLOR_KEY, $value, $desc);
return $this->settingRepository->set(self::APP_PRIMARY_COLOR_KEY, $value, $desc);
}
public static function getSmtpHost(): ?string
public function getSmtpHost(): ?string
{
$value = SettingRepository::getValue(self::SMTP_HOST_KEY);
$value = $this->settingRepository->getValue(self::SMTP_HOST_KEY);
$value = $value !== null ? trim((string) $value) : '';
return $value !== '' ? $value : null;
}
public static function setSmtpHost(?string $host, ?string $description = null): bool
public function setSmtpHost(?string $host, ?string $description = null): bool
{
$value = $host !== null ? trim((string) $host) : '';
if ($value === '') {
$value = null;
}
$desc = $description ?? 'setting.smtp_host';
return SettingRepository::set(self::SMTP_HOST_KEY, $value, $desc);
return $this->settingRepository->set(self::SMTP_HOST_KEY, $value, $desc);
}
public static function getSmtpPort(): ?int
public function getSmtpPort(): ?int
{
$value = SettingRepository::getValue(self::SMTP_PORT_KEY);
$value = $this->settingRepository->getValue(self::SMTP_PORT_KEY);
if ($value === null || $value === '') {
return null;
}
@@ -563,50 +571,50 @@ class SettingService
return $port > 0 ? $port : null;
}
public static function setSmtpPort(?int $port, ?string $description = null): bool
public function setSmtpPort(?int $port, ?string $description = null): bool
{
$value = $port && $port > 0 ? (string) $port : null;
$desc = $description ?? 'setting.smtp_port';
return SettingRepository::set(self::SMTP_PORT_KEY, $value, $desc);
return $this->settingRepository->set(self::SMTP_PORT_KEY, $value, $desc);
}
public static function getSmtpUser(): ?string
public function getSmtpUser(): ?string
{
$value = SettingRepository::getValue(self::SMTP_USER_KEY);
$value = $this->settingRepository->getValue(self::SMTP_USER_KEY);
$value = $value !== null ? trim((string) $value) : '';
return $value !== '' ? $value : null;
}
public static function setSmtpUser(?string $user, ?string $description = null): bool
public function setSmtpUser(?string $user, ?string $description = null): bool
{
$value = $user !== null ? trim((string) $user) : '';
if ($value === '') {
$value = null;
}
$desc = $description ?? 'setting.smtp_user';
return SettingRepository::set(self::SMTP_USER_KEY, $value, $desc);
return $this->settingRepository->set(self::SMTP_USER_KEY, $value, $desc);
}
public static function getSmtpPassword(): ?string
public function getSmtpPassword(): ?string
{
$value = SettingRepository::getValue(self::SMTP_PASSWORD_KEY);
$value = $this->settingRepository->getValue(self::SMTP_PASSWORD_KEY);
$value = $value !== null ? (string) $value : '';
return $value !== '' ? $value : null;
}
public static function setSmtpPassword(?string $password, ?string $description = null): bool
public function setSmtpPassword(?string $password, ?string $description = null): bool
{
$value = $password !== null ? (string) $password : '';
if ($value === '') {
$value = null;
}
$desc = $description ?? 'setting.smtp_password';
return SettingRepository::set(self::SMTP_PASSWORD_KEY, $value, $desc);
return $this->settingRepository->set(self::SMTP_PASSWORD_KEY, $value, $desc);
}
public static function getSmtpSecure(): ?string
public function getSmtpSecure(): ?string
{
$value = SettingRepository::getValue(self::SMTP_SECURE_KEY);
$value = $this->settingRepository->getValue(self::SMTP_SECURE_KEY);
$value = $value !== null ? strtolower(trim((string) $value)) : '';
if (!in_array($value, ['tls', 'ssl', 'none'], true)) {
return null;
@@ -614,7 +622,7 @@ class SettingService
return $value === 'none' ? null : $value;
}
public static function setSmtpSecure(?string $secure, ?string $description = null): bool
public function setSmtpSecure(?string $secure, ?string $description = null): bool
{
$value = $secure !== null ? strtolower(trim((string) $secure)) : '';
if ($value === '' || $value === 'none') {
@@ -623,44 +631,44 @@ class SettingService
return false;
}
$desc = $description ?? 'setting.smtp_secure';
return SettingRepository::set(self::SMTP_SECURE_KEY, $value, $desc);
return $this->settingRepository->set(self::SMTP_SECURE_KEY, $value, $desc);
}
public static function getSmtpFrom(): ?string
public function getSmtpFrom(): ?string
{
$value = SettingRepository::getValue(self::SMTP_FROM_KEY);
$value = $this->settingRepository->getValue(self::SMTP_FROM_KEY);
$value = $value !== null ? trim((string) $value) : '';
return $value !== '' ? $value : null;
}
public static function setSmtpFrom(?string $from, ?string $description = null): bool
public function setSmtpFrom(?string $from, ?string $description = null): bool
{
$value = $from !== null ? trim((string) $from) : '';
if ($value === '') {
$value = null;
}
$desc = $description ?? 'setting.smtp_from';
return SettingRepository::set(self::SMTP_FROM_KEY, $value, $desc);
return $this->settingRepository->set(self::SMTP_FROM_KEY, $value, $desc);
}
public static function getSmtpFromName(): ?string
public function getSmtpFromName(): ?string
{
$value = SettingRepository::getValue(self::SMTP_FROM_NAME_KEY);
$value = $this->settingRepository->getValue(self::SMTP_FROM_NAME_KEY);
$value = $value !== null ? trim((string) $value) : '';
return $value !== '' ? $value : null;
}
public static function setSmtpFromName(?string $fromName, ?string $description = null): bool
public function setSmtpFromName(?string $fromName, ?string $description = null): bool
{
$value = $fromName !== null ? trim((string) $fromName) : '';
if ($value === '') {
$value = null;
}
$desc = $description ?? 'setting.smtp_from_name';
return SettingRepository::set(self::SMTP_FROM_NAME_KEY, $value, $desc);
return $this->settingRepository->set(self::SMTP_FROM_NAME_KEY, $value, $desc);
}
private static function isValidInactivityDays(int $days): bool
private function isValidInactivityDays(int $days): bool
{
return $days >= self::USER_INACTIVITY_DAYS_MIN && $days <= self::USER_INACTIVITY_DAYS_MAX;
}

View File

@@ -0,0 +1,66 @@
<?php
namespace MintyPHP\Service\Settings;
use MintyPHP\Repository\Access\RoleRepository;
use MintyPHP\Repository\Org\DepartmentRepository;
use MintyPHP\Repository\Settings\SettingRepository;
use MintyPHP\Repository\Tenant\TenantRepository;
class SettingServicesFactory
{
private ?SettingRepository $settingRepository = null;
private ?TenantRepository $tenantRepository = null;
private ?RoleRepository $roleRepository = null;
private ?DepartmentRepository $departmentRepository = null;
private ?ThemeConfigService $themeConfigService = null;
private ?SettingService $settingService = null;
private ?SettingCacheService $settingCacheService = null;
private ?SettingGateway $settingGateway = null;
public function createSettingRepository(): SettingRepository
{
return $this->settingRepository ??= new SettingRepository();
}
public function createSettingService(): SettingService
{
return $this->settingService ??= new SettingService(
$this->createSettingRepository(),
$this->createTenantRepository(),
$this->createRoleRepository(),
$this->createDepartmentRepository(),
$this->createThemeConfigService()
);
}
public function createSettingGateway(): SettingGateway
{
return $this->settingGateway ??= new SettingGateway($this->createSettingService());
}
public function createSettingCacheService(): SettingCacheService
{
return $this->settingCacheService ??= new SettingCacheService();
}
public function createThemeConfigService(): ThemeConfigService
{
return $this->themeConfigService ??= new ThemeConfigService();
}
private function createTenantRepository(): TenantRepository
{
return $this->tenantRepository ??= new TenantRepository();
}
private function createRoleRepository(): RoleRepository
{
return $this->roleRepository ??= new RoleRepository();
}
private function createDepartmentRepository(): DepartmentRepository
{
return $this->departmentRepository ??= new DepartmentRepository();
}
}

View File

@@ -9,24 +9,24 @@ class ThemeConfigService
'dark' => 'Dark',
];
private static ?array $themes = null;
private ?array $themes = null;
public static function all(): array
public function all(): array
{
if (self::$themes !== null) {
return self::$themes;
if ($this->themes !== null) {
return $this->themes;
}
$file = dirname(__DIR__, 3) . '/config/themes.php';
if (!is_file($file)) {
self::$themes = self::DEFAULT_THEMES;
return self::$themes;
$this->themes = self::DEFAULT_THEMES;
return $this->themes;
}
$loaded = include $file;
if (!is_array($loaded)) {
self::$themes = self::DEFAULT_THEMES;
return self::$themes;
$this->themes = self::DEFAULT_THEMES;
return $this->themes;
}
$clean = [];
@@ -39,7 +39,7 @@ class ThemeConfigService
$clean[$themeKey] = $themeLabel;
}
self::$themes = $clean ?: self::DEFAULT_THEMES;
return self::$themes;
$this->themes = $clean ?: self::DEFAULT_THEMES;
return $this->themes;
}
}