Replace settings repository

This commit is contained in:
Lance Pioch 2022-10-24 01:03:53 -04:00
parent e49ba65709
commit e5d5d7de4d
8 changed files with 76 additions and 118 deletions

View file

@ -1,24 +0,0 @@
<?php
namespace Pterodactyl\Contracts\Repository;
interface SettingsRepositoryInterface extends RepositoryInterface
{
/**
* Store a new persistent setting in the database.
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function set(string $key, string $value = null);
/**
* Retrieve a persistent setting from the database.
*/
public function get(string $key, mixed $default): mixed;
/**
* Remove a key from the database cache.
*/
public function forget(string $key);
}

View file

@ -7,10 +7,12 @@ use Illuminate\Http\RedirectResponse;
use Prologue\Alerts\AlertsMessageBag; use Prologue\Alerts\AlertsMessageBag;
use Illuminate\Contracts\Console\Kernel; use Illuminate\Contracts\Console\Kernel;
use Illuminate\View\Factory as ViewFactory; use Illuminate\View\Factory as ViewFactory;
use Pterodactyl\Exceptions\Model\DataValidationException;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Illuminate\Contracts\Config\Repository as ConfigRepository; use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface;
use Pterodactyl\Http\Requests\Admin\Settings\AdvancedSettingsFormRequest; use Pterodactyl\Http\Requests\Admin\Settings\AdvancedSettingsFormRequest;
use Pterodactyl\Models\Setting;
class AdvancedController extends Controller class AdvancedController extends Controller
{ {
@ -21,7 +23,6 @@ class AdvancedController extends Controller
private AlertsMessageBag $alert, private AlertsMessageBag $alert,
private ConfigRepository $config, private ConfigRepository $config,
private Kernel $kernel, private Kernel $kernel,
private SettingsRepositoryInterface $settings,
private ViewFactory $view private ViewFactory $view
) { ) {
} }
@ -45,13 +46,13 @@ class AdvancedController extends Controller
} }
/** /**
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @throws RecordNotFoundException
*/ */
public function update(AdvancedSettingsFormRequest $request): RedirectResponse public function update(AdvancedSettingsFormRequest $request): RedirectResponse
{ {
foreach ($request->normalize() as $key => $value) { foreach ($request->normalize() as $key => $value) {
$this->settings->set('settings::' . $key, $value); Setting::set('settings::' . $key, $value);
} }
$this->kernel->call('queue:restart'); $this->kernel->call('queue:restart');

View file

@ -3,6 +3,7 @@
namespace Pterodactyl\Http\Controllers\Admin\Settings; namespace Pterodactyl\Http\Controllers\Admin\Settings;
use Illuminate\View\View; use Illuminate\View\View;
use Pterodactyl\Models\Setting;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Prologue\Alerts\AlertsMessageBag; use Prologue\Alerts\AlertsMessageBag;
use Illuminate\Contracts\Console\Kernel; use Illuminate\Contracts\Console\Kernel;
@ -10,7 +11,8 @@ use Illuminate\View\Factory as ViewFactory;
use Pterodactyl\Http\Controllers\Controller; use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Traits\Helpers\AvailableLanguages; use Pterodactyl\Traits\Helpers\AvailableLanguages;
use Pterodactyl\Services\Helpers\SoftwareVersionService; 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; use Pterodactyl\Http\Requests\Admin\Settings\BaseSettingsFormRequest;
class IndexController extends Controller class IndexController extends Controller
@ -23,7 +25,6 @@ class IndexController extends Controller
public function __construct( public function __construct(
private AlertsMessageBag $alert, private AlertsMessageBag $alert,
private Kernel $kernel, private Kernel $kernel,
private SettingsRepositoryInterface $settings,
private SoftwareVersionService $versionService, private SoftwareVersionService $versionService,
private ViewFactory $view private ViewFactory $view
) { ) {
@ -43,13 +44,13 @@ class IndexController extends Controller
/** /**
* Handle settings update. * Handle settings update.
* *
* @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException * @throws RecordNotFoundException
*/ */
public function update(BaseSettingsFormRequest $request): RedirectResponse public function update(BaseSettingsFormRequest $request): RedirectResponse
{ {
foreach ($request->normalize() as $key => $value) { foreach ($request->normalize() as $key => $value) {
$this->settings->set('settings::' . $key, $value); Setting::set('settings::' . $key, $value);
} }
$this->kernel->call('queue:restart'); $this->kernel->call('queue:restart');

View file

@ -7,6 +7,7 @@ use Illuminate\View\View;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Illuminate\Contracts\Console\Kernel; use Illuminate\Contracts\Console\Kernel;
use Pterodactyl\Models\Setting;
use Pterodactyl\Notifications\MailTested; use Pterodactyl\Notifications\MailTested;
use Illuminate\View\Factory as ViewFactory; use Illuminate\View\Factory as ViewFactory;
use Illuminate\Support\Facades\Notification; use Illuminate\Support\Facades\Notification;
@ -15,7 +16,6 @@ use Pterodactyl\Http\Controllers\Controller;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Providers\SettingsServiceProvider; use Pterodactyl\Providers\SettingsServiceProvider;
use Illuminate\Contracts\Config\Repository as ConfigRepository; use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface;
use Pterodactyl\Http\Requests\Admin\Settings\MailSettingsFormRequest; use Pterodactyl\Http\Requests\Admin\Settings\MailSettingsFormRequest;
class MailController extends Controller class MailController extends Controller
@ -27,7 +27,6 @@ class MailController extends Controller
private ConfigRepository $config, private ConfigRepository $config,
private Encrypter $encrypter, private Encrypter $encrypter,
private Kernel $kernel, private Kernel $kernel,
private SettingsRepositoryInterface $settings,
private ViewFactory $view private ViewFactory $view
) { ) {
} }
@ -66,7 +65,7 @@ class MailController extends Controller
$value = $this->encrypter->encrypt($value); $value = $this->encrypter->encrypt($value);
} }
$this->settings->set('settings::' . $key, $value); Setting::set('settings::' . $key, $value);
} }
$this->kernel->call('queue:restart'); $this->kernel->call('queue:restart');

View file

@ -17,4 +17,63 @@ class Setting extends Model
'key' => 'required|string|between:1,191', 'key' => 'required|string|between:1,191',
'value' => 'string', '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]);
}
} }

View file

@ -15,7 +15,6 @@ use Pterodactyl\Repositories\Eloquent\SubuserRepository;
use Pterodactyl\Repositories\Eloquent\DatabaseRepository; use Pterodactyl\Repositories\Eloquent\DatabaseRepository;
use Pterodactyl\Repositories\Eloquent\LocationRepository; use Pterodactyl\Repositories\Eloquent\LocationRepository;
use Pterodactyl\Repositories\Eloquent\ScheduleRepository; use Pterodactyl\Repositories\Eloquent\ScheduleRepository;
use Pterodactyl\Repositories\Eloquent\SettingsRepository;
use Pterodactyl\Repositories\Eloquent\AllocationRepository; use Pterodactyl\Repositories\Eloquent\AllocationRepository;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface; use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Repositories\Eloquent\EggVariableRepository; use Pterodactyl\Repositories\Eloquent\EggVariableRepository;
@ -32,7 +31,6 @@ use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface; use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface;
use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface;
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface; use Pterodactyl\Contracts\Repository\EggVariableRepositoryInterface;
use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface; use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface;
@ -59,7 +57,6 @@ class RepositoryServiceProvider extends ServiceProvider
$this->app->bind(ServerRepositoryInterface::class, ServerRepository::class); $this->app->bind(ServerRepositoryInterface::class, ServerRepository::class);
$this->app->bind(ServerVariableRepositoryInterface::class, ServerVariableRepository::class); $this->app->bind(ServerVariableRepositoryInterface::class, ServerVariableRepository::class);
$this->app->bind(SessionRepositoryInterface::class, SessionRepository::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(SubuserRepositoryInterface::class, SubuserRepository::class);
$this->app->bind(TaskRepositoryInterface::class, TaskRepository::class); $this->app->bind(TaskRepositoryInterface::class, TaskRepository::class);
$this->app->bind(UserRepositoryInterface::class, UserRepository::class); $this->app->bind(UserRepositoryInterface::class, UserRepository::class);

View file

@ -8,7 +8,7 @@ use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Encryption\Encrypter; use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Contracts\Encryption\DecryptException; use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Config\Repository as ConfigRepository; use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface; use Pterodactyl\Models\Setting;
class SettingsServiceProvider extends ServiceProvider class SettingsServiceProvider extends ServiceProvider
{ {
@ -57,7 +57,7 @@ class SettingsServiceProvider extends ServiceProvider
/** /**
* Boot the service provider. * 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 // Only set the email driver settings from the database if we
// are configured using SMTP as the driver. // are configured using SMTP as the driver.
@ -66,7 +66,7 @@ class SettingsServiceProvider extends ServiceProvider
} }
try { try {
$values = $settings->all()->mapWithKeys(function ($setting) { $values = Setting::all()->mapWithKeys(function ($setting) {
return [$setting->key => $setting->value]; return [$setting->key => $setting->value];
})->toArray(); })->toArray();
} catch (QueryException $exception) { } catch (QueryException $exception) {

View file

@ -1,75 +0,0 @@
<?php
namespace Pterodactyl\Repositories\Eloquent;
use Pterodactyl\Models\Setting;
use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface;
class SettingsRepository extends EloquentRepository implements SettingsRepositoryInterface
{
private static array $cache = [];
private static array $databaseMiss = [];
/**
* Return the model backing this repository.
*/
public function model(): string
{
return Setting::class;
}
/**
* Store a new persistent setting in the database.
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
public function set(string $key, string $value = null)
{
// Clear item from the cache.
$this->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]);
}
}