Skip to content

Commit 4288daf

Browse files
authored
Merge pull request #3 from netsells/feature/enable-hashing-override
feature/enable-hashing-override
2 parents a0f930c + 8c16546 commit 4288daf

22 files changed

+105
-22
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,17 @@ Publish the package config file:
5959

6060
Optionally set a `HASH_MODEL_IDS_SALT` in `.env`.
6161

62-
6362
## Translations
6463

6564
Publish the package translations file:
6665

6766
`php artisan vendor:publish --tag=hash-model-ids-lang`
6867

6968

69+
## Development
70+
71+
Sometimes, during development, it can be awkward dealing with hashed ids. Set `HASH_MODEL_IDS_ENABLED=false` in your environment file to enable use of a (configurable) prefixed version of a model's actual id rather than the default hashed version.
72+
7073
## Testing
7174

7275
`./vendor/bin/phpunit`

config/hash-model-ids.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22

33
return [
4+
'enabled' => (bool) env('HASH_MODEL_IDS_ENABLED', true),
45
'salt' => env('HASH_MODEL_IDS_SALT', hash('sha256', env('APP_KEY'))),
56
'min_hash_length' => 10,
6-
'alphabet' => 'abcdefghijklmnopqrstuvwxyz0123456789'
7+
'alphabet' => 'abcdefghijklmnopqrstuvwxyz0123456789',
8+
'prefix' => 'id_',
79
];

src/ModelIdPrefixer.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Netsells\HashModelIds;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class ModelIdPrefixer implements ModelIdHasherInterface
8+
{
9+
public const DEFAULT_PREFIX = 'id_';
10+
11+
public function __construct(private readonly string $prefix = self::DEFAULT_PREFIX)
12+
{
13+
//
14+
}
15+
16+
public function encode(Model $model, $id): string
17+
{
18+
return "{$this->prefix}{$id}";
19+
}
20+
21+
public function decode(Model $model, $hash): string
22+
{
23+
return substr($hash, strlen($this->prefix));
24+
}
25+
}

src/ServiceProvider.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Netsells\HashModelIds;
44

5+
use Illuminate\Foundation\Application;
56
use Illuminate\Support\ServiceProvider as BaseServiceProvider;
67

78
class ServiceProvider extends BaseServiceProvider
@@ -19,14 +20,21 @@ public function register()
1920
);
2021

2122
$this->app->singleton(ModelIdHasherInterface::class, function ($app) {
22-
return new ModelIdHasher([
23-
'salt' => config('hash-model-ids.salt'),
24-
'min_hash_length' => config('hash-model-ids.min_hash_length'),
25-
'alphabet' => config('hash-model-ids.alphabet'),
26-
]);
23+
return ! $this->shouldHashIds($app)
24+
? new ModelIdPrefixer(config('hash-model-ids.prefix', ModelIdPrefixer::DEFAULT_PREFIX))
25+
: new ModelIdHasher([
26+
'salt' => config('hash-model-ids.salt'),
27+
'min_hash_length' => config('hash-model-ids.min_hash_length'),
28+
'alphabet' => config('hash-model-ids.alphabet'),
29+
]);
2730
});
2831
}
2932

33+
private function shouldHashIds(Application $app): bool
34+
{
35+
return $app->isProduction() || config('hash-model-ids.enabled', true);
36+
}
37+
3038
/**
3139
* Bootstrap any package services.
3240
*

tests/Integration/AbstractIntegrationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ protected function getPackageProviders($app): array
1414

1515
protected function defineDatabaseMigrations(): void
1616
{
17-
$this->loadMigrationsFrom(__DIR__ . '/fixtures/migrations');
17+
$this->loadMigrationsFrom(__DIR__ . '/Fixtures/migrations');
1818
}
1919

2020
protected function defineEnvironment($app): void
2121
{
22-
$app['config']->set('hash_model_ids.salt', 'test-salt');
22+
$app['config']->set('hash-model-ids.salt', 'test-salt');
2323
}
2424
}

tests/Integration/ExistsWithHashedIdRuleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Illuminate\Database\Eloquent\Builder;
66
use InvalidArgumentException;
77
use Netsells\HashModelIds\ExistsWithHashedIdRule;
8-
use Netsells\HashModelIds\Tests\Integration\fixtures\Models;
8+
use Netsells\HashModelIds\Tests\Integration\Fixtures\Models;
99

1010
class ExistsWithHashedIdRuleTest extends AbstractIntegrationTest
1111
{

tests/Integration/fixtures/Models/Bar.php renamed to tests/Integration/Fixtures/Models/Bar.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Netsells\HashModelIds\Tests\Integration\fixtures\Models;
3+
namespace Netsells\HashModelIds\Tests\Integration\Fixtures\Models;
44

55
use Illuminate\Database\Eloquent\Model;
66

tests/Integration/fixtures/Models/Foo.php renamed to tests/Integration/Fixtures/Models/Foo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Netsells\HashModelIds\Tests\Integration\fixtures\Models;
3+
namespace Netsells\HashModelIds\Tests\Integration\Fixtures\Models;
44

55
use Illuminate\Database\Eloquent\Model;
66
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

tests/Integration/fixtures/Models/FooRelation.php renamed to tests/Integration/Fixtures/Models/FooRelation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Netsells\HashModelIds\Tests\Integration\fixtures\Models;
3+
namespace Netsells\HashModelIds\Tests\Integration\Fixtures\Models;
44

55
use Illuminate\Database\Eloquent\Model;
66
use Netsells\HashModelIds\HashesModelIdsTrait;

tests/Integration/fixtures/Models/Trashed.php renamed to tests/Integration/Fixtures/Models/Trashed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Netsells\HashModelIds\Tests\Integration\fixtures\Models;
3+
namespace Netsells\HashModelIds\Tests\Integration\Fixtures\Models;
44

55
use Illuminate\Database\Eloquent\Model;
66
use Illuminate\Database\Eloquent\SoftDeletes;

0 commit comments

Comments
 (0)