From 4f79622c7419ccb08b32b0306ac3e91e8b1d71cd Mon Sep 17 00:00:00 2001 From: Mojmir Fendek Date: Thu, 9 Apr 2020 12:13:20 +1200 Subject: [PATCH] PR fixes --- .../01_Templates/10_Unique_Keys.md | 2 +- src/ORM/DataObject.php | 10 +++++----- src/ORM/UniqueKey/UniqueKeyInterface.php | 4 ++-- src/ORM/UniqueKey/UniqueKeyService.php | 20 +++++++++++-------- 4 files changed, 20 insertions(+), 16 deletions(-) diff --git a/docs/en/02_Developer_Guides/01_Templates/10_Unique_Keys.md b/docs/en/02_Developer_Guides/01_Templates/10_Unique_Keys.md index cf2a94cbed1..36ead330b27 100644 --- a/docs/en/02_Developer_Guides/01_Templates/10_Unique_Keys.md +++ b/docs/en/02_Developer_Guides/01_Templates/10_Unique_Keys.md @@ -28,7 +28,7 @@ The unique key generation can be altered in two ways: ### Extension point -`cacheKeyComponent` extension point is located in `DataObject::getCacheKeyComponent`. +`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: diff --git a/src/ORM/DataObject.php b/src/ORM/DataObject.php index 01b8b9f8b77..898bbea027e 100644 --- a/src/ORM/DataObject.php +++ b/src/ORM/DataObject.php @@ -3237,7 +3237,7 @@ public static function get_one($callerClass, $filter = "", $cache = true, $order /** @var DataObject $singleton */ $singleton = singleton($callerClass); - $cacheComponents = [$filter, $orderby, $singleton->getCacheKeyComponent()]; + $cacheComponents = [$filter, $orderby, $singleton->getUniqueKeyComponents()]; $cacheKey = md5(serialize($cacheComponents)); $item = null; @@ -4189,7 +4189,7 @@ public function mergeRelatedObjects($list, $items) /** * Generate a unique key for data object - * the unique key uses the @see DataObject::getCacheKeyComponent() extension point so unique key modifiers + * the unique key uses the @see DataObject::getUniqueKeyComponents() extension point so unique key modifiers * such as versioned or fluent are covered * i.e. same data object in different stages or different locales will produce different unique key * @@ -4207,9 +4207,9 @@ public function getUniqueKey(): string return ''; } - $cacheKeys = $this->getCacheKeyComponent(); + $keyComponents = $this->getUniqueKeyComponents(); - return $service->generateKey($this, $cacheKeys); + return $service->generateKey($this, $keyComponents); } /** @@ -4238,7 +4238,7 @@ protected function mergeRelatedObject($list, $added, $item) } } - private function getCacheKeyComponent(): array + private function getUniqueKeyComponents(): array { return $this->extend('cacheKeyComponent'); } diff --git a/src/ORM/UniqueKey/UniqueKeyInterface.php b/src/ORM/UniqueKey/UniqueKeyInterface.php index c17fcf2120f..9582de1348c 100644 --- a/src/ORM/UniqueKey/UniqueKeyInterface.php +++ b/src/ORM/UniqueKey/UniqueKeyInterface.php @@ -10,8 +10,8 @@ interface UniqueKeyInterface * Generate a unique key for data object * * @param DataObject $object - * @param array $extraKeys + * @param array $keyComponents * @return string */ - public function generateKey(DataObject $object, array $extraKeys = []): string; + public function generateKey(DataObject $object, array $keyComponents = []): string; } diff --git a/src/ORM/UniqueKey/UniqueKeyService.php b/src/ORM/UniqueKey/UniqueKeyService.php index b32d859477f..b3d7d57a2d6 100644 --- a/src/ORM/UniqueKey/UniqueKeyService.php +++ b/src/ORM/UniqueKey/UniqueKeyService.php @@ -2,6 +2,7 @@ namespace SilverStripe\ORM\UniqueKey; +use Exception; use SilverStripe\Core\ClassInfo; use SilverStripe\Core\Injector\Injectable; use SilverStripe\ORM\DataObject; @@ -21,19 +22,22 @@ class UniqueKeyService implements UniqueKeyInterface { use Injectable; - public function generateKey(DataObject $object, array $extraKeys = []): string + /** + * @param DataObject $object + * @param array $keyComponents + * @return string + * @throws Exception + */ + public function generateKey(DataObject $object, array $keyComponents = []): string { - if (!$object->isInDB()) { - return ''; - } - + $id = $object->isInDB() ? (string) $object->ID : bin2hex(random_bytes(16)); $class = ClassInfo::shortName($object); - $extraKeys = json_encode($extraKeys); + $keyComponents = json_encode($keyComponents); - $hash = md5(sprintf('%s-%s-%d', $extraKeys, $object->ClassName, $object->ID)); + $hash = md5(sprintf('%s-%s-%s', $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-%d-%s', $class, $object->ID, $hash); + return sprintf('ss-%s-%s-%s', $class, $id, $hash); } }