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