Skip to content

Commit

Permalink
tons of bugfixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
roncodes committed Aug 30, 2024
1 parent e0c00ef commit 7f5f189
Show file tree
Hide file tree
Showing 6 changed files with 381 additions and 240 deletions.
61 changes: 29 additions & 32 deletions src/Http/Controllers/Internal/v1/SettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Illuminate\Http\Request;
use Illuminate\Notifications\AnonymousNotifiable;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -89,9 +90,9 @@ public function saveFilesystemConfig(AdminRequest $request)
$s3 = $request->input('s3', config('filesystems.disks.s3'));
$gcs = static::_getGcsConfigWithIcomingRequest($request);

Setting::configure('system.filesystem.driver', $driver);
Setting::configure('system.filesystem.gcs', $gcs);
Setting::configure('system.filesystem.s3', array_merge(config('filesystems.disks.s3', []), $s3));
Setting::configureSystem('filesystem.driver', $driver);
Setting::configureSystem('filesystem.gcs', $gcs);
Setting::configureSystem('filesystem.s3', array_merge(config('filesystems.disks.s3', []), $s3));

return response()->json(['status' => 'OK']);
}
Expand Down Expand Up @@ -172,7 +173,7 @@ public function testFilesystemConfig(AdminRequest $request)
// set config values from input
config(['filesystems.default' => $disk]);
try {
$uploaded = Storage::disk($disk)->put('testfile.txt', 'Hello World');
Storage::disk($disk)->put('testfile.txt', 'Hello World');
} catch (\Aws\S3\Exception\S3Exception $e) {
$message = $e->getMessage();
$status = 'error';
Expand All @@ -181,6 +182,14 @@ public function testFilesystemConfig(AdminRequest $request)
$status = 'error';
}

// confirm file uploaded
try {
$uploaded = Storage::disk($disk)->exists('testfile.txt');
} catch (\Throwable $e) {
$message = $e->getMessage();
$status = 'error';
}

// sometimes there is no error but file is not uploaded
if (!$uploaded) {
$status = 'error';
Expand Down Expand Up @@ -231,9 +240,9 @@ public function saveMailConfig(AdminRequest $request)
$from = $request->input('from', []);
$smtp = $request->input('smtp', []);

Setting::configure('system.mail.mailer', $mailer);
Setting::configure('system.mail.from', $from);
Setting::configure('system.mail.smtp', array_merge(['transport' => 'smtp'], $smtp));
Setting::configureSystem('mail.mailer', $mailer);
Setting::configureSystem('mail.from', $from);
Setting::configureSystem('mail.smtp', array_merge(['transport' => 'smtp'], $smtp));

return response()->json(['status' => 'OK']);
}
Expand Down Expand Up @@ -269,13 +278,7 @@ public function testMailConfig(AdminRequest $request)

try {
Mail::send(new \Fleetbase\Mail\TestMail($user));
} catch (\Aws\Ses\Exception\SesException $e) {
$message = $e->getMessage();
$status = 'error';
} catch (\Swift_TransportException $e) {
$message = $e->getMessage();
$status = 'error';
} catch (\Throwable $e) {
} catch (\Aws\Ses\Exception\SesException|\Exception $e) {
$message = $e->getMessage();
$status = 'error';
}
Expand Down Expand Up @@ -322,9 +325,9 @@ public function saveQueueConfig(AdminRequest $request)
$sqs = $request->input('sqs', config('queue.connections.sqs'));
$beanstalkd = $request->input('beanstalkd', config('queue.connections.beanstalkd'));

Setting::configure('system.queue.driver', $driver);
Setting::configure('system.queue.sqs', array_merge(config('queue.connections.sqs'), $sqs));
Setting::configure('system.queue.beanstalkd', array_merge(config('queue.connections.beanstalkd'), $beanstalkd));
Setting::configureSystem('queue.driver', $driver);
Setting::configureSystem('queue.sqs', array_merge(config('queue.connections.sqs'), $sqs));
Setting::configureSystem('queue.beanstalkd', array_merge(config('queue.connections.beanstalkd'), $beanstalkd));

return response()->json(['status' => 'OK']);
}
Expand All @@ -346,17 +349,11 @@ public function testQueueConfig(AdminRequest $request)
config(['queue.default' => $queue]);

try {
\Illuminate\Support\Facades\Queue::pushRaw(new \Illuminate\Support\MessageBag(['Hello World']));
Queue::pushRaw(json_encode(['message' => 'Hello World']));
} catch (\Aws\Sqs\Exception\SqsException $e) {
$message = $e->getMessage();
$status = 'error';
} catch (\Exception $e) {
$message = $e->getMessage();
$status = 'error';
} catch (\Error $e) {
$message = $e->getMessage();
$status = 'error';
} catch (\ErrorException $e) {
} catch (\Throwable $e) {
$message = $e->getMessage();
$status = 'error';
}
Expand Down Expand Up @@ -418,11 +415,11 @@ public function saveServicesConfig(AdminRequest $request)
$twilio = $request->input('twilio', config('services.twilio'));
$sentry = $request->input('sentry', config('sentry.dsn'));

Setting::configure('system.services.aws', array_merge(config('services.aws', []), $aws));
Setting::configure('system.services.ipinfo', array_merge(config('services.ipinfo', []), $ipinfo));
Setting::configure('system.services.google_maps', array_merge(config('services.google_maps', []), $googleMaps));
Setting::configure('system.services.twilio', array_merge(config('services.twilio', []), $twilio));
Setting::configure('system.services.sentry', array_merge(config('sentry', []), $sentry));
Setting::configureSystem('services.aws', array_merge(config('services.aws', []), $aws));
Setting::configureSystem('services.ipinfo', array_merge(config('services.ipinfo', []), $ipinfo));
Setting::configureSystem('services.google_maps', array_merge(config('services.google_maps', []), $googleMaps));
Setting::configureSystem('services.twilio', array_merge(config('services.twilio', []), $twilio));
Setting::configureSystem('services.sentry', array_merge(config('sentry', []), $sentry));

return response()->json(['status' => 'OK']);
}
Expand Down Expand Up @@ -470,8 +467,8 @@ public function saveNotificationChannelsConfig(AdminRequest $request)
// Get credentials config array from file contents
$firebase = static::_setupFcmConfigUsingFileId($firebase);

Setting::configure('system.broadcasting.apn', array_merge(config('broadcasting.connections.apn', []), $apn));
Setting::configure('system.firebase.app', array_merge(config('firebase.projects.app', []), $firebase));
Setting::configureSystem('broadcasting.apn', array_merge(config('broadcasting.connections.apn', []), $apn));
Setting::configureSystem('firebase.app', array_merge(config('firebase.projects.app', []), $firebase));

return response()->json(['status' => 'OK']);
}
Expand Down
69 changes: 49 additions & 20 deletions src/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
use Fleetbase\Traits\HasApiModelBehavior;
use Fleetbase\Traits\Searchable;
use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;

class Setting extends EloquentModel
{
Expand Down Expand Up @@ -127,29 +130,30 @@ protected static function boot()
*/
public static function system($key, $defaultValue = null)
{
if (!$key || !is_string($key)) {
return $defaultValue;
}

$cacheKey = 'system_settings.' . $key;

// Attempt to get the value from the cache
return cache()->rememberForever($cacheKey, function () use ($key, $defaultValue) {
$prefix = Str::startsWith($key, 'system.') ? '' : 'system.';
$segments = explode('.', $key);
$settingKey = 'system.' . $key;
$queryKey = 'system.' . $segments[0] . '.' . $segments[1];
$settingKey = $prefix . $key;

if (count($segments) >= 3) {
$subKey = implode('.', array_slice($segments, 2));
$setting = static::where('key', $queryKey)->first();
$queryKey = $prefix . $segments[0] . '.' . $segments[1];
$subKey = implode('.', array_slice($segments, 2));
$setting = static::where('key', $queryKey)->first();
if ($setting) {
return data_get($setting->value, $subKey, $defaultValue);
}
}

$setting = static::where('key', $settingKey)->first();

return $setting ? $setting->value : $defaultValue;
} else {
$setting = static::where('key', $settingKey)->first();
$setting = static::where('key', $settingKey)->first();

return $setting ? $setting->value : $defaultValue;
}
return $setting ? $setting->value : $defaultValue;
});
}

Expand All @@ -165,27 +169,36 @@ public static function system($key, $defaultValue = null)
*/
public static function configureSystem($key, $value = null): ?Setting
{
return static::configure('system.' . $key, $value);
return static::configure('system.' . $key, $value, function () {
static::clearSystemCache();
});
}

/**
* Updates a system setting by key and value, or creates it if it does not exist.
* This function is typically used to dynamically configure system settings during runtime.
*
* @param string $key the key of the setting to update or create
* @param mixed $value The value to set for the setting. If null, it updates the setting with null.
* @param string $key the key of the setting to update or create
* @param mixed $value The value to set for the setting. If null, it updates the setting with null.
* @param \Closure|null $callback a callback to do something with the setting record
*
* @return Setting|null the updated or created setting instance, or null if the operation fails
*/
public static function configure($key, $value = null): ?Setting
public static function configure($key, $value = null, ?\Closure $callback = null): ?Setting
{
return static::updateOrCreate(
$setting = static::updateOrCreate(
['key' => $key],
[
'key' => $key,
'value' => $value,
]
);

if (is_callable($callback)) {
$callback($setting);
}

return $setting;
}

/**
Expand Down Expand Up @@ -294,7 +307,7 @@ public static function getBranding()
$defaultTheme = static::where('key', 'branding.default_theme')->value('value');

// get icon file record
if (\Illuminate\Support\Str::isUuid($iconUuid)) {
if (Str::isUuid($iconUuid)) {
$icon = File::where('uuid', $iconUuid)->first();

if ($icon && $icon instanceof File) {
Expand All @@ -303,7 +316,7 @@ public static function getBranding()
}

// getlogo file record
if (\Illuminate\Support\Str::isUuid($logoUuid)) {
if (Str::isUuid($logoUuid)) {
$logo = File::where('uuid', $logoUuid)->first();

if ($logo && $logo instanceof File) {
Expand Down Expand Up @@ -332,7 +345,7 @@ public static function getBrandingLogoUrl()
{
$logoUuid = static::where('key', 'branding.logo_uuid')->value('value');

if (\Illuminate\Support\Str::isUuid($logoUuid)) {
if (Str::isUuid($logoUuid)) {
$logo = File::where('uuid', $logoUuid)->first();

if ($logo && $logo instanceof File) {
Expand All @@ -356,7 +369,7 @@ public static function getBrandingIconUrl()
{
$iconUuid = static::where('key', 'branding.icon_uuid')->value('value');

if (\Illuminate\Support\Str::isUuid($iconUuid)) {
if (Str::isUuid($iconUuid)) {
$icon = File::where('uuid', $iconUuid)->first();

if ($icon && $icon instanceof File) {
Expand Down Expand Up @@ -434,4 +447,20 @@ public static function doesntHaveConnection(): bool
{
return !static::hasConnection();
}

/**
* Clears all cache entries with keys that start with "system_setting".
*
* This method connects to the Redis cache store and retrieves all keys
* that begin with the prefix "system_setting". It then iterates through each key
* and removes the corresponding cache entry using Laravel's Cache facade.
*
* Note: This function assumes that the application is using Redis as the
* cache driver. If a different cache driver is being used, this method
* may not function as expected.
*/
public static function clearSystemCache(): void
{
Utils::clearCacheByPattern('system_setting*');
}
}
Loading

0 comments on commit 7f5f189

Please sign in to comment.