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