uuid generator
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user