| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Uid; |
| 13 |
|
| 14 |
use Symfony\Component\Uid\Exception\InvalidArgumentException; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
class UuidV7 extends Uuid implements TimeBasedUidInterface |
| 26 |
{ |
| 27 |
protected const TYPE = 7; |
| 28 |
|
| 29 |
private static string $time = ''; |
| 30 |
private static int $subMs = 0; |
| 31 |
private static array $rand = []; |
| 32 |
private static string $seed; |
| 33 |
private static array $seedParts; |
| 34 |
private static int $seedIndex = 0; |
| 35 |
|
| 36 |
public function __construct(?string $uuid = null) |
| 37 |
{ |
| 38 |
if (null === $uuid) { |
| 39 |
$this->uid = static::generate(); |
| 40 |
} else { |
| 41 |
parent::__construct($uuid, true); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
public function getDateTime(): \DateTimeImmutable |
| 46 |
{ |
| 47 |
$time = substr($this->uid, 0, 8).substr($this->uid, 9, 4); |
| 48 |
$time = \PHP_INT_SIZE >= 8 ? (string) hexdec($time) : BinaryUtil::toBase(hex2bin($time), BinaryUtil::BASE10); |
| 49 |
|
| 50 |
if (4 > \strlen($time)) { |
| 51 |
$time = '000'.$time; |
| 52 |
} |
| 53 |
$time .= substr(1000 + (hexdec(substr($this->uid, 14, 4)) >> 2 & 0x3FF), -3); |
| 54 |
|
| 55 |
return \DateTimeImmutable::createFromFormat('U.u', substr_replace($time, '.', -6, 0)); |
| 56 |
} |
| 57 |
|
| 58 |
public static function generate(?\DateTimeInterface $time = null): string |
| 59 |
{ |
| 60 |
if (null === $mtime = $time) { |
| 61 |
$time = microtime(false); |
| 62 |
$subMs = (int) substr($time, 5, 3); |
| 63 |
$time = substr($time, 11).substr($time, 2, 3); |
| 64 |
} elseif (0 > $time = $time->format('Uu')) { |
| 65 |
throw new InvalidArgumentException('The timestamp must be positive.'); |
| 66 |
} else { |
| 67 |
$subMs = (int) substr($time, -3); |
| 68 |
$time = substr($time, 0, -3); |
| 69 |
} |
| 70 |
|
| 71 |
if ($time > self::$time || (null !== $mtime && $time !== self::$time)) { |
| 72 |
randomize: |
| 73 |
self::$rand = unpack(\PHP_INT_SIZE >= 8 ? 'L*' : 'S*', isset(self::$seed) ? random_bytes(8) : self::$seed = random_bytes(16)); |
| 74 |
self::$time = $time; |
| 75 |
} else { |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
if (!self::$seedIndex) { |
| 88 |
$s = unpack(\PHP_INT_SIZE >= 8 ? 'L*' : 'l*', self::$seed = hash('sha512', self::$seed, true)); |
| 89 |
$s[] = ($s[1] >> 8 & 0xFF0000) | ($s[2] >> 16 & 0xFF00) | ($s[3] >> 24 & 0xFF); |
| 90 |
$s[] = ($s[4] >> 8 & 0xFF0000) | ($s[5] >> 16 & 0xFF00) | ($s[6] >> 24 & 0xFF); |
| 91 |
$s[] = ($s[7] >> 8 & 0xFF0000) | ($s[8] >> 16 & 0xFF00) | ($s[9] >> 24 & 0xFF); |
| 92 |
$s[] = ($s[10] >> 8 & 0xFF0000) | ($s[11] >> 16 & 0xFF00) | ($s[12] >> 24 & 0xFF); |
| 93 |
$s[] = ($s[13] >> 8 & 0xFF0000) | ($s[14] >> 16 & 0xFF00) | ($s[15] >> 24 & 0xFF); |
| 94 |
self::$seedParts = $s; |
| 95 |
self::$seedIndex = 21; |
| 96 |
} |
| 97 |
|
| 98 |
if (\PHP_INT_SIZE >= 8) { |
| 99 |
self::$rand[2] = 0xFFFFFFFF & $carry = self::$rand[2] + 1 + (self::$seedParts[self::$seedIndex--] & 0xFFFFFF); |
| 100 |
self::$rand[1] = 0xFFFFFFFF & $carry = self::$rand[1] + ($carry >> 32); |
| 101 |
$carry >>= 32; |
| 102 |
} else { |
| 103 |
self::$rand[4] = 0xFFFF & $carry = self::$rand[4] + 1 + (self::$seedParts[self::$seedIndex--] & 0xFFFFFF); |
| 104 |
self::$rand[3] = 0xFFFF & $carry = self::$rand[3] + ($carry >> 16); |
| 105 |
self::$rand[2] = 0xFFFF & $carry = self::$rand[2] + ($carry >> 16); |
| 106 |
self::$rand[1] = 0xFFFF & $carry = self::$rand[1] + ($carry >> 16); |
| 107 |
$carry >>= 16; |
| 108 |
} |
| 109 |
|
| 110 |
if ($carry && $subMs <= self::$subMs) { |
| 111 |
usleep(1); |
| 112 |
|
| 113 |
if (1024 <= ++$subMs) { |
| 114 |
if (\PHP_INT_SIZE >= 8 || 10 > \strlen($time = self::$time)) { |
| 115 |
$time = (string) (1 + $time); |
| 116 |
} elseif ('999999999' === $mtime = substr($time, -9)) { |
| 117 |
$time = (1 + substr($time, 0, -9)).'000000000'; |
| 118 |
} else { |
| 119 |
$time = substr_replace($time, str_pad(++$mtime, 9, '0', \STR_PAD_LEFT), -9); |
| 120 |
} |
| 121 |
|
| 122 |
goto randomize; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
$time = self::$time; |
| 127 |
} |
| 128 |
self::$subMs = $subMs; |
| 129 |
|
| 130 |
if (\PHP_INT_SIZE >= 8) { |
| 131 |
return substr_replace(\sprintf('%012x-%04x-%04x-%04x%08x', |
| 132 |
$time, |
| 133 |
0x7000 | ($subMs << 2) | (self::$rand[1] >> 30), |
| 134 |
0x8000 | (self::$rand[1] >> 16 & 0x3FFF), |
| 135 |
self::$rand[1] & 0xFFFF, |
| 136 |
self::$rand[2], |
| 137 |
), '-', 8, 0); |
| 138 |
} |
| 139 |
|
| 140 |
return substr_replace(\sprintf('%012s-%04x-%04x-%04x%04x%04x', |
| 141 |
bin2hex(BinaryUtil::fromBase($time, BinaryUtil::BASE10)), |
| 142 |
0x7000 | ($subMs << 2) | (self::$rand[1] >> 14), |
| 143 |
0x8000 | (self::$rand[1] & 0x3FFF), |
| 144 |
self::$rand[2], |
| 145 |
self::$rand[3], |
| 146 |
self::$rand[4], |
| 147 |
), '-', 8, 0); |
| 148 |
} |
| 149 |
} |