uuid generator

This commit is contained in:
Yuri Kuznetsov
2023-02-15 17:03:14 +02:00
parent 4a9bd1b54c
commit d9c0f7d055
2 changed files with 26 additions and 1 deletions
@@ -29,6 +29,7 @@
namespace Espo\Core\Utils\Id;
use Espo\Core\Utils\Metadata;
use Espo\Core\Utils\Util;
/**
@@ -36,8 +37,17 @@ use Espo\Core\Utils\Util;
*/
class DefaultRecordIdGenerator implements RecordIdGenerator
{
private bool $isUuid;
public function __construct(Metadata $metadata)
{
$this->isUuid = $metadata->get(['app', 'recordId', 'dbType']) === 'uuid';
}
public function generate(): string
{
return Util::generateId();
return $this->isUuid ?
Util::generateUuid4() :
Util::generateId();
}
}
+15
View File
@@ -630,6 +630,21 @@ class Util
return true;
}
/**
* Generate a UUID v4.
*/
public static function generateUuid4(): string
{
try {
$hex = bin2hex(random_bytes(16));
}
catch (\Exception $e) {
throw new \RuntimeException("Could not generate UUID.");
}
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split($hex, 4));
}
/**
* Generate a 17-character hex ID.
*/