2017-12-15 03:05:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Repositories\Eloquent;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Setting;
|
|
|
|
use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface;
|
|
|
|
|
|
|
|
class SettingsRepository extends EloquentRepository implements SettingsRepositoryInterface
|
|
|
|
{
|
2022-10-14 16:59:20 +00:00
|
|
|
private static array $cache = [];
|
2017-12-15 03:05:26 +00:00
|
|
|
|
2022-10-14 16:59:20 +00:00
|
|
|
private static array $databaseMiss = [];
|
2017-12-15 03:05:26 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-05 04:49:50 +00:00
|
|
|
* Return the model backing this repository.
|
2017-12-15 03:05:26 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function model(): string
|
2017-12-15 03:05:26 +00:00
|
|
|
{
|
|
|
|
return Setting::class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a new persistent setting in the database.
|
|
|
|
*
|
2018-01-05 04:49:50 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
2017-12-15 03:05:26 +00:00
|
|
|
*/
|
2018-02-03 18:28:39 +00:00
|
|
|
public function set(string $key, string $value = null)
|
2017-12-15 03:05:26 +00:00
|
|
|
{
|
|
|
|
// Clear item from the cache.
|
|
|
|
$this->clearCache($key);
|
2018-02-03 18:28:39 +00:00
|
|
|
$this->withoutFreshModel()->updateOrCreate(['key' => $key], ['value' => $value ?? '']);
|
2018-01-05 04:49:50 +00:00
|
|
|
|
|
|
|
self::$cache[$key] = $value;
|
2017-12-15 03:05:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve a persistent setting from the database.
|
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function get(string $key, mixed $default = null): mixed
|
2017-12-15 03:05:26 +00:00
|
|
|
{
|
|
|
|
// If item has already been requested return it from the cache. If
|
2018-01-05 04:49:50 +00:00
|
|
|
// 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);
|
2017-12-15 03:05:26 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 16:56:03 +00:00
|
|
|
/** @var Setting $instance */
|
2017-12-15 03:05:26 +00:00
|
|
|
$instance = $this->getBuilder()->where('key', $key)->first();
|
|
|
|
if (is_null($instance)) {
|
2018-01-05 04:49:50 +00:00
|
|
|
self::$databaseMiss[$key] = true;
|
2017-12-15 03:05:26 +00:00
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
return value($default);
|
2017-12-15 03:05:26 +00:00
|
|
|
}
|
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
return self::$cache[$key] = $instance->value;
|
2017-12-15 03:05:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-01-05 04:49:50 +00:00
|
|
|
private function clearCache(string $key)
|
2017-12-15 03:05:26 +00:00
|
|
|
{
|
2018-01-05 04:49:50 +00:00
|
|
|
unset(self::$cache[$key], self::$databaseMiss[$key]);
|
2017-12-15 03:05:26 +00:00
|
|
|
}
|
|
|
|
}
|