mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-04-15 09:14:08 +00:00
Remove duplicate configureMediaDisk() from AppServiceProvider — all FileDisk and media-library config is now in AppConfigProvider's configureFileSystemFromDatabase(). Replace setConfig() calls with inline config registration everywhere to avoid mutating filesystems.default, which caused infinite request loops on the File Disk admin page.
193 lines
7.9 KiB
PHP
193 lines
7.9 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Models\FileDisk;
|
|
use App\Models\Setting;
|
|
use App\Services\Setup\InstallUtils;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
class AppConfigProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Bootstrap services.
|
|
*/
|
|
public function boot(): void
|
|
{
|
|
// Check if database is available
|
|
if (! InstallUtils::isDbCreated()) {
|
|
return;
|
|
}
|
|
|
|
$this->configureMailFromDatabase();
|
|
$this->configurePDFFromDatabase();
|
|
$this->configureFileSystemFromDatabase();
|
|
}
|
|
|
|
/**
|
|
* Configure mail settings from database
|
|
*/
|
|
protected function configureMailFromDatabase(): void
|
|
{
|
|
try {
|
|
// Get mail settings from database
|
|
$mailSettings = Setting::getSettings([
|
|
'mail_driver',
|
|
'mail_host',
|
|
'mail_port',
|
|
'mail_username',
|
|
'mail_password',
|
|
'mail_encryption',
|
|
'mail_scheme',
|
|
'mail_url',
|
|
'mail_timeout',
|
|
'mail_local_domain',
|
|
'from_name',
|
|
'from_mail',
|
|
'mail_mailgun_domain',
|
|
'mail_mailgun_secret',
|
|
'mail_mailgun_endpoint',
|
|
'mail_mailgun_scheme',
|
|
'mail_ses_key',
|
|
'mail_ses_secret',
|
|
'mail_ses_region',
|
|
'mail_sendmail_path',
|
|
]);
|
|
|
|
if (! empty($mailSettings['mail_driver'])) {
|
|
$driver = $mailSettings['mail_driver'];
|
|
|
|
// Set default mailer
|
|
Config::set('mail.default', $driver);
|
|
|
|
// Configure based on driver
|
|
switch ($driver) {
|
|
case 'smtp':
|
|
Config::set('mail.mailers.smtp.host', $mailSettings['mail_host'] ?? '127.0.0.1');
|
|
Config::set('mail.mailers.smtp.port', $mailSettings['mail_port'] ?? 2525);
|
|
Config::set('mail.mailers.smtp.username', $mailSettings['mail_username'] ?? null);
|
|
Config::set('mail.mailers.smtp.password', $mailSettings['mail_password'] ?? null);
|
|
Config::set('mail.mailers.smtp.encryption', $mailSettings['mail_encryption'] ?? 'none');
|
|
Config::set('mail.mailers.smtp.scheme', $mailSettings['mail_scheme'] ?? null);
|
|
Config::set('mail.mailers.smtp.url', $mailSettings['mail_url'] ?? null);
|
|
Config::set('mail.mailers.smtp.timeout', $mailSettings['mail_timeout'] ?? null);
|
|
Config::set('mail.mailers.smtp.local_domain', $mailSettings['mail_local_domain'] ?? null);
|
|
break;
|
|
|
|
case 'mailgun':
|
|
Config::set('mail.mailers.mailgun.domain', $mailSettings['mail_mailgun_domain'] ?? null);
|
|
Config::set('mail.mailers.mailgun.secret', $mailSettings['mail_mailgun_secret'] ?? null);
|
|
Config::set('mail.mailers.mailgun.endpoint', $mailSettings['mail_mailgun_endpoint'] ?? 'api.mailgun.net');
|
|
Config::set('mail.mailers.mailgun.scheme', $mailSettings['mail_mailgun_scheme'] ?? 'https');
|
|
|
|
// Also set services config for mailgun
|
|
Config::set('services.mailgun.domain', $mailSettings['mail_mailgun_domain'] ?? null);
|
|
Config::set('services.mailgun.secret', $mailSettings['mail_mailgun_secret'] ?? null);
|
|
Config::set('services.mailgun.endpoint', $mailSettings['mail_mailgun_endpoint'] ?? 'api.mailgun.net');
|
|
break;
|
|
|
|
case 'ses':
|
|
Config::set('services.ses.key', $mailSettings['mail_ses_key'] ?? null);
|
|
Config::set('services.ses.secret', $mailSettings['mail_ses_secret'] ?? null);
|
|
Config::set('services.ses.region', $mailSettings['mail_ses_region'] ?? 'us-east-1');
|
|
break;
|
|
|
|
case 'sendmail':
|
|
Config::set('mail.mailers.sendmail.path', $mailSettings['mail_sendmail_path'] ?? '/usr/sbin/sendmail -bs -i');
|
|
break;
|
|
}
|
|
|
|
// Set global from address and name
|
|
if (! empty($mailSettings['from_mail'])) {
|
|
Config::set('mail.from.address', $mailSettings['from_mail']);
|
|
}
|
|
if (! empty($mailSettings['from_name'])) {
|
|
Config::set('mail.from.name', $mailSettings['from_name']);
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
// Silently fail if database is not available (during installation, migrations, etc.)
|
|
// This prevents the application from breaking during setup
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Configure PDF settings from database
|
|
*/
|
|
protected function configurePDFFromDatabase(): void
|
|
{
|
|
try {
|
|
// Get PDF settings from database
|
|
$pdfSettings = Setting::getSettings([
|
|
'pdf_driver',
|
|
'gotenberg_host',
|
|
'gotenberg_papersize',
|
|
'gotenberg_margins',
|
|
]);
|
|
|
|
if (! empty($pdfSettings['pdf_driver'])) {
|
|
$driver = $pdfSettings['pdf_driver'];
|
|
|
|
// Set PDF driver
|
|
Config::set('pdf.driver', $driver);
|
|
|
|
// Configure based on driver
|
|
switch ($driver) {
|
|
case 'gotenberg':
|
|
if (! empty($pdfSettings['gotenberg_host'])) {
|
|
Config::set('pdf.connections.gotenberg.host', $pdfSettings['gotenberg_host']);
|
|
}
|
|
if (! empty($pdfSettings['gotenberg_papersize'])) {
|
|
Config::set('pdf.connections.gotenberg.papersize', $pdfSettings['gotenberg_papersize']);
|
|
}
|
|
if (! empty($pdfSettings['gotenberg_margins'])) {
|
|
Config::set('pdf.connections.gotenberg.margins', $pdfSettings['gotenberg_margins']);
|
|
}
|
|
break;
|
|
|
|
case 'dompdf':
|
|
// dompdf doesn't have additional configuration in the current setup
|
|
break;
|
|
}
|
|
}
|
|
} catch (\Exception $e) {
|
|
// Silently fail if database is not available (during installation, migrations, etc.)
|
|
// This prevents the application from breaking during setup
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Configure file system settings from database
|
|
*/
|
|
protected function configureFileSystemFromDatabase(): void
|
|
{
|
|
try {
|
|
// Register the default file disk config without changing filesystems.default.
|
|
// Calling setConfig() mutates the global default which causes side effects
|
|
// on pages that make multiple API requests (e.g., File Disk admin page).
|
|
$fileDisk = FileDisk::whereSetAsDefault(true)->first();
|
|
|
|
if ($fileDisk) {
|
|
$prefix = env('DYNAMIC_DISK_PREFIX', 'temp_');
|
|
$diskName = $prefix.$fileDisk->driver;
|
|
$credentials = collect(json_decode($fileDisk->credentials));
|
|
$baseConfig = config('filesystems.disks.'.$fileDisk->driver, []);
|
|
|
|
foreach ($baseConfig as $key => $value) {
|
|
if ($credentials->has($key)) {
|
|
$baseConfig[$key] = $credentials[$key];
|
|
}
|
|
}
|
|
|
|
config(['filesystems.disks.'.$diskName => $baseConfig]);
|
|
|
|
// Point Spatie Media Library at the same disk
|
|
config(['media-library.disk_name' => $diskName]);
|
|
}
|
|
} catch (\Exception $e) {
|
|
// Silently fail if database is not available (during installation, migrations, etc.)
|
|
}
|
|
}
|
|
}
|