diff --git a/config/settings.php b/config/settings.php index 7d898ee..6553bf3 100644 --- a/config/settings.php +++ b/config/settings.php @@ -1,5 +1,7 @@ app['config']['settings.driver'] = $driver; + } + protected function createDatabaseDriver(array $config): DatabaseDriver { return new DatabaseDriver( @@ -54,11 +59,6 @@ protected function getDefaultDriver(): string return $this->app['config']['settings.driver']; } - public function setDefaultDriver(string $driver): void - { - $this->app['config']['settings.driver'] = $driver; - } - protected function getDriverConfig(string $driver): ?array { return $this->app['config']["settings.drivers.{$driver}"]; diff --git a/src/Models/Setting.php b/src/Models/Setting.php index eec7de1..781946b 100644 --- a/src/Models/Setting.php +++ b/src/Models/Setting.php @@ -10,8 +10,12 @@ class Setting extends Model implements SettingContract { + public $timestamps = false; + protected ?string $teamForeignKey = null; + protected $guarded = ['id']; + public function __construct(array $attributes = []) { parent::__construct($attributes); @@ -20,10 +24,6 @@ public function __construct(array $attributes = []) $this->teamForeignKey = config('settings.team_foreign_key'); } - protected $guarded = ['id']; - - public $timestamps = false; - public static function getValue(string $key, $default = null, $teamId = null) { $value = static::query() diff --git a/src/Settings.php b/src/Settings.php index 40b4619..c670c82 100755 --- a/src/Settings.php +++ b/src/Settings.php @@ -213,62 +213,6 @@ public function isTrue(string $key, $default = true): bool return $value === true || $value === '1' || $value === 1; } - protected function normalizeKey(string $key): string - { - if (Str::startsWith(haystack: $key, needles: 'file_')) { - return str_replace(search: 'file_', replace: 'file.', subject: $key); - } - - return $key; - } - - protected function getCacheKey(string $key): string - { - $cacheKey = $this->cacheKeyPrefix . $key; - - if ($this->teams) { - $teamId = $this->teamId ?? 'null'; - - $cacheKey .= "::team:{$teamId}"; - } - - return $cacheKey; - } - - protected function getKeyForStorage(string $key): string - { - return $this->keyGenerator->generate(key: $key, context: $this->context); - } - - protected function serializeValue($value): string - { - return $this->valueSerializer->serialize($value); - } - - protected function unserializeValue($serialized) - { - if (! is_string($serialized)) { - return $serialized; - } - - // Attempt to unserialize the value, but return the original value if that fails. - return rescue(fn () => $this->valueSerializer->unserialize($serialized), fn () => $serialized); - } - - protected function shouldSetNewValue(string $key, $newValue): bool - { - if (! $this->cacheIsEnabled()) { - return true; - } - - // To prevent decryption errors, we will check if we have a setting set for the current context and key. - if (! $this->doNotResetContext()->has($key)) { - return true; - } - - return $newValue !== $this->doNotResetContext()->get($key); - } - public function disableCache(): self { $this->cacheEnabled = false; @@ -297,15 +241,6 @@ public function setCache(Cache $cache): self return $this; } - protected function cacheIsEnabled(): bool - { - if ($this->temporarilyDisableCache) { - return false; - } - - return $this->cacheEnabled && $this->cache !== null; - } - public function disableEncryption(): self { $this->encryptionEnabled = false; @@ -327,11 +262,6 @@ public function setEncrypter(Encrypter $encrypter): self return $this; } - protected function encryptionIsEnabled(): bool - { - return $this->encryptionEnabled && $this->encrypter !== null; - } - public function enableTeams(): self { $this->teams = true; @@ -358,6 +288,76 @@ public function useCacheKeyPrefix(string $prefix): self return $this; } + protected function normalizeKey(string $key): string + { + if (Str::startsWith(haystack: $key, needles: 'file_')) { + return str_replace(search: 'file_', replace: 'file.', subject: $key); + } + + return $key; + } + + protected function getCacheKey(string $key): string + { + $cacheKey = $this->cacheKeyPrefix . $key; + + if ($this->teams) { + $teamId = $this->teamId ?? 'null'; + + $cacheKey .= "::team:{$teamId}"; + } + + return $cacheKey; + } + + protected function getKeyForStorage(string $key): string + { + return $this->keyGenerator->generate(key: $key, context: $this->context); + } + + protected function serializeValue($value): string + { + return $this->valueSerializer->serialize($value); + } + + protected function unserializeValue($serialized) + { + if (! is_string($serialized)) { + return $serialized; + } + + // Attempt to unserialize the value, but return the original value if that fails. + return rescue(fn () => $this->valueSerializer->unserialize($serialized), fn () => $serialized); + } + + protected function shouldSetNewValue(string $key, $newValue): bool + { + if (! $this->cacheIsEnabled()) { + return true; + } + + // To prevent decryption errors, we will check if we have a setting set for the current context and key. + if (! $this->doNotResetContext()->has($key)) { + return true; + } + + return $newValue !== $this->doNotResetContext()->get($key); + } + + protected function cacheIsEnabled(): bool + { + if ($this->temporarilyDisableCache) { + return false; + } + + return $this->cacheEnabled && $this->cache !== null; + } + + protected function encryptionIsEnabled(): bool + { + return $this->encryptionEnabled && $this->encrypter !== null; + } + protected function decryptValue($value) { if (! $this->encryptionIsEnabled()) { diff --git a/src/SettingsServiceProvider.php b/src/SettingsServiceProvider.php index 0a2bea9..4e30aab 100644 --- a/src/SettingsServiceProvider.php +++ b/src/SettingsServiceProvider.php @@ -32,6 +32,14 @@ public function packageRegistered(): void $this->registerSettings(); } + public function provides(): array + { + return [ + Settings::class, + 'SettingsFactory', + ]; + } + protected function bootModelBindings(): void { $config = $this->app['config']['settings.drivers.eloquent']; @@ -70,12 +78,4 @@ protected function registerSettings(): void return $settings; }); } - - public function provides(): array - { - return [ - Settings::class, - 'SettingsFactory', - ]; - } } diff --git a/tests/Feature/SettingsTest.php b/tests/Feature/SettingsTest.php index 8b672bd..9fe2eed 100644 --- a/tests/Feature/SettingsTest.php +++ b/tests/Feature/SettingsTest.php @@ -340,7 +340,7 @@ function isSerialized(mixed $data): bool case 'd': $end = ''; - return (bool) preg_match("/^{$token}:[0-9.E+-]+;$end/", $data); + return (bool) preg_match("/^{$token}:[0-9.E+-]+;{$end}/", $data); } return false; diff --git a/tests/Pest.php b/tests/Pest.php index 2b450b0..e50d933 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -1,5 +1,7 @@ up(); } } + + protected function getPackageProviders($app): array + { + return [ + SettingsServiceProvider::class, + ]; + } }