feat: add legacy setting support to ease migration

This commit is contained in:
idevakk
2025-11-16 10:59:42 -08:00
parent e3da8cf950
commit cb05040c96
4 changed files with 205 additions and 4 deletions

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Helpers;
use Illuminate\Support\Facades\Log;
class ArrayHelper
{
/**
* Get value from array using dot notation.
*/
public static function getValueFromArray(string $key, array $array)
{
$keys = explode('.', $key);
foreach ($keys as $segment) {
if (!isset($array[$segment])) {
return null;
}
$array = $array[$segment];
}
return $array;
}
public static function jsonEncodeSafe(mixed $value): string
{
try {
return json_encode(
$value,
JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
);
} catch (\JsonException $e) {
// Optional: Log the error
Log::error("JSON encode failed: " . $e->getMessage());
// Fallback: return empty object instead of crashing
return '{}';
}
}
}

View File

@@ -68,4 +68,31 @@ class Domain extends Model
return $query->pluck('name')->toArray();
}
/**
* Retrieve all active domains grouped by type.
*
* @return array Array with 'domains' and 'premium_domains' keys
*/
public static function getActiveDomains(): array
{
$domains = static::query()
->where('is_active', true)
->where(function ($query) {
$query->whereNull('starts_at')
->orWhere('starts_at', '<=', now());
})
->where(function ($query) {
$query->whereNull('ends_at')
->orWhere('ends_at', '>=', now());
})
->select('name', 'domain_type')
->get()
->groupBy('domain_type');
return [
'domains' => $domains->get(DomainType::PUBLIC->value, collect())->pluck('name')->toArray(),
'premium_domains' => $domains->get(DomainType::PREMIUM->value, collect())->pluck('name')->toArray(),
];
}
}

View File

@@ -69,4 +69,38 @@ class Username extends Model
return $query->pluck('username')->toArray();
}
/**
* Retrieve all active usernames grouped by type and provider.
*
* @return array Array with gmailUsernames, premium_gmailUsernames, outlookUsernames, premium_outlookUsernames keys
*/
public static function getActiveUsernames(): array
{
$usernames = static::query()
->where('is_active', true)
->where(function ($query) {
$query->whereNull('starts_at')
->orWhere('starts_at', '<=', now());
})
->where(function ($query) {
$query->whereNull('ends_at')
->orWhere('ends_at', '>=', now());
})
->select('username', 'username_type', 'provider_type')
->get()
->groupBy(['provider_type', 'username_type']);
$gmailPublic = $usernames->get(ProviderType::GMAIL->value, collect())->get(UsernameType::PUBLIC->value, collect())->pluck('username')->toArray();
$gmailPremium = $usernames->get(ProviderType::GMAIL->value, collect())->get(UsernameType::PREMIUM->value, collect())->pluck('username')->toArray();
$outlookPublic = $usernames->get(ProviderType::OUTLOOK->value, collect())->get(UsernameType::PUBLIC->value, collect())->pluck('username')->toArray();
$outlookPremium = $usernames->get(ProviderType::OUTLOOK->value, collect())->get(UsernameType::PREMIUM->value, collect())->pluck('username')->toArray();
return [
'gmailUsernames' => $gmailPublic,
'premium_gmailUsernames' => $gmailPremium,
'outlookUsernames' => $outlookPublic,
'premium_outlookUsernames' => $outlookPremium,
];
}
}

View File

@@ -2,16 +2,23 @@
namespace App\Providers;
use Illuminate\Support\Facades\DB;
use App\Helpers\ArrayHelper;
use App\Models\Blog;
use App\Models\Domain;
use App\Models\Menu;
use App\Models\Plan;
use App\Models\Username;
use Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use Inerba\DbConfig\DbConfig;
use Laravel\Cashier\Cashier;
class AppServiceProvider extends ServiceProvider
{
private array $appConfig;
/**
* Register any application services.
*/
@@ -25,6 +32,9 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot(): void
{
$this->loadConfiguration();
$this->loadDomainUsernameData();
// Only load application data when not in testing environment
if (! $this->app->environment('testing')) {
$this->loadApplicationData();
@@ -47,7 +57,8 @@ class AppServiceProvider extends ServiceProvider
$plans = cache()->remember('app_plans', now()->addHours(6), Plan::all(...));
config(['app.settings' => (array) $settings]);
$legacySettings = cache()->remember('legacy_app_settings', now()->addHours(6), fn (): array => $this->loadLegacySettings());
config(['app.settings' => (array) $legacySettings]);
config(['app.menus' => $menus]);
config(['app.blogs' => $blogs]);
config(['app.plans' => $plans]);
@@ -56,4 +67,92 @@ class AppServiceProvider extends ServiceProvider
// This allows the application to boot during migrations and testing
}
}
private function loadConfiguration(): void
{
try {
$websiteGroup = DbConfig::getGroup('website') ?? [];
$imapGroup = DbConfig::getGroup('imap') ?? [];
$configurationGroup = DbConfig::getGroup('configuration') ?? [];
$this->appConfig['website_settings'] = (in_array($websiteGroup, [null, []], true)) ? [] : $websiteGroup;
$this->appConfig['imap_settings'] = (in_array($imapGroup, [null, []], true)) ? [] : $imapGroup;
$this->appConfig['configuration_settings'] = (in_array($configurationGroup, [null, []], true)) ? [] : $configurationGroup;
} catch (\Exception $e) {
$this->appConfig = [
'website_settings' => [],
'imap_settings' => [],
'configuration_settings' => []
];
Log::error($e->getMessage());
}
}
private function loadDomainUsernameData(): void
{
try {
// Use new array scopes to get data in the desired format
$domains = Domain::getActiveDomains();
$usernames = Username::getActiveUsernames();
// Merge into domain config for legacy compatibility
$domainUsernameConfig = array_merge($domains, $usernames);
$this->appConfig['configuration_settings'] = array_merge($this->appConfig['configuration_settings'], $domainUsernameConfig);
} catch (\Exception $e) {
$domainUsernameConfig = [
'domains' => [],
'premium_domains' => [],
'gmailUsernames' => [],
'premium_gmailUsernames' => [],
'outlookUsernames' => [],
'premium_outlookUsernames' => [],
];
$this->appConfig['configuration_settings'] = array_merge($this->appConfig['configuration_settings'], $domainUsernameConfig);
Log::error($e->getMessage());
}
}
private function loadLegacySettings(): array
{
return [
"app_name" => $this->getConfig('website_settings.app_name'),
"app_version" => $this->getConfig('website_settings.app_version'),
"app_base_url" => $this->getConfig('website_settings.app_base_url'),
"app_admin" => $this->getConfig('website_settings.app_admin'),
"app_title" => $this->getConfig('website_settings.app_title'),
"app_description" => $this->getConfig('website_settings.app_description'),
"app_keywords" => $this->getConfig('website_settings.app_keywords'),
"app_contact" => $this->getConfig('website_settings.app_contact'),
"app_meta" => ArrayHelper::jsonEncodeSafe($this->getConfig('website_settings.app_meta')),
"app_social" => ArrayHelper::jsonEncodeSafe($this->getConfig('website_settings.app_social')),
"app_header" => $this->getConfig('website_settings.app_header'),
"app_footer" => $this->getConfig('website_settings.app_footer'),
"imap_settings" => ArrayHelper::jsonEncodeSafe([
"host" => $this->getConfig('imap_settings.public.host'),
"port" => $this->getConfig('imap_settings.public.port'),
"username" => $this->getConfig('imap_settings.public.username'),
"password" => $this->getConfig('imap_settings.public.password'),
"encryption" => $this->getConfig('imap_settings.public.encryption'),
"validate_cert" => $this->getConfig('imap_settings.public.validate_cert'),
"default_account" => $this->getConfig('imap_settings.public.default_account'),
"protocol" => $this->getConfig('imap_settings.public.protocol'),
"cc_check" => $this->getConfig('imap_settings.public.cc_check'),
"premium_host" => $this->getConfig('imap_settings.premium.host'),
"premium_port" => $this->getConfig('imap_settings.premium.port'),
"premium_username" => $this->getConfig('imap_settings.premium.username'),
"premium_password" => $this->getConfig('imap_settings.premium.password'),
"premium_encryption" => $this->getConfig('imap_settings.premium.premium_encryption'),
"premium_validate_cert" => $this->getConfig('imap_settings.premium.validate_cert'),
"premium_default_account" => $this->getConfig('imap_settings.premium.default_account'),
"premium_protocol" => $this->getConfig('imap_settings.premium.protocol'),
"premium_cc_check" => $this->getConfig('imap_settings.premium.cc_check'),
]),
"configuration_settings" => ArrayHelper::jsonEncodeSafe($this->appConfig['configuration_settings']),
"ads_settings" => ArrayHelper::jsonEncodeSafe($this->getConfig('website_settings.ads_settings')),
];
}
private function getConfig(string $key, $default = null)
{
return ArrayHelper::getValueFromArray($key, $this->appConfig) ?? $default;
}
}