From 497801e10105c59573fa13f2277de8b9b9368f57 Mon Sep 17 00:00:00 2001 From: Karem Sobhy Date: Fri, 5 Jul 2024 15:23:33 +0300 Subject: [PATCH 1/5] implement HasV7UUids --- .../Database/Eloquent/Concerns/HasV7Uuids.php | 20 +++++++++++++++++++ src/Illuminate/Support/Str.php | 12 +++++++++++ 2 files changed, 32 insertions(+) create mode 100644 src/Illuminate/Database/Eloquent/Concerns/HasV7Uuids.php diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasV7Uuids.php b/src/Illuminate/Database/Eloquent/Concerns/HasV7Uuids.php new file mode 100644 index 000000000000..8b70c4a07429 --- /dev/null +++ b/src/Illuminate/Database/Eloquent/Concerns/HasV7Uuids.php @@ -0,0 +1,20 @@ + Date: Fri, 5 Jul 2024 18:47:57 +0300 Subject: [PATCH 2/5] Implement all uuid4 methods to uuid7 for unified experience and add ability to define time for the uuid7 --- src/Illuminate/Support/Str.php | 110 +++++++++++++++++++++++++++++---- 1 file changed, 98 insertions(+), 12 deletions(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index a29df926468c..a936aa1de7e1 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -51,6 +51,13 @@ class Str */ protected static $uuidFactory; + /** + * The callback that should be used to generate UUID7s. + * + * @var callable|null + */ + protected static $uuid7Factory; + /** * The callback that should be used to generate ULIDs. * @@ -1736,18 +1743,6 @@ public static function uuid() : Uuid::uuid4(); } - /** - * Generate a UUID (version 7). - * - * @return \Ramsey\Uuid\UuidInterface - */ - public static function uuid7() - { - return static::$uuidFactory - ? call_user_func(static::$uuidFactory) - : Uuid::uuid7(); - } - /** * Generate a time-ordered UUID. * @@ -1851,6 +1846,97 @@ public static function createUuidsNormally() static::$uuidFactory = null; } + /** + * Generate a UUID (version 7). + * + * @param \DateTimeInterface|null $time + * @return \Ramsey\Uuid\UuidInterface + */ + public static function uuid7($time = null) + { + return static::$uuid7Factory + ? call_user_func(static::$uuid7Factory) + : Uuid::uuid7($time); + } + + /** + * Set the callable that will be used to generate UUID7s. + * + * @param callable|null $factory + * @return void + */ + public static function createUuid7sUsing(?callable $factory = null) + { + static::$uuid7Factory = $factory; + } + + /** + * Set the sequence that will be used to generate UUID7s. + * + * @param array $sequence + * @param callable|null $whenMissing + * @return void + */ + public static function createUuid7sUsingSequence(array $sequence, $whenMissing = null) + { + $next = 0; + + $whenMissing ??= function () use (&$next) { + $factoryCache = static::$uuid7Factory; + + static::$uuid7Factory = null; + + $uuid7 = static::uuid7(); + + static::$uuid7Factory = $factoryCache; + + $next++; + + return $uuid7; + }; + + static::createUuid7sUsing(function () use (&$next, $sequence, $whenMissing) { + if (array_key_exists($next, $sequence)) { + return $sequence[$next++]; + } + + return $whenMissing(); + }); + } + + /** + * Always return the same UUID7 when generating new UUID7s. + * + * @param \Closure|null $callback + * @return \Ramsey\Uuid\UuidInterface + */ + public static function freezeUuid7s(?Closure $callback = null) + { + $uuid7 = Str::uuid7(); + + Str::createUuid7sUsing(fn () => $uuid7); + + if ($callback !== null) { + try { + $callback($uuid7); + } finally { + Str::createUuid7sNormally(); + } + } + + return $uuid7; + } + + /** + * Indicate that UUID7s should be created normally and not using a custom factory. + * + * @return void + */ + public static function createUuid7sNormally() + { + static::$uuid7Factory = null; + } + /** * Generate a ULID. * From e9a4b15be259379c2f615e834c1b5e585a9faad1 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 18 Jul 2024 13:36:44 -0500 Subject: [PATCH 3/5] formatting --- src/Illuminate/Support/Str.php | 104 ++++--------------------------- tests/Support/SupportStrTest.php | 3 +- 2 files changed, 15 insertions(+), 92 deletions(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index a936aa1de7e1..5681c6642394 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -1743,6 +1743,19 @@ public static function uuid() : Uuid::uuid4(); } + /** + * Generate a UUID (version 7). + * + * @param \DateTimeInterface|null $time + * @return \Ramsey\Uuid\UuidInterface + */ + public static function uuid7($time = null) + { + return static::$uuidFactory + ? call_user_func(static::$uuidFactory) + : Uuid::uuid7($time); + } + /** * Generate a time-ordered UUID. * @@ -1846,97 +1859,6 @@ public static function createUuidsNormally() static::$uuidFactory = null; } - /** - * Generate a UUID (version 7). - * - * @param \DateTimeInterface|null $time - * @return \Ramsey\Uuid\UuidInterface - */ - public static function uuid7($time = null) - { - return static::$uuid7Factory - ? call_user_func(static::$uuid7Factory) - : Uuid::uuid7($time); - } - - /** - * Set the callable that will be used to generate UUID7s. - * - * @param callable|null $factory - * @return void - */ - public static function createUuid7sUsing(?callable $factory = null) - { - static::$uuid7Factory = $factory; - } - - /** - * Set the sequence that will be used to generate UUID7s. - * - * @param array $sequence - * @param callable|null $whenMissing - * @return void - */ - public static function createUuid7sUsingSequence(array $sequence, $whenMissing = null) - { - $next = 0; - - $whenMissing ??= function () use (&$next) { - $factoryCache = static::$uuid7Factory; - - static::$uuid7Factory = null; - - $uuid7 = static::uuid7(); - - static::$uuid7Factory = $factoryCache; - - $next++; - - return $uuid7; - }; - - static::createUuid7sUsing(function () use (&$next, $sequence, $whenMissing) { - if (array_key_exists($next, $sequence)) { - return $sequence[$next++]; - } - - return $whenMissing(); - }); - } - - /** - * Always return the same UUID7 when generating new UUID7s. - * - * @param \Closure|null $callback - * @return \Ramsey\Uuid\UuidInterface - */ - public static function freezeUuid7s(?Closure $callback = null) - { - $uuid7 = Str::uuid7(); - - Str::createUuid7sUsing(fn () => $uuid7); - - if ($callback !== null) { - try { - $callback($uuid7); - } finally { - Str::createUuid7sNormally(); - } - } - - return $uuid7; - } - - /** - * Indicate that UUID7s should be created normally and not using a custom factory. - * - * @return void - */ - public static function createUuid7sNormally() - { - static::$uuid7Factory = null; - } - /** * Generate a ULID. * diff --git a/tests/Support/SupportStrTest.php b/tests/Support/SupportStrTest.php index 725326e7e8aa..15e142574b3f 100755 --- a/tests/Support/SupportStrTest.php +++ b/tests/Support/SupportStrTest.php @@ -1101,6 +1101,7 @@ public function testUuid() { $this->assertInstanceOf(UuidInterface::class, Str::uuid()); $this->assertInstanceOf(UuidInterface::class, Str::orderedUuid()); + $this->assertInstanceOf(UuidInterface::class, Str::uuid7()); } public function testAsciiNull() @@ -1354,7 +1355,7 @@ public function testItCanSpecifyASequenceOfUuidsToUtilise() { Str::createUuidsUsingSequence([ 0 => ($zeroth = Str::uuid()), - 1 => ($first = Str::uuid()), + 1 => ($first = Str::uuid7()), // just generate a random one here... 3 => ($third = Str::uuid()), // continue to generate random uuids... From 92a4efb5991bbf8ee70f47231789b432c899e62d Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 18 Jul 2024 13:37:50 -0500 Subject: [PATCH 4/5] formatting --- .../Eloquent/Concerns/{HasV7Uuids.php => HasVersion7Uuids.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/Illuminate/Database/Eloquent/Concerns/{HasV7Uuids.php => HasVersion7Uuids.php} (92%) diff --git a/src/Illuminate/Database/Eloquent/Concerns/HasV7Uuids.php b/src/Illuminate/Database/Eloquent/Concerns/HasVersion7Uuids.php similarity index 92% rename from src/Illuminate/Database/Eloquent/Concerns/HasV7Uuids.php rename to src/Illuminate/Database/Eloquent/Concerns/HasVersion7Uuids.php index 8b70c4a07429..455bf74aa576 100644 --- a/src/Illuminate/Database/Eloquent/Concerns/HasV7Uuids.php +++ b/src/Illuminate/Database/Eloquent/Concerns/HasVersion7Uuids.php @@ -4,7 +4,7 @@ use Illuminate\Support\Str; -trait HasV7Uuids +trait HasVersion7Uuids { use HasUuids; From dfcf3ce5ae7723ba44fe0aaaa1b113695ae2be8a Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Thu, 18 Jul 2024 13:39:12 -0500 Subject: [PATCH 5/5] remove property --- src/Illuminate/Support/Str.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/Illuminate/Support/Str.php b/src/Illuminate/Support/Str.php index 5681c6642394..a9ff9aae2133 100644 --- a/src/Illuminate/Support/Str.php +++ b/src/Illuminate/Support/Str.php @@ -51,13 +51,6 @@ class Str */ protected static $uuidFactory; - /** - * The callback that should be used to generate UUID7s. - * - * @var callable|null - */ - protected static $uuid7Factory; - /** * The callback that should be used to generate ULIDs. *