initial commit; version 22.5.12042
This commit is contained in:
127
libs/Ramsey/Uuid4/Generator/CombGenerator.php
Normal file
127
libs/Ramsey/Uuid4/Generator/CombGenerator.php
Normal file
@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Generator;
|
||||
|
||||
use Ramsey\Uuid\Converter\NumberConverterInterface;
|
||||
use Ramsey\Uuid\Exception\InvalidArgumentException;
|
||||
|
||||
use function bin2hex;
|
||||
use function explode;
|
||||
use function hex2bin;
|
||||
use function microtime;
|
||||
use function str_pad;
|
||||
use function substr;
|
||||
|
||||
use const STR_PAD_LEFT;
|
||||
|
||||
/**
|
||||
* CombGenerator generates COMBs (combined UUID/timestamp)
|
||||
*
|
||||
* The CombGenerator, when used with the StringCodec (and, by proxy, the
|
||||
* TimestampLastCombCodec) or the TimestampFirstCombCodec, combines the current
|
||||
* timestamp with a UUID (hence the name "COMB"). The timestamp either appears
|
||||
* as the first or last 48 bits of the COMB, depending on the codec used.
|
||||
*
|
||||
* By default, COMBs will have the timestamp set as the last 48 bits of the
|
||||
* identifier.
|
||||
*
|
||||
* ``` php
|
||||
* $factory = new UuidFactory();
|
||||
*
|
||||
* $factory->setRandomGenerator(new CombGenerator(
|
||||
* $factory->getRandomGenerator(),
|
||||
* $factory->getNumberConverter()
|
||||
* ));
|
||||
*
|
||||
* $comb = $factory->uuid4();
|
||||
* ```
|
||||
*
|
||||
* To generate a COMB with the timestamp as the first 48 bits, set the
|
||||
* TimestampFirstCombCodec as the codec.
|
||||
*
|
||||
* ``` php
|
||||
* $factory->setCodec(new TimestampFirstCombCodec($factory->getUuidBuilder()));
|
||||
* ```
|
||||
*
|
||||
* @link https://www.informit.com/articles/printerfriendly/25862 The Cost of GUIDs as Primary Keys
|
||||
*/
|
||||
class CombGenerator implements RandomGeneratorInterface
|
||||
{
|
||||
public const TIMESTAMP_BYTES = 6;
|
||||
|
||||
/**
|
||||
* @var RandomGeneratorInterface
|
||||
*/
|
||||
private $randomGenerator;
|
||||
|
||||
/**
|
||||
* @var NumberConverterInterface
|
||||
*/
|
||||
private $converter;
|
||||
|
||||
public function __construct(
|
||||
RandomGeneratorInterface $generator,
|
||||
NumberConverterInterface $numberConverter
|
||||
) {
|
||||
$this->converter = $numberConverter;
|
||||
$this->randomGenerator = $generator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidArgumentException if $length is not a positive integer
|
||||
* greater than or equal to CombGenerator::TIMESTAMP_BYTES
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function generate(int $length): string
|
||||
{
|
||||
if ($length < self::TIMESTAMP_BYTES || $length < 0) {
|
||||
throw new InvalidArgumentException(
|
||||
'Length must be a positive integer greater than or equal to ' . self::TIMESTAMP_BYTES
|
||||
);
|
||||
}
|
||||
|
||||
$hash = '';
|
||||
if (self::TIMESTAMP_BYTES > 0 && $length > self::TIMESTAMP_BYTES) {
|
||||
$hash = $this->randomGenerator->generate($length - self::TIMESTAMP_BYTES);
|
||||
}
|
||||
|
||||
$lsbTime = str_pad(
|
||||
$this->converter->toHex($this->timestamp()),
|
||||
self::TIMESTAMP_BYTES * 2,
|
||||
'0',
|
||||
STR_PAD_LEFT
|
||||
);
|
||||
|
||||
return (string) hex2bin(
|
||||
str_pad(
|
||||
bin2hex($hash),
|
||||
$length - self::TIMESTAMP_BYTES,
|
||||
'0'
|
||||
)
|
||||
. $lsbTime
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns current timestamp a string integer, precise to 0.00001 seconds
|
||||
*/
|
||||
private function timestamp(): string
|
||||
{
|
||||
$time = explode(' ', microtime(false));
|
||||
|
||||
return $time[1] . substr($time[0], 2, 5);
|
||||
}
|
||||
}
|
160
libs/Ramsey/Uuid4/Generator/DceSecurityGenerator.php
Normal file
160
libs/Ramsey/Uuid4/Generator/DceSecurityGenerator.php
Normal file
@ -0,0 +1,160 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Generator;
|
||||
|
||||
use Ramsey\Uuid\Converter\NumberConverterInterface;
|
||||
use Ramsey\Uuid\Exception\DceSecurityException;
|
||||
use Ramsey\Uuid\Provider\DceSecurityProviderInterface;
|
||||
use Ramsey\Uuid\Type\Hexadecimal;
|
||||
use Ramsey\Uuid\Type\Integer as IntegerObject;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
use function hex2bin;
|
||||
use function in_array;
|
||||
use function pack;
|
||||
use function str_pad;
|
||||
use function strlen;
|
||||
use function substr_replace;
|
||||
|
||||
use const STR_PAD_LEFT;
|
||||
|
||||
/**
|
||||
* DceSecurityGenerator generates strings of binary data based on a local
|
||||
* domain, local identifier, node ID, clock sequence, and the current time
|
||||
*/
|
||||
class DceSecurityGenerator implements DceSecurityGeneratorInterface
|
||||
{
|
||||
private const DOMAINS = [
|
||||
Uuid::DCE_DOMAIN_PERSON,
|
||||
Uuid::DCE_DOMAIN_GROUP,
|
||||
Uuid::DCE_DOMAIN_ORG,
|
||||
];
|
||||
|
||||
/**
|
||||
* Upper bounds for the clock sequence in DCE Security UUIDs.
|
||||
*/
|
||||
private const CLOCK_SEQ_HIGH = 63;
|
||||
|
||||
/**
|
||||
* Lower bounds for the clock sequence in DCE Security UUIDs.
|
||||
*/
|
||||
private const CLOCK_SEQ_LOW = 0;
|
||||
|
||||
/**
|
||||
* @var NumberConverterInterface
|
||||
*/
|
||||
private $numberConverter;
|
||||
|
||||
/**
|
||||
* @var TimeGeneratorInterface
|
||||
*/
|
||||
private $timeGenerator;
|
||||
|
||||
/**
|
||||
* @var DceSecurityProviderInterface
|
||||
*/
|
||||
private $dceSecurityProvider;
|
||||
|
||||
public function __construct(
|
||||
NumberConverterInterface $numberConverter,
|
||||
TimeGeneratorInterface $timeGenerator,
|
||||
DceSecurityProviderInterface $dceSecurityProvider
|
||||
) {
|
||||
$this->numberConverter = $numberConverter;
|
||||
$this->timeGenerator = $timeGenerator;
|
||||
$this->dceSecurityProvider = $dceSecurityProvider;
|
||||
}
|
||||
|
||||
public function generate(
|
||||
int $localDomain,
|
||||
?IntegerObject $localIdentifier = null,
|
||||
?Hexadecimal $node = null,
|
||||
?int $clockSeq = null
|
||||
): string {
|
||||
if (!in_array($localDomain, self::DOMAINS)) {
|
||||
throw new DceSecurityException(
|
||||
'Local domain must be a valid DCE Security domain'
|
||||
);
|
||||
}
|
||||
|
||||
if ($localIdentifier && $localIdentifier->isNegative()) {
|
||||
throw new DceSecurityException(
|
||||
'Local identifier out of bounds; it must be a value between 0 and 4294967295'
|
||||
);
|
||||
}
|
||||
|
||||
if ($clockSeq > self::CLOCK_SEQ_HIGH || $clockSeq < self::CLOCK_SEQ_LOW) {
|
||||
throw new DceSecurityException(
|
||||
'Clock sequence out of bounds; it must be a value between 0 and 63'
|
||||
);
|
||||
}
|
||||
|
||||
switch ($localDomain) {
|
||||
case Uuid::DCE_DOMAIN_ORG:
|
||||
if ($localIdentifier === null) {
|
||||
throw new DceSecurityException(
|
||||
'A local identifier must be provided for the org domain'
|
||||
);
|
||||
}
|
||||
|
||||
break;
|
||||
case Uuid::DCE_DOMAIN_PERSON:
|
||||
if ($localIdentifier === null) {
|
||||
$localIdentifier = $this->dceSecurityProvider->getUid();
|
||||
}
|
||||
|
||||
break;
|
||||
case Uuid::DCE_DOMAIN_GROUP:
|
||||
default:
|
||||
if ($localIdentifier === null) {
|
||||
$localIdentifier = $this->dceSecurityProvider->getGid();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
$identifierHex = $this->numberConverter->toHex($localIdentifier->toString());
|
||||
|
||||
// The maximum value for the local identifier is 0xffffffff, or
|
||||
// 4294967295. This is 8 hexadecimal digits, so if the length of
|
||||
// hexadecimal digits is greater than 8, we know the value is greater
|
||||
// than 0xffffffff.
|
||||
if (strlen($identifierHex) > 8) {
|
||||
throw new DceSecurityException(
|
||||
'Local identifier out of bounds; it must be a value between 0 and 4294967295'
|
||||
);
|
||||
}
|
||||
|
||||
$domainByte = pack('n', $localDomain)[1];
|
||||
$identifierBytes = (string) hex2bin(str_pad($identifierHex, 8, '0', STR_PAD_LEFT));
|
||||
|
||||
if ($node instanceof Hexadecimal) {
|
||||
$node = $node->toString();
|
||||
}
|
||||
|
||||
// Shift the clock sequence 8 bits to the left, so it matches 0x3f00.
|
||||
if ($clockSeq !== null) {
|
||||
$clockSeq = $clockSeq << 8;
|
||||
}
|
||||
|
||||
$bytes = $this->timeGenerator->generate($node, $clockSeq);
|
||||
|
||||
// Replace bytes in the time-based UUID with DCE Security values.
|
||||
$bytes = substr_replace($bytes, $identifierBytes, 0, 4);
|
||||
$bytes = substr_replace($bytes, $domainByte, 9, 1);
|
||||
|
||||
return $bytes;
|
||||
}
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Generator;
|
||||
|
||||
use Ramsey\Uuid\Rfc4122\UuidV2;
|
||||
use Ramsey\Uuid\Type\Hexadecimal;
|
||||
use Ramsey\Uuid\Type\Integer as IntegerObject;
|
||||
|
||||
/**
|
||||
* A DCE Security generator generates strings of binary data based on a local
|
||||
* domain, local identifier, node ID, clock sequence, and the current time
|
||||
*
|
||||
* @see UuidV2
|
||||
*/
|
||||
interface DceSecurityGeneratorInterface
|
||||
{
|
||||
/**
|
||||
* Generate a binary string from a local domain, local identifier, node ID,
|
||||
* clock sequence, and current time
|
||||
*
|
||||
* @param int $localDomain The local domain to use when generating bytes,
|
||||
* according to DCE Security
|
||||
* @param IntegerObject|null $localIdentifier The local identifier for the
|
||||
* given domain; this may be a UID or GID on POSIX systems, if the local
|
||||
* domain is person or group, or it may be a site-defined identifier
|
||||
* if the local domain is org
|
||||
* @param Hexadecimal|null $node A 48-bit number representing the hardware
|
||||
* address
|
||||
* @param int|null $clockSeq A 14-bit number used to help avoid duplicates
|
||||
* that could arise when the clock is set backwards in time or if the
|
||||
* node ID changes
|
||||
*
|
||||
* @return string A binary string
|
||||
*/
|
||||
public function generate(
|
||||
int $localDomain,
|
||||
?IntegerObject $localIdentifier = null,
|
||||
?Hexadecimal $node = null,
|
||||
?int $clockSeq = null
|
||||
): string;
|
||||
}
|
48
libs/Ramsey/Uuid4/Generator/DefaultNameGenerator.php
Normal file
48
libs/Ramsey/Uuid4/Generator/DefaultNameGenerator.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Generator;
|
||||
|
||||
use Ramsey\Uuid\Exception\NameException;
|
||||
use Ramsey\Uuid\UuidInterface;
|
||||
use ValueError;
|
||||
|
||||
use function hash;
|
||||
|
||||
/**
|
||||
* DefaultNameGenerator generates strings of binary data based on a namespace,
|
||||
* name, and hashing algorithm
|
||||
*/
|
||||
class DefaultNameGenerator implements NameGeneratorInterface
|
||||
{
|
||||
/** @psalm-pure */
|
||||
public function generate(UuidInterface $ns, string $name, string $hashAlgorithm): string
|
||||
{
|
||||
try {
|
||||
/** @var string|bool $bytes */
|
||||
$bytes = @hash($hashAlgorithm, $ns->getBytes() . $name, true);
|
||||
} catch (ValueError $e) {
|
||||
$bytes = false; // keep same behavior than PHP 7
|
||||
}
|
||||
|
||||
if ($bytes === false) {
|
||||
throw new NameException(sprintf(
|
||||
'Unable to hash namespace and name with algorithm \'%s\'',
|
||||
$hashAlgorithm
|
||||
));
|
||||
}
|
||||
|
||||
return (string) $bytes;
|
||||
}
|
||||
}
|
147
libs/Ramsey/Uuid4/Generator/DefaultTimeGenerator.php
Normal file
147
libs/Ramsey/Uuid4/Generator/DefaultTimeGenerator.php
Normal file
@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Generator;
|
||||
|
||||
use Ramsey\Uuid\Converter\TimeConverterInterface;
|
||||
use Ramsey\Uuid\Exception\InvalidArgumentException;
|
||||
use Ramsey\Uuid\Exception\RandomSourceException;
|
||||
use Ramsey\Uuid\Exception\TimeSourceException;
|
||||
use Ramsey\Uuid\Provider\NodeProviderInterface;
|
||||
use Ramsey\Uuid\Provider\TimeProviderInterface;
|
||||
use Ramsey\Uuid\Type\Hexadecimal;
|
||||
use Throwable;
|
||||
|
||||
use function ctype_xdigit;
|
||||
use function dechex;
|
||||
use function hex2bin;
|
||||
use function is_int;
|
||||
use function pack;
|
||||
use function sprintf;
|
||||
use function str_pad;
|
||||
use function strlen;
|
||||
|
||||
use const STR_PAD_LEFT;
|
||||
|
||||
/**
|
||||
* DefaultTimeGenerator generates strings of binary data based on a node ID,
|
||||
* clock sequence, and the current time
|
||||
*/
|
||||
class DefaultTimeGenerator implements TimeGeneratorInterface
|
||||
{
|
||||
/**
|
||||
* @var NodeProviderInterface
|
||||
*/
|
||||
private $nodeProvider;
|
||||
|
||||
/**
|
||||
* @var TimeConverterInterface
|
||||
*/
|
||||
private $timeConverter;
|
||||
|
||||
/**
|
||||
* @var TimeProviderInterface
|
||||
*/
|
||||
private $timeProvider;
|
||||
|
||||
public function __construct(
|
||||
NodeProviderInterface $nodeProvider,
|
||||
TimeConverterInterface $timeConverter,
|
||||
TimeProviderInterface $timeProvider
|
||||
) {
|
||||
$this->nodeProvider = $nodeProvider;
|
||||
$this->timeConverter = $timeConverter;
|
||||
$this->timeProvider = $timeProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidArgumentException if the parameters contain invalid values
|
||||
* @throws RandomSourceException if random_int() throws an exception/error
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function generate($node = null, ?int $clockSeq = null): string
|
||||
{
|
||||
if ($node instanceof Hexadecimal) {
|
||||
$node = $node->toString();
|
||||
}
|
||||
|
||||
$node = $this->getValidNode($node);
|
||||
|
||||
if ($clockSeq === null) {
|
||||
try {
|
||||
// This does not use "stable storage"; see RFC 4122, Section 4.2.1.1.
|
||||
$clockSeq = random_int(0, 0x3fff);
|
||||
} catch (Throwable $exception) {
|
||||
throw new RandomSourceException(
|
||||
$exception->getMessage(),
|
||||
(int) $exception->getCode(),
|
||||
$exception
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$time = $this->timeProvider->getTime();
|
||||
|
||||
$uuidTime = $this->timeConverter->calculateTime(
|
||||
$time->getSeconds()->toString(),
|
||||
$time->getMicroseconds()->toString()
|
||||
);
|
||||
|
||||
$timeHex = str_pad($uuidTime->toString(), 16, '0', STR_PAD_LEFT);
|
||||
|
||||
if (strlen($timeHex) !== 16) {
|
||||
throw new TimeSourceException(sprintf(
|
||||
'The generated time of \'%s\' is larger than expected',
|
||||
$timeHex
|
||||
));
|
||||
}
|
||||
|
||||
$timeBytes = (string) hex2bin($timeHex);
|
||||
|
||||
return $timeBytes[4] . $timeBytes[5] . $timeBytes[6] . $timeBytes[7]
|
||||
. $timeBytes[2] . $timeBytes[3]
|
||||
. $timeBytes[0] . $timeBytes[1]
|
||||
. pack('n*', $clockSeq)
|
||||
. $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses the node provider given when constructing this instance to get
|
||||
* the node ID (usually a MAC address)
|
||||
*
|
||||
* @param string|int|null $node A node value that may be used to override the node provider
|
||||
*
|
||||
* @return string 6-byte binary string representation of the node
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
private function getValidNode($node): string
|
||||
{
|
||||
if ($node === null) {
|
||||
$node = $this->nodeProvider->getNode();
|
||||
}
|
||||
|
||||
// Convert the node to hex, if it is still an integer.
|
||||
if (is_int($node)) {
|
||||
$node = dechex($node);
|
||||
}
|
||||
|
||||
if (!ctype_xdigit((string) $node) || strlen((string) $node) > 12) {
|
||||
throw new InvalidArgumentException('Invalid node value');
|
||||
}
|
||||
|
||||
return (string) hex2bin(str_pad((string) $node, 12, '0', STR_PAD_LEFT));
|
||||
}
|
||||
}
|
30
libs/Ramsey/Uuid4/Generator/NameGeneratorFactory.php
Normal file
30
libs/Ramsey/Uuid4/Generator/NameGeneratorFactory.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Generator;
|
||||
|
||||
/**
|
||||
* NameGeneratorFactory retrieves a default name generator, based on the
|
||||
* environment
|
||||
*/
|
||||
class NameGeneratorFactory
|
||||
{
|
||||
/**
|
||||
* Returns a default name generator, based on the current environment
|
||||
*/
|
||||
public function getGenerator(): NameGeneratorInterface
|
||||
{
|
||||
return new DefaultNameGenerator();
|
||||
}
|
||||
}
|
38
libs/Ramsey/Uuid4/Generator/NameGeneratorInterface.php
Normal file
38
libs/Ramsey/Uuid4/Generator/NameGeneratorInterface.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Generator;
|
||||
|
||||
use Ramsey\Uuid\UuidInterface;
|
||||
|
||||
/**
|
||||
* A name generator generates strings of binary data created by hashing together
|
||||
* a namespace with a name, according to a hashing algorithm
|
||||
*/
|
||||
interface NameGeneratorInterface
|
||||
{
|
||||
/**
|
||||
* Generate a binary string from a namespace and name hashed together with
|
||||
* the specified hashing algorithm
|
||||
*
|
||||
* @param UuidInterface $ns The namespace
|
||||
* @param string $name The name to use for creating a UUID
|
||||
* @param string $hashAlgorithm The hashing algorithm to use
|
||||
*
|
||||
* @return string A binary string
|
||||
*
|
||||
* @psalm-pure
|
||||
*/
|
||||
public function generate(UuidInterface $ns, string $name, string $hashAlgorithm): string;
|
||||
}
|
54
libs/Ramsey/Uuid4/Generator/PeclUuidNameGenerator.php
Normal file
54
libs/Ramsey/Uuid4/Generator/PeclUuidNameGenerator.php
Normal file
@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Generator;
|
||||
|
||||
use Ramsey\Uuid\Exception\NameException;
|
||||
use Ramsey\Uuid\UuidInterface;
|
||||
|
||||
use function sprintf;
|
||||
use function uuid_generate_md5;
|
||||
use function uuid_generate_sha1;
|
||||
use function uuid_parse;
|
||||
|
||||
/**
|
||||
* PeclUuidNameGenerator generates strings of binary data from a namespace and a
|
||||
* name, using ext-uuid
|
||||
*
|
||||
* @link https://pecl.php.net/package/uuid ext-uuid
|
||||
*/
|
||||
class PeclUuidNameGenerator implements NameGeneratorInterface
|
||||
{
|
||||
/** @psalm-pure */
|
||||
public function generate(UuidInterface $ns, string $name, string $hashAlgorithm): string
|
||||
{
|
||||
switch ($hashAlgorithm) {
|
||||
case 'md5':
|
||||
$uuid = uuid_generate_md5($ns->toString(), $name);
|
||||
|
||||
break;
|
||||
case 'sha1':
|
||||
$uuid = uuid_generate_sha1($ns->toString(), $name);
|
||||
|
||||
break;
|
||||
default:
|
||||
throw new NameException(sprintf(
|
||||
'Unable to hash namespace and name with algorithm \'%s\'',
|
||||
$hashAlgorithm
|
||||
));
|
||||
}
|
||||
|
||||
return uuid_parse($uuid);
|
||||
}
|
||||
}
|
35
libs/Ramsey/Uuid4/Generator/PeclUuidRandomGenerator.php
Normal file
35
libs/Ramsey/Uuid4/Generator/PeclUuidRandomGenerator.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Generator;
|
||||
|
||||
use function uuid_create;
|
||||
use function uuid_parse;
|
||||
|
||||
use const UUID_TYPE_RANDOM;
|
||||
|
||||
/**
|
||||
* PeclUuidRandomGenerator generates strings of random binary data using ext-uuid
|
||||
*
|
||||
* @link https://pecl.php.net/package/uuid ext-uuid
|
||||
*/
|
||||
class PeclUuidRandomGenerator implements RandomGeneratorInterface
|
||||
{
|
||||
public function generate(int $length): string
|
||||
{
|
||||
$uuid = uuid_create(UUID_TYPE_RANDOM);
|
||||
|
||||
return uuid_parse($uuid);
|
||||
}
|
||||
}
|
39
libs/Ramsey/Uuid4/Generator/PeclUuidTimeGenerator.php
Normal file
39
libs/Ramsey/Uuid4/Generator/PeclUuidTimeGenerator.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Generator;
|
||||
|
||||
use function uuid_create;
|
||||
use function uuid_parse;
|
||||
|
||||
use const UUID_TYPE_TIME;
|
||||
|
||||
/**
|
||||
* PeclUuidTimeGenerator generates strings of binary data for time-base UUIDs,
|
||||
* using ext-uuid
|
||||
*
|
||||
* @link https://pecl.php.net/package/uuid ext-uuid
|
||||
*/
|
||||
class PeclUuidTimeGenerator implements TimeGeneratorInterface
|
||||
{
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function generate($node = null, ?int $clockSeq = null): string
|
||||
{
|
||||
$uuid = uuid_create(UUID_TYPE_TIME);
|
||||
|
||||
return uuid_parse($uuid);
|
||||
}
|
||||
}
|
45
libs/Ramsey/Uuid4/Generator/RandomBytesGenerator.php
Normal file
45
libs/Ramsey/Uuid4/Generator/RandomBytesGenerator.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Generator;
|
||||
|
||||
use Ramsey\Uuid\Exception\RandomSourceException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* RandomBytesGenerator generates strings of random binary data using the
|
||||
* built-in `random_bytes()` PHP function
|
||||
*
|
||||
* @link http://php.net/random_bytes random_bytes()
|
||||
*/
|
||||
class RandomBytesGenerator implements RandomGeneratorInterface
|
||||
{
|
||||
/**
|
||||
* @throws RandomSourceException if random_bytes() throws an exception/error
|
||||
*
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function generate(int $length): string
|
||||
{
|
||||
try {
|
||||
return random_bytes($length);
|
||||
} catch (Throwable $exception) {
|
||||
throw new RandomSourceException(
|
||||
$exception->getMessage(),
|
||||
(int) $exception->getCode(),
|
||||
$exception
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
30
libs/Ramsey/Uuid4/Generator/RandomGeneratorFactory.php
Normal file
30
libs/Ramsey/Uuid4/Generator/RandomGeneratorFactory.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Generator;
|
||||
|
||||
/**
|
||||
* RandomGeneratorFactory retrieves a default random generator, based on the
|
||||
* environment
|
||||
*/
|
||||
class RandomGeneratorFactory
|
||||
{
|
||||
/**
|
||||
* Returns a default random generator, based on the current environment
|
||||
*/
|
||||
public function getGenerator(): RandomGeneratorInterface
|
||||
{
|
||||
return new RandomBytesGenerator();
|
||||
}
|
||||
}
|
30
libs/Ramsey/Uuid4/Generator/RandomGeneratorInterface.php
Normal file
30
libs/Ramsey/Uuid4/Generator/RandomGeneratorInterface.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Generator;
|
||||
|
||||
/**
|
||||
* A random generator generates strings of random binary data
|
||||
*/
|
||||
interface RandomGeneratorInterface
|
||||
{
|
||||
/**
|
||||
* Generates a string of randomized binary data
|
||||
*
|
||||
* @param int $length The number of bytes of random binary data to generate
|
||||
*
|
||||
* @return string A binary string
|
||||
*/
|
||||
public function generate(int $length): string;
|
||||
}
|
55
libs/Ramsey/Uuid4/Generator/RandomLibAdapter.php
Normal file
55
libs/Ramsey/Uuid4/Generator/RandomLibAdapter.php
Normal file
@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Generator;
|
||||
|
||||
use RandomLib\Factory;
|
||||
use RandomLib\Generator;
|
||||
|
||||
/**
|
||||
* RandomLibAdapter generates strings of random binary data using the
|
||||
* paragonie/random-lib library
|
||||
*
|
||||
* @link https://packagist.org/packages/paragonie/random-lib paragonie/random-lib
|
||||
*/
|
||||
class RandomLibAdapter implements RandomGeneratorInterface
|
||||
{
|
||||
/**
|
||||
* @var Generator
|
||||
*/
|
||||
private $generator;
|
||||
|
||||
/**
|
||||
* Constructs a RandomLibAdapter
|
||||
*
|
||||
* By default, if no Generator is passed in, this creates a high-strength
|
||||
* generator to use when generating random binary data.
|
||||
*
|
||||
* @param Generator|null $generator The generator to use when generating binary data
|
||||
*/
|
||||
public function __construct(?Generator $generator = null)
|
||||
{
|
||||
if ($generator === null) {
|
||||
$factory = new Factory();
|
||||
$generator = $factory->getHighStrengthGenerator();
|
||||
}
|
||||
|
||||
$this->generator = $generator;
|
||||
}
|
||||
|
||||
public function generate(int $length): string
|
||||
{
|
||||
return $this->generator->generate($length);
|
||||
}
|
||||
}
|
63
libs/Ramsey/Uuid4/Generator/TimeGeneratorFactory.php
Normal file
63
libs/Ramsey/Uuid4/Generator/TimeGeneratorFactory.php
Normal file
@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Generator;
|
||||
|
||||
use Ramsey\Uuid\Converter\TimeConverterInterface;
|
||||
use Ramsey\Uuid\Provider\NodeProviderInterface;
|
||||
use Ramsey\Uuid\Provider\TimeProviderInterface;
|
||||
|
||||
/**
|
||||
* TimeGeneratorFactory retrieves a default time generator, based on the
|
||||
* environment
|
||||
*/
|
||||
class TimeGeneratorFactory
|
||||
{
|
||||
/**
|
||||
* @var NodeProviderInterface
|
||||
*/
|
||||
private $nodeProvider;
|
||||
|
||||
/**
|
||||
* @var TimeConverterInterface
|
||||
*/
|
||||
private $timeConverter;
|
||||
|
||||
/**
|
||||
* @var TimeProviderInterface
|
||||
*/
|
||||
private $timeProvider;
|
||||
|
||||
public function __construct(
|
||||
NodeProviderInterface $nodeProvider,
|
||||
TimeConverterInterface $timeConverter,
|
||||
TimeProviderInterface $timeProvider
|
||||
) {
|
||||
$this->nodeProvider = $nodeProvider;
|
||||
$this->timeConverter = $timeConverter;
|
||||
$this->timeProvider = $timeProvider;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a default time generator, based on the current environment
|
||||
*/
|
||||
public function getGenerator(): TimeGeneratorInterface
|
||||
{
|
||||
return new DefaultTimeGenerator(
|
||||
$this->nodeProvider,
|
||||
$this->timeConverter,
|
||||
$this->timeProvider
|
||||
);
|
||||
}
|
||||
}
|
38
libs/Ramsey/Uuid4/Generator/TimeGeneratorInterface.php
Normal file
38
libs/Ramsey/Uuid4/Generator/TimeGeneratorInterface.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*
|
||||
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Generator;
|
||||
|
||||
use Ramsey\Uuid\Type\Hexadecimal;
|
||||
|
||||
/**
|
||||
* A time generator generates strings of binary data based on a node ID,
|
||||
* clock sequence, and the current time
|
||||
*/
|
||||
interface TimeGeneratorInterface
|
||||
{
|
||||
/**
|
||||
* Generate a binary string from a node ID, clock sequence, and current time
|
||||
*
|
||||
* @param Hexadecimal|int|string|null $node A 48-bit number representing the
|
||||
* hardware address; this number may be represented as an integer or a
|
||||
* hexadecimal string
|
||||
* @param int|null $clockSeq A 14-bit number used to help avoid duplicates
|
||||
* that could arise when the clock is set backwards in time or if the
|
||||
* node ID changes
|
||||
*
|
||||
* @return string A binary string
|
||||
*/
|
||||
public function generate($node = null, ?int $clockSeq = null): string;
|
||||
}
|
Reference in New Issue
Block a user