cms/app/Services/SettingService.php

82 lines
1.9 KiB
PHP
Raw Normal View History

<?php
namespace App\Services;
use App\Models\Setting;
use Illuminate\Support\Facades\Cache;
/**
* Service to handle site-wide settings with caching.
*/
class SettingService
{
/**
* Get all settings grouped by their group.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getAllSettings()
{
return Setting::all();
}
/**
* Get a setting value by key.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function get(string $key, $default = null)
{
return Cache::rememberForever("setting.{$key}", function () use ($key, $default) {
return Setting::get($key, $default);
});
}
/**
* Update or create multiple settings.
*
* @param array $settings Map of key => value
* @param string $group
* @return void
*/
public function updateSettings(array $settings, string $group = 'general'): void
{
foreach ($settings as $key => $value) {
Setting::set($key, $value, $group);
Cache::forget("setting.{$key}");
}
// Clear dependent caches if languages change
if (isset($settings['supported_languages'])) {
Cache::forget('supported_languages');
}
}
/**
* Get supported languages from settings.
*
* @return array
*/
public function getSupportedLanguages(): array
{
return Cache::rememberForever('supported_languages', function () {
return Setting::get('supported_languages', [
['name' => 'English', 'abbreviation' => 'en']
]);
});
}
/**
* Get supported language abbreviations.
*
* @return array
*/
public function getSupportedLocales(): array
{
$languages = $this->getSupportedLanguages();
return array_column($languages, 'abbreviation');
}
}