Skip to content

Commit

Permalink
PR fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mfendeksilverstripe committed May 1, 2020
1 parent e6614e3 commit 78626c1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
6 changes: 6 additions & 0 deletions _config/unique-id.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
Name: unique-id
---
SilverStripe\Core\Injector\Injector:
SilverStripe\ORM\UniqueKey\UniqueKeyInterface:
class: SilverStripe\ORM\UniqueKey\UniqueKeyService
2 changes: 1 addition & 1 deletion docs/en/02_Developer_Guides/01_Templates/10_Unique_Keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ This value will be used when unique key is generated. Common cases are:
```yaml
SilverStripe\Core\Injector\Injector:
SilverStripe\ORM\UniqueKey\UniqueKeyService:
class: App\Service\MuCustomService
class: App\Service\MyCustomService
```
Your custom service has to implement `UniqueKeyInterface`.
6 changes: 1 addition & 5 deletions src/ORM/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -4203,11 +4203,7 @@ public function mergeRelatedObjects($list, $items)
public function getUniqueKey(): string
{
/** @var UniqueKeyInterface $service */
$service = UniqueKeyService::singleton();
if (!$service instanceof UniqueKeyInterface) {
return bin2hex(random_bytes(16));
}

$service = Injector::inst()->get(UniqueKeyInterface::class);
$keyComponents = $this->getUniqueKeyComponents();

return $service->generateKey($this, $keyComponents);
Expand Down
6 changes: 6 additions & 0 deletions src/ORM/UniqueKey/UniqueKeyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

use SilverStripe\ORM\DataObject;

/**
* Interface UniqueKeyInterface
*
* Useful when you want to implement your own custom service and use it instead of the default one (@see UniqueKeyService)
* your custom service needs to implement this interface
*/
interface UniqueKeyInterface
{
/**
Expand Down
6 changes: 2 additions & 4 deletions src/ORM/UniqueKey/UniqueKeyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
* recommended use:
* - when you need unique key for caching purposes
* - when you need unique id on the front end (for example JavaScript needs to target specific element)
*
* @package SilverStripe\ORM\UniqueKey
*/
class UniqueKeyService implements UniqueKeyInterface
{
use Injectable;

/**
* @param DataObject $object
* @param array $keyComponents
* @param array $keyComponents key components are expected to be strings (or at least scalar values)
* @return string
* @throws Exception
*/
Expand All @@ -38,6 +36,6 @@ public function generateKey(DataObject $object, array $keyComponents = []): stri

// note: class name and id are added just for readability as the hash already contains all parts
// needed to create a unique key
return sprintf('ss-%s-%s-%s', $class, $id, $hash);
return sprintf('%s-%s-%s', $class, $id, $hash);
}
}
16 changes: 8 additions & 8 deletions tests/php/ORM/UniqueKey/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public function testUniqueKey(int $id, string $class, bool $extraKeys, string $e
public function uniqueKeysProvider(): array
{
return [
[1, River::class, false, 'ss-River-1-e64cc160ce00cc28cb0f8a3096cf3ed5'],
[1, River::class, true, 'ss-River-1-1484f5b9c7d403b7fd2ba944efead0a6'],
[2, River::class, false, 'ss-River-2-93608031dbdb53167fce1c700e71adfd'],
[2, River::class, true, 'ss-River-2-cfb8c8328ca792cfe83859b0ef28d3f4'],
[1, Mountain::class, false, 'ss-Mountain-1-8d1e32d7d9a5f55b9c5e87facc6a0acc'],
[1, Mountain::class, true, 'ss-Mountain-1-7d286845ff54b023fb43450ecd55aeb8'],
[2, Mountain::class, false, 'ss-Mountain-2-813dc6d6a905b6d3720130b9fb46e01a'],
[2, Mountain::class, true, 'ss-Mountain-2-d1133d717d00c944732ac25e6043ce5e'],
[1, River::class, false, 'River-1-e64cc160ce00cc28cb0f8a3096cf3ed5'],
[1, River::class, true, 'River-1-1484f5b9c7d403b7fd2ba944efead0a6'],
[2, River::class, false, 'River-2-93608031dbdb53167fce1c700e71adfd'],
[2, River::class, true, 'River-2-cfb8c8328ca792cfe83859b0ef28d3f4'],
[1, Mountain::class, false, 'Mountain-1-8d1e32d7d9a5f55b9c5e87facc6a0acc'],
[1, Mountain::class, true, 'Mountain-1-7d286845ff54b023fb43450ecd55aeb8'],
[2, Mountain::class, false, 'Mountain-2-813dc6d6a905b6d3720130b9fb46e01a'],
[2, Mountain::class, true, 'Mountain-2-d1133d717d00c944732ac25e6043ce5e'],
];
}
}

0 comments on commit 78626c1

Please sign in to comment.