[Uid] minor improvements

This commit is contained in:
Nicolas Grekas 2020-03-16 01:17:07 +01:00
parent 7e181b93ca
commit 660326bed3
5 changed files with 25 additions and 3 deletions

View File

@ -78,6 +78,7 @@
<element key="5"><string>Symfony\Component\Cache\Traits</string></element>
<element key="6"><string>Symfony\Component\Console</string></element>
<element key="7"><string>Symfony\Component\HttpFoundation</string></element>
<element key="8"><string>Symfony\Component\Uid</string></element>
</array>
</element>
</array>

View File

@ -74,7 +74,13 @@ abstract class AbstractUid implements \JsonSerializable
*/
public function toRfc4122(): string
{
return uuid_unparse($this->toBinary());
// don't use uuid_unparse(), it's slower
$uuid = bin2hex($this->toBinary());
$uuid = substr_replace($uuid, '-', 8, 0);
$uuid = substr_replace($uuid, '-', 13, 0);
$uuid = substr_replace($uuid, '-', 18, 0);
return substr_replace($uuid, '-', 23, 0);
}
/**

View File

@ -22,7 +22,7 @@ namespace Symfony\Component\Uid;
*/
class Ulid extends AbstractUid
{
private static $time = -1;
private static $time = '';
private static $rand = [];
public function __construct(string $ulid = null)

View File

@ -41,7 +41,12 @@ class Uuid extends AbstractUid
}
if (16 === \strlen($uuid)) {
$uuid = uuid_unparse($uuid);
// don't use uuid_unparse(), it's slower
$uuid = bin2hex($uuid);
$uuid = substr_replace($uuid, '-', 8, 0);
$uuid = substr_replace($uuid, '-', 13, 0);
$uuid = substr_replace($uuid, '-', 18, 0);
$uuid = substr_replace($uuid, '-', 23, 0);
} elseif (26 === \strlen($uuid) && Ulid::isValid($uuid)) {
$uuid = (new Ulid($uuid))->toRfc4122();
}

View File

@ -27,4 +27,14 @@
</exclude>
</whitelist>
</filter>
<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener">
<arguments>
<array>
<element key="time-sensitive"><string>Symfony\Component\Uid</string></element>
</array>
</arguments>
</listener>
</listeners>
</phpunit>