From cb05040c96830f914e1a1803b6ff00cd58cc53b5 Mon Sep 17 00:00:00 2001 From: idevakk <219866223+idevakk@users.noreply.github.com> Date: Sun, 16 Nov 2025 10:59:42 -0800 Subject: [PATCH] feat: add legacy setting support to ease migration --- app/Helpers/ArrayHelper.php | 41 ++++++++++ app/Models/Domain.php | 27 +++++++ app/Models/Username.php | 34 +++++++++ app/Providers/AppServiceProvider.php | 107 ++++++++++++++++++++++++++- 4 files changed, 205 insertions(+), 4 deletions(-) create mode 100644 app/Helpers/ArrayHelper.php diff --git a/app/Helpers/ArrayHelper.php b/app/Helpers/ArrayHelper.php new file mode 100644 index 0000000..e74e653 --- /dev/null +++ b/app/Helpers/ArrayHelper.php @@ -0,0 +1,41 @@ +getMessage()); + + // Fallback: return empty object instead of crashing + return '{}'; + } + } +} diff --git a/app/Models/Domain.php b/app/Models/Domain.php index 8a2d603..edcb85a 100644 --- a/app/Models/Domain.php +++ b/app/Models/Domain.php @@ -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(), + ]; + } } diff --git a/app/Models/Username.php b/app/Models/Username.php index fb870e9..b5b0a49 100644 --- a/app/Models/Username.php +++ b/app/Models/Username.php @@ -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, + ]; + } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 34c3771..6f9dc23 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -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(); @@ -39,15 +49,16 @@ class AppServiceProvider extends ServiceProvider private function loadApplicationData(): void { try { - $settings = cache()->remember('app_settings', now()->addHours(6), fn(): array => (array) DB::table('settings')->find(1)); + $settings = cache()->remember('app_settings', now()->addHours(6), fn (): array => (array) DB::table('settings')->find(1)); $menus = cache()->remember('app_menus', now()->addHours(6), Menu::all(...)); - $blogs = cache()->remember('app_blogs', now()->addHours(6), fn() => Blog::query()->where('is_published', 1)->get()); + $blogs = cache()->remember('app_blogs', now()->addHours(6), fn () => Blog::query()->where('is_published', 1)->get()); $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; + } }