diff --git a/app/Contracts/Repository/SettingsRepositoryInterface.php b/app/Contracts/Repository/SettingsRepositoryInterface.php deleted file mode 100644 index c55c09b74..000000000 --- a/app/Contracts/Repository/SettingsRepositoryInterface.php +++ /dev/null @@ -1,24 +0,0 @@ -normalize() as $key => $value) { - $this->settings->set('settings::' . $key, $value); + Setting::set('settings::' . $key, $value); } $this->kernel->call('queue:restart'); diff --git a/app/Http/Controllers/Admin/Settings/IndexController.php b/app/Http/Controllers/Admin/Settings/IndexController.php index eabede932..532f13395 100644 --- a/app/Http/Controllers/Admin/Settings/IndexController.php +++ b/app/Http/Controllers/Admin/Settings/IndexController.php @@ -3,6 +3,7 @@ namespace Pterodactyl\Http\Controllers\Admin\Settings; use Illuminate\View\View; +use Pterodactyl\Models\Setting; use Illuminate\Http\RedirectResponse; use Prologue\Alerts\AlertsMessageBag; use Illuminate\Contracts\Console\Kernel; @@ -10,7 +11,8 @@ use Illuminate\View\Factory as ViewFactory; use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Traits\Helpers\AvailableLanguages; use Pterodactyl\Services\Helpers\SoftwareVersionService; -use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface; +use Pterodactyl\Exceptions\Model\DataValidationException; +use Pterodactyl\Exceptions\Repository\RecordNotFoundException; use Pterodactyl\Http\Requests\Admin\Settings\BaseSettingsFormRequest; class IndexController extends Controller @@ -23,7 +25,6 @@ class IndexController extends Controller public function __construct( private AlertsMessageBag $alert, private Kernel $kernel, - private SettingsRepositoryInterface $settings, private SoftwareVersionService $versionService, private ViewFactory $view ) { @@ -43,13 +44,13 @@ class IndexController extends Controller /** * Handle settings update. * - * @throws \Pterodactyl\Exceptions\Model\DataValidationException - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException + * @throws DataValidationException + * @throws RecordNotFoundException */ public function update(BaseSettingsFormRequest $request): RedirectResponse { foreach ($request->normalize() as $key => $value) { - $this->settings->set('settings::' . $key, $value); + Setting::set('settings::' . $key, $value); } $this->kernel->call('queue:restart'); diff --git a/app/Http/Controllers/Admin/Settings/MailController.php b/app/Http/Controllers/Admin/Settings/MailController.php index 2db87fd58..e6b63cb85 100644 --- a/app/Http/Controllers/Admin/Settings/MailController.php +++ b/app/Http/Controllers/Admin/Settings/MailController.php @@ -7,6 +7,7 @@ use Illuminate\View\View; use Illuminate\Http\Request; use Illuminate\Http\Response; use Illuminate\Contracts\Console\Kernel; +use Pterodactyl\Models\Setting; use Pterodactyl\Notifications\MailTested; use Illuminate\View\Factory as ViewFactory; use Illuminate\Support\Facades\Notification; @@ -15,7 +16,6 @@ use Pterodactyl\Http\Controllers\Controller; use Illuminate\Contracts\Encryption\Encrypter; use Pterodactyl\Providers\SettingsServiceProvider; use Illuminate\Contracts\Config\Repository as ConfigRepository; -use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface; use Pterodactyl\Http\Requests\Admin\Settings\MailSettingsFormRequest; class MailController extends Controller @@ -27,7 +27,6 @@ class MailController extends Controller private ConfigRepository $config, private Encrypter $encrypter, private Kernel $kernel, - private SettingsRepositoryInterface $settings, private ViewFactory $view ) { } @@ -66,7 +65,7 @@ class MailController extends Controller $value = $this->encrypter->encrypt($value); } - $this->settings->set('settings::' . $key, $value); + Setting::set('settings::' . $key, $value); } $this->kernel->call('queue:restart'); diff --git a/app/Models/Setting.php b/app/Models/Setting.php index 52c7f1cff..f79ef3693 100644 --- a/app/Models/Setting.php +++ b/app/Models/Setting.php @@ -17,4 +17,63 @@ class Setting extends Model 'key' => 'required|string|between:1,191', 'value' => 'string', ]; + + private static array $cache = []; + + private static array $databaseMiss = []; + + /** + * Store a new persistent setting in the database. + * + */ + public static function set(string $key, string $value = null) + { + // Clear item from the cache. + self::clearCache($key); + + self::query()->updateOrCreate(['key' => $key], ['value' => $value ?? '']); + + self::$cache[$key] = $value; + } + + /** + * Retrieve a persistent setting from the database. + */ + public static function get(string $key, mixed $default = null): mixed + { + // If item has already been requested return it from the cache. If + // we already know it is missing, immediately return the default value. + if (array_key_exists($key, self::$cache)) { + return self::$cache[$key]; + } elseif (array_key_exists($key, self::$databaseMiss)) { + return value($default); + } + + $instance = self::query()->where('key', $key)->first(); + if (is_null($instance)) { + self::$databaseMiss[$key] = true; + + return value($default); + } + + return self::$cache[$key] = $instance->value; + } + + /** + * Remove a key from the database cache. + */ + public static function forget(string $key) + { + self::clearCache($key); + + return self::query()->where('key', $key)->delete(); + } + + /** + * Remove a key from the cache. + */ + private static function clearCache(string $key) + { + unset(self::$cache[$key], self::$databaseMiss[$key]); + } } diff --git a/app/Providers/RepositoryServiceProvider.php b/app/Providers/RepositoryServiceProvider.php index 8a0434f52..00259c579 100644 --- a/app/Providers/RepositoryServiceProvider.php +++ b/app/Providers/RepositoryServiceProvider.php @@ -15,7 +15,6 @@ use Pterodactyl\Repositories\Eloquent\SubuserRepository; use Pterodactyl\Repositories\Eloquent\DatabaseRepository; use Pterodactyl\Repositories\Eloquent\LocationRepository; use Pterodactyl\Repositories\Eloquent\ScheduleRepository; -use Pterodactyl\Repositories\Eloquent\SettingsRepository; use Pterodactyl\Repositories\Eloquent\AllocationRepository; use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Repositories\Eloquent\EggVariableRepository; @@ -32,7 +31,6 @@ use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface; -use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface; @@ -59,7 +57,6 @@ class RepositoryServiceProvider extends ServiceProvider $this->app->bind(ServerRepositoryInterface::class, ServerRepository::class); $this->app->bind(ServerVariableRepositoryInterface::class, ServerVariableRepository::class); $this->app->bind(SessionRepositoryInterface::class, SessionRepository::class); - $this->app->bind(SettingsRepositoryInterface::class, SettingsRepository::class); $this->app->bind(SubuserRepositoryInterface::class, SubuserRepository::class); $this->app->bind(TaskRepositoryInterface::class, TaskRepository::class); $this->app->bind(UserRepositoryInterface::class, UserRepository::class); diff --git a/app/Providers/SettingsServiceProvider.php b/app/Providers/SettingsServiceProvider.php index 9c8a7445e..971ac4a00 100644 --- a/app/Providers/SettingsServiceProvider.php +++ b/app/Providers/SettingsServiceProvider.php @@ -8,7 +8,7 @@ use Illuminate\Support\ServiceProvider; use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Contracts\Config\Repository as ConfigRepository; -use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface; +use Pterodactyl\Models\Setting; class SettingsServiceProvider extends ServiceProvider { @@ -57,7 +57,7 @@ class SettingsServiceProvider extends ServiceProvider /** * Boot the service provider. */ - public function boot(ConfigRepository $config, Encrypter $encrypter, Log $log, SettingsRepositoryInterface $settings) + public function boot(ConfigRepository $config, Encrypter $encrypter, Log $log) { // Only set the email driver settings from the database if we // are configured using SMTP as the driver. @@ -66,7 +66,7 @@ class SettingsServiceProvider extends ServiceProvider } try { - $values = $settings->all()->mapWithKeys(function ($setting) { + $values = Setting::all()->mapWithKeys(function ($setting) { return [$setting->key => $setting->value]; })->toArray(); } catch (QueryException $exception) { diff --git a/app/Repositories/Eloquent/SettingsRepository.php b/app/Repositories/Eloquent/SettingsRepository.php deleted file mode 100644 index df22fce59..000000000 --- a/app/Repositories/Eloquent/SettingsRepository.php +++ /dev/null @@ -1,75 +0,0 @@ -clearCache($key); - $this->withoutFreshModel()->updateOrCreate(['key' => $key], ['value' => $value ?? '']); - - self::$cache[$key] = $value; - } - - /** - * Retrieve a persistent setting from the database. - */ - public function get(string $key, mixed $default = null): mixed - { - // If item has already been requested return it from the cache. If - // we already know it is missing, immediately return the default value. - if (array_key_exists($key, self::$cache)) { - return self::$cache[$key]; - } elseif (array_key_exists($key, self::$databaseMiss)) { - return value($default); - } - - $instance = $this->getBuilder()->where('key', $key)->first(); - if (is_null($instance)) { - self::$databaseMiss[$key] = true; - - return value($default); - } - - return self::$cache[$key] = $instance->value; - } - - /** - * Remove a key from the database cache. - */ - public function forget(string $key) - { - $this->clearCache($key); - $this->deleteWhere(['key' => $key]); - } - - /** - * Remove a key from the cache. - */ - private function clearCache(string $key) - { - unset(self::$cache[$key], self::$databaseMiss[$key]); - } -}