Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Repositories/Eloquent/SettingsRepository.php
7460 views
1
<?php
2
3
namespace Pterodactyl\Repositories\Eloquent;
4
5
use Pterodactyl\Models\Setting;
6
use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface;
7
8
class SettingsRepository extends EloquentRepository implements SettingsRepositoryInterface
9
{
10
private static array $cache = [];
11
12
private static array $databaseMiss = [];
13
14
/**
15
* Return the model backing this repository.
16
*/
17
public function model(): string
18
{
19
return Setting::class;
20
}
21
22
/**
23
* Store a new persistent setting in the database.
24
*
25
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
26
*/
27
public function set(string $key, ?string $value = null)
28
{
29
// Clear item from the cache.
30
$this->clearCache($key);
31
$this->withoutFreshModel()->updateOrCreate(['key' => $key], ['value' => $value ?? '']);
32
33
self::$cache[$key] = $value;
34
}
35
36
/**
37
* Retrieve a persistent setting from the database.
38
*/
39
public function get(string $key, mixed $default = null): mixed
40
{
41
// If item has already been requested return it from the cache. If
42
// we already know it is missing, immediately return the default value.
43
if (array_key_exists($key, self::$cache)) {
44
return self::$cache[$key];
45
} elseif (array_key_exists($key, self::$databaseMiss)) {
46
return value($default);
47
}
48
49
$instance = $this->getBuilder()->where('key', $key)->first();
50
if (is_null($instance)) {
51
self::$databaseMiss[$key] = true;
52
53
return value($default);
54
}
55
56
return self::$cache[$key] = $instance->value;
57
}
58
59
/**
60
* Remove a key from the database cache.
61
*/
62
public function forget(string $key)
63
{
64
$this->clearCache($key);
65
$this->deleteWhere(['key' => $key]);
66
}
67
68
/**
69
* Remove a key from the cache.
70
*/
71
private function clearCache(string $key)
72
{
73
unset(self::$cache[$key], self::$databaseMiss[$key]);
74
}
75
}
76
77