Path: blob/1.0-develop/app/Repositories/Eloquent/SettingsRepository.php
7460 views
<?php12namespace Pterodactyl\Repositories\Eloquent;34use Pterodactyl\Models\Setting;5use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface;67class SettingsRepository extends EloquentRepository implements SettingsRepositoryInterface8{9private static array $cache = [];1011private static array $databaseMiss = [];1213/**14* Return the model backing this repository.15*/16public function model(): string17{18return Setting::class;19}2021/**22* Store a new persistent setting in the database.23*24* @throws \Pterodactyl\Exceptions\Model\DataValidationException25*/26public function set(string $key, ?string $value = null)27{28// Clear item from the cache.29$this->clearCache($key);30$this->withoutFreshModel()->updateOrCreate(['key' => $key], ['value' => $value ?? '']);3132self::$cache[$key] = $value;33}3435/**36* Retrieve a persistent setting from the database.37*/38public function get(string $key, mixed $default = null): mixed39{40// If item has already been requested return it from the cache. If41// we already know it is missing, immediately return the default value.42if (array_key_exists($key, self::$cache)) {43return self::$cache[$key];44} elseif (array_key_exists($key, self::$databaseMiss)) {45return value($default);46}4748$instance = $this->getBuilder()->where('key', $key)->first();49if (is_null($instance)) {50self::$databaseMiss[$key] = true;5152return value($default);53}5455return self::$cache[$key] = $instance->value;56}5758/**59* Remove a key from the database cache.60*/61public function forget(string $key)62{63$this->clearCache($key);64$this->deleteWhere(['key' => $key]);65}6667/**68* Remove a key from the cache.69*/70private function clearCache(string $key)71{72unset(self::$cache[$key], self::$databaseMiss[$key]);73}74}757677