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