-
Notifications
You must be signed in to change notification settings - Fork 824
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
Unique key for DataObject #9400
Merged
emteknetnz
merged 8 commits into
silverstripe:4
from
silverstripe-terraformers:feature/unique-key
May 3, 2020
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
449b39e
Unique key for DataObject
mfendeksilverstripe d560007
PR fixes
mfendeksilverstripe 2f183d5
Dependencies change reverted
mfendeksilverstripe 2899060
Doc updated
mfendeksilverstripe 554fea4
PR fixes
mfendeksilverstripe 7067f64
PR fixes
mfendeksilverstripe 63f4a0e
PR fixes
mfendeksilverstripe edf7d43
PR fixes
mfendeksilverstripe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
48 changes: 48 additions & 0 deletions
48
docs/en/02_Developer_Guides/01_Templates/10_Unique_Keys.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
--- | ||
title: Generating Unique Keys | ||
summary: Outputting unique keys in templates. | ||
icon: code | ||
--- | ||
|
||
# Unique Keys | ||
|
||
There are several cases where you may want to generate a unique key. For example: | ||
|
||
* populate `ID` attribute in your HTML output | ||
* key for partial cache | ||
|
||
This can be done simply by including following code in your template: | ||
|
||
```ss | ||
$DataObject.UniqueKey | ||
``` | ||
|
||
`getUniqueKey` method is available on `DataObject` so you can use it on many object types like pages and blocks. | ||
|
||
## Customisation | ||
|
||
The unique key generation can be altered in two ways: | ||
|
||
* you can provide extra data to be used when generating a key via an extension | ||
* you can inject over the key generation service and write your own custom code | ||
|
||
### Extension point | ||
|
||
`cacheKeyComponent` extension point is located in `DataObject::getUniqueKeyComponents`. | ||
Use standard extension flow to define the `cacheKeyComponent` method on your extension which is expected to return a `string`. | ||
This value will be used when unique key is generated. Common cases are: | ||
|
||
* versions - object in different version stages needs to have different unique keys | ||
* locales - object in different locales needs to have different unique keys | ||
|
||
### Custom service | ||
|
||
`UniqueKeyService` is used by default but you can use injector to override it with your custom service. For example: | ||
|
||
```yaml | ||
SilverStripe\Core\Injector\Injector: | ||
SilverStripe\ORM\UniqueKey\UniqueKeyService: | ||
class: App\Service\MyCustomService | ||
``` | ||
|
||
Your custom service has to implement `UniqueKeyInterface`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace SilverStripe\ORM\UniqueKey; | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add a quick comment about what's the purpose of the interface. |
||
{ | ||
/** | ||
* Generate a unique key for data object | ||
* | ||
* @param DataObject $object | ||
* @param array $keyComponents | ||
* @return string | ||
*/ | ||
public function generateKey(DataObject $object, array $keyComponents = []): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace SilverStripe\ORM\UniqueKey; | ||
|
||
use Exception; | ||
use SilverStripe\Core\ClassInfo; | ||
use SilverStripe\Core\Injector\Injectable; | ||
use SilverStripe\ORM\DataObject; | ||
|
||
/** | ||
* Class UniqueKeyService | ||
* | ||
* Generate a unique key for data object | ||
* | ||
* 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) | ||
*/ | ||
class UniqueKeyService implements UniqueKeyInterface | ||
{ | ||
use Injectable; | ||
|
||
/** | ||
* @param DataObject $object | ||
* @param array $keyComponents key components are expected to be strings (or at least scalar values) | ||
* @return string | ||
* @throws Exception | ||
*/ | ||
public function generateKey(DataObject $object, array $keyComponents = []): string | ||
{ | ||
$id = $object->isInDB() ? (string) $object->ID : bin2hex(random_bytes(16)); | ||
$class = ClassInfo::shortName($object); | ||
sminnee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$keyComponents = json_encode($keyComponents); | ||
emteknetnz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$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('%s-%s-%s', $class, $id, $hash); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Tests\ORM\UniqueKey; | ||
|
||
use SilverStripe\Core\Extension; | ||
use SilverStripe\Dev\TestOnly; | ||
|
||
class ExtraKeysExtension extends Extension implements TestOnly | ||
{ | ||
public function cacheKeyComponent(): string | ||
{ | ||
return 'extra-key'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Tests\ORM\UniqueKey; | ||
|
||
use SilverStripe\Dev\TestOnly; | ||
use SilverStripe\ORM\DataObject; | ||
|
||
class Mountain extends DataObject implements TestOnly | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private static $table_name = 'UniqueKeyTest_Mountain'; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private static $db = [ | ||
'Title' => 'Varchar', | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Tests\ORM\UniqueKey; | ||
|
||
use SilverStripe\Dev\TestOnly; | ||
use SilverStripe\ORM\DataObject; | ||
|
||
class River extends DataObject implements TestOnly | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private static $table_name = 'UniqueKeyTest_River'; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private static $db = [ | ||
'Title' => 'Varchar', | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace SilverStripe\Tests\ORM\UniqueKey; | ||
|
||
use SilverStripe\Core\Injector\Injector; | ||
use SilverStripe\Dev\SapphireTest; | ||
use SilverStripe\ORM\DataObject; | ||
|
||
class ServiceTest extends SapphireTest | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected static $extra_dataobjects = [ | ||
River::class, | ||
Mountain::class, | ||
]; | ||
|
||
/** | ||
* @param int $id | ||
* @param string $class | ||
* @param bool $extraKeys | ||
* @param string $expected | ||
* @dataProvider uniqueKeysProvider | ||
*/ | ||
public function testUniqueKey(int $id, string $class, bool $extraKeys, string $expected): void | ||
{ | ||
if ($extraKeys) { | ||
$class::add_extension(ExtraKeysExtension::class); | ||
} | ||
|
||
/** @var DataObject $object */ | ||
$object = Injector::inst()->create($class); | ||
$object->ID = $id; | ||
|
||
$this->assertEquals($expected, $object->getUniqueKey()); | ||
|
||
if ($extraKeys) { | ||
$class::remove_extension(ExtraKeysExtension::class); | ||
} | ||
} | ||
|
||
public function uniqueKeysProvider(): array | ||
{ | ||
return [ | ||
[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'], | ||
]; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd note that these two use-cases have different constraints. Populating an ID requires more consideration of human factors (such as maybe not dumping in the FQCN) that you don't need to worry about when generating a key for a partial cache.
It's possible we can get a works-for-all-cases option, but that's not yet clear.