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