mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-28 08:04:45 +02:00
Remove settings repository
This commit is contained in:
parent
df37de4d2d
commit
ba95045583
@ -1,18 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Contracts\Repository;
|
||||
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
interface SessionRepositoryInterface extends RepositoryInterface
|
||||
{
|
||||
/**
|
||||
* Return all the active sessions for a user.
|
||||
*/
|
||||
public function getUserSessions(int $user): Collection;
|
||||
|
||||
/**
|
||||
* Delete a session for a given user.
|
||||
*/
|
||||
public function deleteUserSession(int $user, string $session): ?int;
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Contracts\Repository;
|
||||
|
||||
interface SettingsRepositoryInterface extends RepositoryInterface
|
||||
{
|
||||
/**
|
||||
* Store a new persistent setting in the database.
|
||||
*
|
||||
* @throws \App\Exceptions\Model\DataValidationException
|
||||
* @throws \App\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);
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Admin\Settings;
|
||||
|
||||
use App\Models\Setting;
|
||||
use Illuminate\View\View;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Prologue\Alerts\AlertsMessageBag;
|
||||
@ -9,7 +10,6 @@ use Illuminate\Contracts\Console\Kernel;
|
||||
use Illuminate\View\Factory as ViewFactory;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
||||
use App\Contracts\Repository\SettingsRepositoryInterface;
|
||||
use App\Http\Requests\Admin\Settings\AdvancedSettingsFormRequest;
|
||||
|
||||
class AdvancedController extends Controller
|
||||
@ -21,7 +21,6 @@ class AdvancedController extends Controller
|
||||
private AlertsMessageBag $alert,
|
||||
private ConfigRepository $config,
|
||||
private Kernel $kernel,
|
||||
private SettingsRepositoryInterface $settings,
|
||||
private ViewFactory $view
|
||||
) {
|
||||
}
|
||||
@ -51,7 +50,7 @@ class AdvancedController extends Controller
|
||||
public function update(AdvancedSettingsFormRequest $request): RedirectResponse
|
||||
{
|
||||
foreach ($request->normalize() as $key => $value) {
|
||||
$this->settings->set('settings::' . $key, $value);
|
||||
Setting::set('settings::' . $key, $value);
|
||||
}
|
||||
|
||||
$this->kernel->call('queue:restart');
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Admin\Settings;
|
||||
|
||||
use App\Models\Setting;
|
||||
use Illuminate\View\View;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Prologue\Alerts\AlertsMessageBag;
|
||||
@ -10,7 +11,6 @@ use Illuminate\View\Factory as ViewFactory;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Traits\Helpers\AvailableLanguages;
|
||||
use App\Services\Helpers\SoftwareVersionService;
|
||||
use App\Contracts\Repository\SettingsRepositoryInterface;
|
||||
use App\Http\Requests\Admin\Settings\BaseSettingsFormRequest;
|
||||
|
||||
class IndexController extends Controller
|
||||
@ -23,7 +23,6 @@ class IndexController extends Controller
|
||||
public function __construct(
|
||||
private AlertsMessageBag $alert,
|
||||
private Kernel $kernel,
|
||||
private SettingsRepositoryInterface $settings,
|
||||
private SoftwareVersionService $versionService,
|
||||
private ViewFactory $view
|
||||
) {
|
||||
@ -49,7 +48,7 @@ class IndexController extends Controller
|
||||
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');
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Admin\Settings;
|
||||
|
||||
use App\Models\Setting;
|
||||
use Illuminate\View\View;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
@ -14,7 +15,6 @@ use App\Http\Controllers\Controller;
|
||||
use Illuminate\Contracts\Encryption\Encrypter;
|
||||
use App\Providers\SettingsServiceProvider;
|
||||
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
||||
use App\Contracts\Repository\SettingsRepositoryInterface;
|
||||
use App\Http\Requests\Admin\Settings\MailSettingsFormRequest;
|
||||
|
||||
class MailController extends Controller
|
||||
@ -26,7 +26,6 @@ class MailController extends Controller
|
||||
private ConfigRepository $config,
|
||||
private Encrypter $encrypter,
|
||||
private Kernel $kernel,
|
||||
private SettingsRepositoryInterface $settings,
|
||||
private ViewFactory $view
|
||||
) {
|
||||
}
|
||||
@ -65,7 +64,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');
|
||||
|
@ -24,4 +24,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): void
|
||||
{
|
||||
// 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): void
|
||||
{
|
||||
unset(self::$cache[$key], self::$databaseMiss[$key]);
|
||||
}
|
||||
}
|
||||
|
@ -4,17 +4,13 @@ namespace App\Providers;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use App\Repositories\Eloquent\NodeRepository;
|
||||
use App\Repositories\Eloquent\SessionRepository;
|
||||
use App\Repositories\Eloquent\SubuserRepository;
|
||||
use App\Repositories\Eloquent\DatabaseRepository;
|
||||
use App\Repositories\Eloquent\SettingsRepository;
|
||||
use App\Repositories\Eloquent\EggVariableRepository;
|
||||
use App\Contracts\Repository\NodeRepositoryInterface;
|
||||
use App\Repositories\Eloquent\DatabaseHostRepository;
|
||||
use App\Contracts\Repository\SessionRepositoryInterface;
|
||||
use App\Contracts\Repository\SubuserRepositoryInterface;
|
||||
use App\Contracts\Repository\DatabaseRepositoryInterface;
|
||||
use App\Contracts\Repository\SettingsRepositoryInterface;
|
||||
use App\Contracts\Repository\EggVariableRepositoryInterface;
|
||||
use App\Contracts\Repository\DatabaseHostRepositoryInterface;
|
||||
|
||||
@ -30,8 +26,6 @@ class RepositoryServiceProvider extends ServiceProvider
|
||||
$this->app->bind(DatabaseHostRepositoryInterface::class, DatabaseHostRepository::class);
|
||||
$this->app->bind(EggVariableRepositoryInterface::class, EggVariableRepository::class);
|
||||
$this->app->bind(NodeRepositoryInterface::class, NodeRepository::class);
|
||||
$this->app->bind(SessionRepositoryInterface::class, SessionRepository::class);
|
||||
$this->app->bind(SettingsRepositoryInterface::class, SettingsRepository::class);
|
||||
$this->app->bind(SubuserRepositoryInterface::class, SubuserRepository::class);
|
||||
}
|
||||
}
|
||||
|
@ -2,13 +2,13 @@
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Models\Setting;
|
||||
use Psr\Log\LoggerInterface as Log;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Contracts\Encryption\Encrypter;
|
||||
use Illuminate\Contracts\Encryption\DecryptException;
|
||||
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
||||
use App\Contracts\Repository\SettingsRepositoryInterface;
|
||||
|
||||
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): void
|
||||
public function boot(ConfigRepository $config, Encrypter $encrypter, Log $log): void
|
||||
{
|
||||
// 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) {
|
||||
|
@ -1,34 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Eloquent;
|
||||
|
||||
use App\Models\Session;
|
||||
use Illuminate\Support\Collection;
|
||||
use App\Contracts\Repository\SessionRepositoryInterface;
|
||||
|
||||
class SessionRepository extends EloquentRepository implements SessionRepositoryInterface
|
||||
{
|
||||
/**
|
||||
* Return the model backing this repository.
|
||||
*/
|
||||
public function model(): string
|
||||
{
|
||||
return Session::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all the active sessions for a user.
|
||||
*/
|
||||
public function getUserSessions(int $user): Collection
|
||||
{
|
||||
return $this->getBuilder()->where('user_id', $user)->get($this->getColumns());
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a session for a given user.
|
||||
*/
|
||||
public function deleteUserSession(int $user, string $session): ?int
|
||||
{
|
||||
return $this->getBuilder()->where('user_id', $user)->where('id', $session)->delete();
|
||||
}
|
||||
}
|
@ -1,75 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Repositories\Eloquent;
|
||||
|
||||
use App\Models\Setting;
|
||||
use App\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 \App\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]);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user