From 11dfc862a5aa06ecb0a5e6fe174c1358529db9d6 Mon Sep 17 00:00:00 2001 From: Randall Wilk Date: Wed, 16 Oct 2024 10:07:07 -0500 Subject: [PATCH 1/3] Add new HashKeyGenerator --- config/settings.php | 11 +++++ .../KeyGenerators/HashKeyGenerator.php | 40 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/Support/KeyGenerators/HashKeyGenerator.php diff --git a/config/settings.php b/config/settings.php index 59fda16..686df5a 100644 --- a/config/settings.php +++ b/config/settings.php @@ -145,6 +145,7 @@ | Supported: | - \Rawilk\Settings\Support\KeyGenerators\ReadableKeyGenerator | - \Rawilk\Settings\Support\KeyGenerators\Md5KeyGenerator (default) + | - \Rawilk\Settings\Support\KeyGenerators\HashKeyGenerator | */ 'key_generator' => \Rawilk\Settings\Support\KeyGenerators\Md5KeyGenerator::class, @@ -194,4 +195,14 @@ \Carbon\CarbonImmutable::class, \Illuminate\Support\Carbon::class, ], + + /* + |-------------------------------------------------------------------------- + | Hash Algorithm + |-------------------------------------------------------------------------- + | + | The hashing algorithm to use for the HashKeyGenerator. + | + */ + 'hash_algorithm' => 'xxh128', ]; diff --git a/src/Support/KeyGenerators/HashKeyGenerator.php b/src/Support/KeyGenerators/HashKeyGenerator.php new file mode 100644 index 0000000..815ba89 --- /dev/null +++ b/src/Support/KeyGenerators/HashKeyGenerator.php @@ -0,0 +1,40 @@ +serializer->serialize($context), + ); + } + + public function removeContextFromKey(string $key): string + { + throw new RuntimeException('HashKeyGenerator does not support removing the context from the key.'); + } + + public function setContextSerializer(ContextSerializer $serializer): static + { + $this->serializer = $serializer; + + return $this; + } + + public function contextPrefix(): string + { + throw new RuntimeException('HashKeyGenerator does not support a context prefix.'); + } +} From 974dd6d45030d1928a346825a4e19c0a09f6481f Mon Sep 17 00:00:00 2001 From: rawilk Date: Wed, 16 Oct 2024 15:07:33 +0000 Subject: [PATCH 2/3] PHP Linting (Pint) --- src/Contracts/ContextSerializer.php | 2 +- src/Contracts/KeyGenerator.php | 2 +- src/Drivers/DatabaseDriver.php | 3 +-- src/Drivers/EloquentDriver.php | 4 +--- src/Drivers/Factory.php | 8 +++----- src/Events/SettingWasDeleted.php | 3 +-- src/Events/SettingWasStored.php | 3 +-- src/Events/SettingsFlushed.php | 3 +-- src/Settings.php | 5 ++--- src/Support/ContextSerializers/ContextSerializer.php | 2 +- .../ContextSerializers/DotNotationContextSerializer.php | 2 +- src/Support/KeyGenerators/HashKeyGenerator.php | 2 +- src/Support/KeyGenerators/Md5KeyGenerator.php | 2 +- src/Support/KeyGenerators/ReadableKeyGenerator.php | 2 +- tests/Support/Drivers/CustomDriver.php | 4 +--- 15 files changed, 18 insertions(+), 29 deletions(-) diff --git a/src/Contracts/ContextSerializer.php b/src/Contracts/ContextSerializer.php index bb632c8..f0c8e2d 100644 --- a/src/Contracts/ContextSerializer.php +++ b/src/Contracts/ContextSerializer.php @@ -8,5 +8,5 @@ interface ContextSerializer { - public function serialize(Context $context = null): string; + public function serialize(?Context $context = null): string; } diff --git a/src/Contracts/KeyGenerator.php b/src/Contracts/KeyGenerator.php index 8a10994..cbd2742 100644 --- a/src/Contracts/KeyGenerator.php +++ b/src/Contracts/KeyGenerator.php @@ -8,7 +8,7 @@ interface KeyGenerator { - public function generate(string $key, Context $context = null): string; + public function generate(string $key, ?Context $context = null): string; public function removeContextFromKey(string $key): string; diff --git a/src/Drivers/DatabaseDriver.php b/src/Drivers/DatabaseDriver.php index ab8da3d..cebe2a9 100644 --- a/src/Drivers/DatabaseDriver.php +++ b/src/Drivers/DatabaseDriver.php @@ -17,8 +17,7 @@ public function __construct( protected Connection $connection, protected string $table, protected ?string $teamForeignKey = null, - ) { - } + ) {} public function forget($key, $teamId = null): void { diff --git a/src/Drivers/EloquentDriver.php b/src/Drivers/EloquentDriver.php index b0143bf..b6bd9cd 100644 --- a/src/Drivers/EloquentDriver.php +++ b/src/Drivers/EloquentDriver.php @@ -10,9 +10,7 @@ class EloquentDriver implements Driver { - public function __construct(protected Setting $model) - { - } + public function __construct(protected Setting $model) {} public function forget($key, $teamId = null): void { diff --git a/src/Drivers/Factory.php b/src/Drivers/Factory.php index 36e9d4a..4ccd09f 100644 --- a/src/Drivers/Factory.php +++ b/src/Drivers/Factory.php @@ -17,11 +17,9 @@ class Factory protected array $customCreators = []; - public function __construct(protected Application $app) - { - } + public function __construct(protected Application $app) {} - public function driver(string $driver = null): Driver + public function driver(?string $driver = null): Driver { return $this->resolveDriver($driver); } @@ -64,7 +62,7 @@ protected function getDriverConfig(string $driver): ?array return $this->app['config']["settings.drivers.{$driver}"]; } - protected function resolveDriver(string $driver = null): Driver + protected function resolveDriver(?string $driver = null): Driver { $driver = $driver ?: $this->getDefaultDriver(); diff --git a/src/Events/SettingWasDeleted.php b/src/Events/SettingWasDeleted.php index d622771..267f301 100644 --- a/src/Events/SettingWasDeleted.php +++ b/src/Events/SettingWasDeleted.php @@ -19,6 +19,5 @@ public function __construct( public string $cacheKey, public mixed $teamId, public bool|Context|null $context, - ) { - } + ) {} } diff --git a/src/Events/SettingWasStored.php b/src/Events/SettingWasStored.php index 2cdef65..71cbcb7 100644 --- a/src/Events/SettingWasStored.php +++ b/src/Events/SettingWasStored.php @@ -20,6 +20,5 @@ public function __construct( public mixed $value, public mixed $teamId, public bool|Context|null $context, - ) { - } + ) {} } diff --git a/src/Events/SettingsFlushed.php b/src/Events/SettingsFlushed.php index 5283e16..f40d394 100644 --- a/src/Events/SettingsFlushed.php +++ b/src/Events/SettingsFlushed.php @@ -18,6 +18,5 @@ public function __construct( public bool|Collection|string $keys, public mixed $teamId, public bool|Context|null $context, - ) { - } + ) {} } diff --git a/src/Settings.php b/src/Settings.php index 337349b..f45598e 100755 --- a/src/Settings.php +++ b/src/Settings.php @@ -66,8 +66,7 @@ public function __construct( protected Driver $driver, protected KeyGenerator $keyGenerator, protected ValueSerializer $valueSerializer, - ) { - } + ) {} // mainly for testing purposes public function getDriver(): Driver @@ -79,7 +78,7 @@ public function getDriver(): Driver * Pass in `false` for context when calling `all()` to only return results * that do not have context. */ - public function context(Context|bool $context = null): self + public function context(Context|bool|null $context = null): self { $this->context = $context; diff --git a/src/Support/ContextSerializers/ContextSerializer.php b/src/Support/ContextSerializers/ContextSerializer.php index fd8f0cd..601b3c7 100644 --- a/src/Support/ContextSerializers/ContextSerializer.php +++ b/src/Support/ContextSerializers/ContextSerializer.php @@ -9,7 +9,7 @@ class ContextSerializer implements ContextSerializerContract { - public function serialize(Context $context = null): string + public function serialize(?Context $context = null): string { return serialize($context); } diff --git a/src/Support/ContextSerializers/DotNotationContextSerializer.php b/src/Support/ContextSerializers/DotNotationContextSerializer.php index 5f36336..d271d98 100644 --- a/src/Support/ContextSerializers/DotNotationContextSerializer.php +++ b/src/Support/ContextSerializers/DotNotationContextSerializer.php @@ -9,7 +9,7 @@ class DotNotationContextSerializer implements ContextSerializerContract { - public function serialize(Context $context = null): string + public function serialize(?Context $context = null): string { if (is_null($context)) { return ''; diff --git a/src/Support/KeyGenerators/HashKeyGenerator.php b/src/Support/KeyGenerators/HashKeyGenerator.php index 815ba89..9c6a8f1 100644 --- a/src/Support/KeyGenerators/HashKeyGenerator.php +++ b/src/Support/KeyGenerators/HashKeyGenerator.php @@ -13,7 +13,7 @@ class HashKeyGenerator implements KeyGeneratorContract { protected ContextSerializer $serializer; - public function generate(string $key, Context $context = null): string + public function generate(string $key, ?Context $context = null): string { return hash( config('settings.hash_algorithm', 'xxh128'), diff --git a/src/Support/KeyGenerators/Md5KeyGenerator.php b/src/Support/KeyGenerators/Md5KeyGenerator.php index d827090..da417b3 100644 --- a/src/Support/KeyGenerators/Md5KeyGenerator.php +++ b/src/Support/KeyGenerators/Md5KeyGenerator.php @@ -13,7 +13,7 @@ class Md5KeyGenerator implements KeyGeneratorContract { protected ContextSerializer $serializer; - public function generate(string $key, Context $context = null): string + public function generate(string $key, ?Context $context = null): string { return md5($key . $this->serializer->serialize($context)); } diff --git a/src/Support/KeyGenerators/ReadableKeyGenerator.php b/src/Support/KeyGenerators/ReadableKeyGenerator.php index 9c73f73..de4ae74 100644 --- a/src/Support/KeyGenerators/ReadableKeyGenerator.php +++ b/src/Support/KeyGenerators/ReadableKeyGenerator.php @@ -13,7 +13,7 @@ class ReadableKeyGenerator implements KeyGeneratorContract { protected ContextSerializer $serializer; - public function generate(string $key, Context $context = null): string + public function generate(string $key, ?Context $context = null): string { $key = $this->normalizeKey($key); diff --git a/tests/Support/Drivers/CustomDriver.php b/tests/Support/Drivers/CustomDriver.php index 944b99b..07536b2 100644 --- a/tests/Support/Drivers/CustomDriver.php +++ b/tests/Support/Drivers/CustomDriver.php @@ -34,7 +34,5 @@ public function all($teamId = null, $keys = null): array|Arrayable return []; } - public function flush($teamId = null, $keys = null): void - { - } + public function flush($teamId = null, $keys = null): void {} } From 4169e0531c6af3563db231d0baedbe709e5357e9 Mon Sep 17 00:00:00 2001 From: Randall Wilk Date: Wed, 16 Oct 2024 10:14:13 -0500 Subject: [PATCH 3/3] Test HashKeyGenerator --- .../KeyGenerators/HashKeyGeneratorTest.php | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 tests/Unit/KeyGenerators/HashKeyGeneratorTest.php diff --git a/tests/Unit/KeyGenerators/HashKeyGeneratorTest.php b/tests/Unit/KeyGenerators/HashKeyGeneratorTest.php new file mode 100644 index 0000000..cc68055 --- /dev/null +++ b/tests/Unit/KeyGenerators/HashKeyGeneratorTest.php @@ -0,0 +1,43 @@ +keyGenerator = (new HashKeyGenerator) + ->setContextSerializer(new ContextSerializer); + + config([ + 'settings.hash_algorithm' => 'xxh128', + ]); +}); + +it('generates a hash of a key', function () { + // N; is for a serialized null context object + expect($this->keyGenerator->generate('my-key'))->toBe(hash('xxh128', 'my-keyN;')); +}); + +it('generates a hash of a key and context object', function () { + $context = new Context([ + 'id' => 123, + ]); + + expect($this->keyGenerator->generate('my-key', $context)) + ->toBe(hash('xxh128', 'my-key' . serialize($context))); +}); + +it('works with other context serializers', function () { + $this->keyGenerator->setContextSerializer(new DotNotationContextSerializer); + + $context = new Context([ + 'id' => 123, + 'bool-value' => false, + ]); + + expect($this->keyGenerator->generate('my-key', $context)) + ->toBe(hash('xxh128', 'my-keyid:123::bool-value:0')); +});