| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the ramsey/uuid library |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
* |
| 9 |
* @copyright Copyright (c) Ben Ramsey <[email protected]> |
| 10 |
* @license http://opensource.org/licenses/MIT MIT |
| 11 |
*/ |
| 12 |
|
| 13 |
declare(strict_types=1); |
| 14 |
|
| 15 |
namespace Ramsey\Uuid; |
| 16 |
|
| 17 |
use Ramsey\Uuid\Builder\BuilderCollection; |
| 18 |
use Ramsey\Uuid\Builder\FallbackBuilder; |
| 19 |
use Ramsey\Uuid\Builder\UuidBuilderInterface; |
| 20 |
use Ramsey\Uuid\Codec\CodecInterface; |
| 21 |
use Ramsey\Uuid\Codec\GuidStringCodec; |
| 22 |
use Ramsey\Uuid\Codec\StringCodec; |
| 23 |
use Ramsey\Uuid\Converter\Number\GenericNumberConverter; |
| 24 |
use Ramsey\Uuid\Converter\NumberConverterInterface; |
| 25 |
use Ramsey\Uuid\Converter\Time\GenericTimeConverter; |
| 26 |
use Ramsey\Uuid\Converter\Time\PhpTimeConverter; |
| 27 |
use Ramsey\Uuid\Converter\TimeConverterInterface; |
| 28 |
use Ramsey\Uuid\Generator\DceSecurityGenerator; |
| 29 |
use Ramsey\Uuid\Generator\DceSecurityGeneratorInterface; |
| 30 |
use Ramsey\Uuid\Generator\NameGeneratorFactory; |
| 31 |
use Ramsey\Uuid\Generator\NameGeneratorInterface; |
| 32 |
use Ramsey\Uuid\Generator\PeclUuidNameGenerator; |
| 33 |
use Ramsey\Uuid\Generator\PeclUuidRandomGenerator; |
| 34 |
use Ramsey\Uuid\Generator\PeclUuidTimeGenerator; |
| 35 |
use Ramsey\Uuid\Generator\RandomGeneratorFactory; |
| 36 |
use Ramsey\Uuid\Generator\RandomGeneratorInterface; |
| 37 |
use Ramsey\Uuid\Generator\TimeGeneratorFactory; |
| 38 |
use Ramsey\Uuid\Generator\TimeGeneratorInterface; |
| 39 |
use Ramsey\Uuid\Guid\GuidBuilder; |
| 40 |
use Ramsey\Uuid\Math\BrickMathCalculator; |
| 41 |
use Ramsey\Uuid\Math\CalculatorInterface; |
| 42 |
use Ramsey\Uuid\Nonstandard\UuidBuilder as NonstandardUuidBuilder; |
| 43 |
use Ramsey\Uuid\Provider\Dce\SystemDceSecurityProvider; |
| 44 |
use Ramsey\Uuid\Provider\DceSecurityProviderInterface; |
| 45 |
use Ramsey\Uuid\Provider\Node\FallbackNodeProvider; |
| 46 |
use Ramsey\Uuid\Provider\Node\NodeProviderCollection; |
| 47 |
use Ramsey\Uuid\Provider\Node\RandomNodeProvider; |
| 48 |
use Ramsey\Uuid\Provider\Node\SystemNodeProvider; |
| 49 |
use Ramsey\Uuid\Provider\NodeProviderInterface; |
| 50 |
use Ramsey\Uuid\Provider\Time\SystemTimeProvider; |
| 51 |
use Ramsey\Uuid\Provider\TimeProviderInterface; |
| 52 |
use Ramsey\Uuid\Rfc4122\UuidBuilder as Rfc4122UuidBuilder; |
| 53 |
use Ramsey\Uuid\Validator\GenericValidator; |
| 54 |
use Ramsey\Uuid\Validator\ValidatorInterface; |
| 55 |
|
| 56 |
use const PHP_INT_SIZE; |
| 57 |
|
| 58 |
/** |
| 59 |
* FeatureSet detects and exposes available features in the current environment |
| 60 |
* |
| 61 |
* A feature set is used by UuidFactory to determine the available features and |
| 62 |
* capabilities of the environment. |
| 63 |
*/ |
| 64 |
class FeatureSet |
| 65 |
{ |
| 66 |
/** |
| 67 |
* @var bool |
| 68 |
*/ |
| 69 |
private $disableBigNumber = false; |
| 70 |
|
| 71 |
/** |
| 72 |
* @var bool |
| 73 |
*/ |
| 74 |
private $disable64Bit = false; |
| 75 |
|
| 76 |
/** |
| 77 |
* @var bool |
| 78 |
*/ |
| 79 |
private $ignoreSystemNode = false; |
| 80 |
|
| 81 |
/** |
| 82 |
* @var bool |
| 83 |
*/ |
| 84 |
private $enablePecl = false; |
| 85 |
|
| 86 |
/** |
| 87 |
* @var UuidBuilderInterface |
| 88 |
*/ |
| 89 |
private $builder; |
| 90 |
|
| 91 |
/** |
| 92 |
* @var CodecInterface |
| 93 |
*/ |
| 94 |
private $codec; |
| 95 |
|
| 96 |
/** |
| 97 |
* @var DceSecurityGeneratorInterface |
| 98 |
*/ |
| 99 |
private $dceSecurityGenerator; |
| 100 |
|
| 101 |
/** |
| 102 |
* @var NameGeneratorInterface |
| 103 |
*/ |
| 104 |
private $nameGenerator; |
| 105 |
|
| 106 |
/** |
| 107 |
* @var NodeProviderInterface |
| 108 |
*/ |
| 109 |
private $nodeProvider; |
| 110 |
|
| 111 |
/** |
| 112 |
* @var NumberConverterInterface |
| 113 |
*/ |
| 114 |
private $numberConverter; |
| 115 |
|
| 116 |
/** |
| 117 |
* @var TimeConverterInterface |
| 118 |
*/ |
| 119 |
private $timeConverter; |
| 120 |
|
| 121 |
/** |
| 122 |
* @var RandomGeneratorInterface |
| 123 |
*/ |
| 124 |
private $randomGenerator; |
| 125 |
|
| 126 |
/** |
| 127 |
* @var TimeGeneratorInterface |
| 128 |
*/ |
| 129 |
private $timeGenerator; |
| 130 |
|
| 131 |
/** |
| 132 |
* @var TimeProviderInterface |
| 133 |
*/ |
| 134 |
private $timeProvider; |
| 135 |
|
| 136 |
/** |
| 137 |
* @var ValidatorInterface |
| 138 |
*/ |
| 139 |
private $validator; |
| 140 |
|
| 141 |
/** |
| 142 |
* @var CalculatorInterface |
| 143 |
*/ |
| 144 |
private $calculator; |
| 145 |
|
| 146 |
/** |
| 147 |
* @param bool $useGuids True build UUIDs using the GuidStringCodec |
| 148 |
* @param bool $force32Bit True to force the use of 32-bit functionality |
| 149 |
* (primarily for testing purposes) |
| 150 |
* @param bool $forceNoBigNumber True to disable the use of moontoast/math |
| 151 |
* (primarily for testing purposes) |
| 152 |
* @param bool $ignoreSystemNode True to disable attempts to check for the |
| 153 |
* system node ID (primarily for testing purposes) |
| 154 |
* @param bool $enablePecl True to enable the use of the PeclUuidTimeGenerator |
| 155 |
* to generate version 1 UUIDs |
| 156 |
*/ |
| 157 |
public function __construct( |
| 158 |
bool $useGuids = false, |
| 159 |
bool $force32Bit = false, |
| 160 |
bool $forceNoBigNumber = false, |
| 161 |
bool $ignoreSystemNode = false, |
| 162 |
bool $enablePecl = false |
| 163 |
) { |
| 164 |
$this->disableBigNumber = $forceNoBigNumber; |
| 165 |
$this->disable64Bit = $force32Bit; |
| 166 |
$this->ignoreSystemNode = $ignoreSystemNode; |
| 167 |
$this->enablePecl = $enablePecl; |
| 168 |
|
| 169 |
$this->setCalculator(new BrickMathCalculator()); |
| 170 |
$this->builder = $this->buildUuidBuilder($useGuids); |
| 171 |
$this->codec = $this->buildCodec($useGuids); |
| 172 |
$this->nodeProvider = $this->buildNodeProvider(); |
| 173 |
$this->nameGenerator = $this->buildNameGenerator(); |
| 174 |
$this->randomGenerator = $this->buildRandomGenerator(); |
| 175 |
$this->setTimeProvider(new SystemTimeProvider()); |
| 176 |
$this->setDceSecurityProvider(new SystemDceSecurityProvider()); |
| 177 |
$this->validator = new GenericValidator(); |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Returns the builder configured for this environment |
| 182 |
*/ |
| 183 |
public function getBuilder(): UuidBuilderInterface |
| 184 |
{ |
| 185 |
return $this->builder; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Returns the calculator configured for this environment |
| 190 |
*/ |
| 191 |
public function getCalculator(): CalculatorInterface |
| 192 |
{ |
| 193 |
return $this->calculator; |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* Returns the codec configured for this environment |
| 198 |
*/ |
| 199 |
public function getCodec(): CodecInterface |
| 200 |
{ |
| 201 |
return $this->codec; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Returns the DCE Security generator configured for this environment |
| 206 |
*/ |
| 207 |
public function getDceSecurityGenerator(): DceSecurityGeneratorInterface |
| 208 |
{ |
| 209 |
return $this->dceSecurityGenerator; |
| 210 |
} |
| 211 |
|
| 212 |
/** |
| 213 |
* Returns the name generator configured for this environment |
| 214 |
*/ |
| 215 |
public function getNameGenerator(): NameGeneratorInterface |
| 216 |
{ |
| 217 |
return $this->nameGenerator; |
| 218 |
} |
| 219 |
|
| 220 |
/** |
| 221 |
* Returns the node provider configured for this environment |
| 222 |
*/ |
| 223 |
public function getNodeProvider(): NodeProviderInterface |
| 224 |
{ |
| 225 |
return $this->nodeProvider; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Returns the number converter configured for this environment |
| 230 |
*/ |
| 231 |
public function getNumberConverter(): NumberConverterInterface |
| 232 |
{ |
| 233 |
return $this->numberConverter; |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Returns the random generator configured for this environment |
| 238 |
*/ |
| 239 |
public function getRandomGenerator(): RandomGeneratorInterface |
| 240 |
{ |
| 241 |
return $this->randomGenerator; |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Returns the time converter configured for this environment |
| 246 |
*/ |
| 247 |
public function getTimeConverter(): TimeConverterInterface |
| 248 |
{ |
| 249 |
return $this->timeConverter; |
| 250 |
} |
| 251 |
|
| 252 |
/** |
| 253 |
* Returns the time generator configured for this environment |
| 254 |
*/ |
| 255 |
public function getTimeGenerator(): TimeGeneratorInterface |
| 256 |
{ |
| 257 |
return $this->timeGenerator; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Returns the validator configured for this environment |
| 262 |
*/ |
| 263 |
public function getValidator(): ValidatorInterface |
| 264 |
{ |
| 265 |
return $this->validator; |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Sets the calculator to use in this environment |
| 270 |
*/ |
| 271 |
public function setCalculator(CalculatorInterface $calculator): void |
| 272 |
{ |
| 273 |
$this->calculator = $calculator; |
| 274 |
$this->numberConverter = $this->buildNumberConverter($calculator); |
| 275 |
$this->timeConverter = $this->buildTimeConverter($calculator); |
| 276 |
|
| 277 |
/** @psalm-suppress RedundantPropertyInitializationCheck */ |
| 278 |
if (isset($this->timeProvider)) { |
| 279 |
$this->timeGenerator = $this->buildTimeGenerator($this->timeProvider); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Sets the DCE Security provider to use in this environment |
| 285 |
*/ |
| 286 |
public function setDceSecurityProvider(DceSecurityProviderInterface $dceSecurityProvider): void |
| 287 |
{ |
| 288 |
$this->dceSecurityGenerator = $this->buildDceSecurityGenerator($dceSecurityProvider); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Sets the node provider to use in this environment |
| 293 |
*/ |
| 294 |
public function setNodeProvider(NodeProviderInterface $nodeProvider): void |
| 295 |
{ |
| 296 |
$this->nodeProvider = $nodeProvider; |
| 297 |
$this->timeGenerator = $this->buildTimeGenerator($this->timeProvider); |
| 298 |
} |
| 299 |
|
| 300 |
/** |
| 301 |
* Sets the time provider to use in this environment |
| 302 |
*/ |
| 303 |
public function setTimeProvider(TimeProviderInterface $timeProvider): void |
| 304 |
{ |
| 305 |
$this->timeProvider = $timeProvider; |
| 306 |
$this->timeGenerator = $this->buildTimeGenerator($timeProvider); |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Set the validator to use in this environment |
| 311 |
*/ |
| 312 |
public function setValidator(ValidatorInterface $validator): void |
| 313 |
{ |
| 314 |
$this->validator = $validator; |
| 315 |
} |
| 316 |
|
| 317 |
/** |
| 318 |
* Returns a codec configured for this environment |
| 319 |
* |
| 320 |
* @param bool $useGuids Whether to build UUIDs using the GuidStringCodec |
| 321 |
*/ |
| 322 |
private function buildCodec(bool $useGuids = false): CodecInterface |
| 323 |
{ |
| 324 |
if ($useGuids) { |
| 325 |
return new GuidStringCodec($this->builder); |
| 326 |
} |
| 327 |
|
| 328 |
return new StringCodec($this->builder); |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* Returns a DCE Security generator configured for this environment |
| 333 |
*/ |
| 334 |
private function buildDceSecurityGenerator( |
| 335 |
DceSecurityProviderInterface $dceSecurityProvider |
| 336 |
): DceSecurityGeneratorInterface { |
| 337 |
return new DceSecurityGenerator( |
| 338 |
$this->numberConverter, |
| 339 |
$this->timeGenerator, |
| 340 |
$dceSecurityProvider |
| 341 |
); |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Returns a node provider configured for this environment |
| 346 |
*/ |
| 347 |
private function buildNodeProvider(): NodeProviderInterface |
| 348 |
{ |
| 349 |
if ($this->ignoreSystemNode) { |
| 350 |
return new RandomNodeProvider(); |
| 351 |
} |
| 352 |
|
| 353 |
return new FallbackNodeProvider(new NodeProviderCollection([ |
| 354 |
new SystemNodeProvider(), |
| 355 |
new RandomNodeProvider(), |
| 356 |
])); |
| 357 |
} |
| 358 |
|
| 359 |
/** |
| 360 |
* Returns a number converter configured for this environment |
| 361 |
*/ |
| 362 |
private function buildNumberConverter(CalculatorInterface $calculator): NumberConverterInterface |
| 363 |
{ |
| 364 |
return new GenericNumberConverter($calculator); |
| 365 |
} |
| 366 |
|
| 367 |
/** |
| 368 |
* Returns a random generator configured for this environment |
| 369 |
*/ |
| 370 |
private function buildRandomGenerator(): RandomGeneratorInterface |
| 371 |
{ |
| 372 |
if ($this->enablePecl) { |
| 373 |
return new PeclUuidRandomGenerator(); |
| 374 |
} |
| 375 |
|
| 376 |
return (new RandomGeneratorFactory())->getGenerator(); |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* Returns a time generator configured for this environment |
| 381 |
* |
| 382 |
* @param TimeProviderInterface $timeProvider The time provider to use with |
| 383 |
* the time generator |
| 384 |
*/ |
| 385 |
private function buildTimeGenerator(TimeProviderInterface $timeProvider): TimeGeneratorInterface |
| 386 |
{ |
| 387 |
if ($this->enablePecl) { |
| 388 |
return new PeclUuidTimeGenerator(); |
| 389 |
} |
| 390 |
|
| 391 |
return (new TimeGeneratorFactory( |
| 392 |
$this->nodeProvider, |
| 393 |
$this->timeConverter, |
| 394 |
$timeProvider |
| 395 |
))->getGenerator(); |
| 396 |
} |
| 397 |
|
| 398 |
/** |
| 399 |
* Returns a name generator configured for this environment |
| 400 |
*/ |
| 401 |
private function buildNameGenerator(): NameGeneratorInterface |
| 402 |
{ |
| 403 |
if ($this->enablePecl) { |
| 404 |
return new PeclUuidNameGenerator(); |
| 405 |
} |
| 406 |
|
| 407 |
return (new NameGeneratorFactory())->getGenerator(); |
| 408 |
} |
| 409 |
|
| 410 |
/** |
| 411 |
* Returns a time converter configured for this environment |
| 412 |
*/ |
| 413 |
private function buildTimeConverter(CalculatorInterface $calculator): TimeConverterInterface |
| 414 |
{ |
| 415 |
$genericConverter = new GenericTimeConverter($calculator); |
| 416 |
|
| 417 |
if ($this->is64BitSystem()) { |
| 418 |
return new PhpTimeConverter($calculator, $genericConverter); |
| 419 |
} |
| 420 |
|
| 421 |
return $genericConverter; |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* Returns a UUID builder configured for this environment |
| 426 |
* |
| 427 |
* @param bool $useGuids Whether to build UUIDs using the GuidStringCodec |
| 428 |
*/ |
| 429 |
private function buildUuidBuilder(bool $useGuids = false): UuidBuilderInterface |
| 430 |
{ |
| 431 |
if ($useGuids) { |
| 432 |
return new GuidBuilder($this->numberConverter, $this->timeConverter); |
| 433 |
} |
| 434 |
|
| 435 |
/** @psalm-suppress ImpureArgument */ |
| 436 |
return new FallbackBuilder(new BuilderCollection([ |
| 437 |
new Rfc4122UuidBuilder($this->numberConverter, $this->timeConverter), |
| 438 |
new NonstandardUuidBuilder($this->numberConverter, $this->timeConverter), |
| 439 |
])); |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* Returns true if the PHP build is 64-bit |
| 444 |
*/ |
| 445 |
private function is64BitSystem(): bool |
| 446 |
{ |
| 447 |
return PHP_INT_SIZE === 8 && !$this->disable64Bit; |
| 448 |
} |
| 449 |
} |
| 450 |
|