Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: Hash key generator #68

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions config/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -194,4 +195,14 @@
\Carbon\CarbonImmutable::class,
\Illuminate\Support\Carbon::class,
],

/*
|--------------------------------------------------------------------------
| Hash Algorithm
|--------------------------------------------------------------------------
|
| The hashing algorithm to use for the HashKeyGenerator.
|
*/
'hash_algorithm' => 'xxh128',
];
2 changes: 1 addition & 1 deletion src/Contracts/ContextSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@

interface ContextSerializer
{
public function serialize(Context $context = null): string;
public function serialize(?Context $context = null): string;
}
2 changes: 1 addition & 1 deletion src/Contracts/KeyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 1 addition & 2 deletions src/Drivers/DatabaseDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ public function __construct(
protected Connection $connection,
protected string $table,
protected ?string $teamForeignKey = null,
) {
}
) {}

public function forget($key, $teamId = null): void
{
Expand Down
4 changes: 1 addition & 3 deletions src/Drivers/EloquentDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
8 changes: 3 additions & 5 deletions src/Drivers/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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();

Expand Down
3 changes: 1 addition & 2 deletions src/Events/SettingWasDeleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@ public function __construct(
public string $cacheKey,
public mixed $teamId,
public bool|Context|null $context,
) {
}
) {}
}
3 changes: 1 addition & 2 deletions src/Events/SettingWasStored.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,5 @@ public function __construct(
public mixed $value,
public mixed $teamId,
public bool|Context|null $context,
) {
}
) {}
}
3 changes: 1 addition & 2 deletions src/Events/SettingsFlushed.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ public function __construct(
public bool|Collection|string $keys,
public mixed $teamId,
public bool|Context|null $context,
) {
}
) {}
}
5 changes: 2 additions & 3 deletions src/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ public function __construct(
protected Driver $driver,
protected KeyGenerator $keyGenerator,
protected ValueSerializer $valueSerializer,
) {
}
) {}

// mainly for testing purposes
public function getDriver(): Driver
Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/Support/ContextSerializers/ContextSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
Expand Down
40 changes: 40 additions & 0 deletions src/Support/KeyGenerators/HashKeyGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Rawilk\Settings\Support\KeyGenerators;

use Rawilk\Settings\Contracts\ContextSerializer;
use Rawilk\Settings\Contracts\KeyGenerator as KeyGeneratorContract;
use Rawilk\Settings\Support\Context;
use RuntimeException;

class HashKeyGenerator implements KeyGeneratorContract
{
protected ContextSerializer $serializer;

public function generate(string $key, ?Context $context = null): string
{
return hash(
config('settings.hash_algorithm', 'xxh128'),
$key . $this->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.');
}
}
2 changes: 1 addition & 1 deletion src/Support/KeyGenerators/Md5KeyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Support/KeyGenerators/ReadableKeyGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions tests/Support/Drivers/CustomDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
43 changes: 43 additions & 0 deletions tests/Unit/KeyGenerators/HashKeyGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

use Rawilk\Settings\Support\Context;
use Rawilk\Settings\Support\ContextSerializers\ContextSerializer;
use Rawilk\Settings\Support\ContextSerializers\DotNotationContextSerializer;
use Rawilk\Settings\Support\KeyGenerators\HashKeyGenerator;

beforeEach(function () {
$this->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'));
});
Loading