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 554fea4 commit 7067f64
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 20 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
9 changes: 3 additions & 6 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 @@ -33,11 +31,10 @@ public function generateKey(DataObject $object, array $keyComponents = []): stri
$id = $object->isInDB() ? (string) $object->ID : bin2hex(random_bytes(16));
$class = ClassInfo::shortName($object);
$keyComponents = json_encode($keyComponents);

$hash = md5(sprintf('%s-%s-%s', $keyComponents, $object->ClassName, $id));
$hash = md5($keyComponents . $object->ClassName . $id);

// 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-8d3310e232f75a01f5a0c9344655263d'],
[1, River::class, true, 'River-1-ff2ea6e873a9e28538dd4af278f35e08'],
[2, River::class, false, 'River-2-c562c31e5c2caaabb124b46e274097c1'],
[2, River::class, true, 'River-2-410c1eb12697a26742bbe4b059625ab2'],
[1, Mountain::class, false, 'Mountain-1-93164c0f65fa28778fb75163c1e3e2f0'],
[1, Mountain::class, true, 'Mountain-1-2daf208e0b89252e5d239fbc0464a517'],
[2, Mountain::class, false, 'Mountain-2-62366f2b970a64de6f2a8e8654f179d5'],
[2, Mountain::class, true, 'Mountain-2-a724046b14d331a1486841eaa591d109'],
];
}
}

0 comments on commit 7067f64

Please sign in to comment.